Android的 - 加快插入在数据库中的数据 [英] Android - Speed up inserting data in database

查看:147
本文介绍了Android的 - 加快插入在数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有我解析一个CSV文件,我试图将数据插入到android的数据库。我遇到的问题是,它走的时间太长插入的所有数据。它的数据量好,但我觉得它不应该采取20分钟左右完成。

基本上,我创建的数据库,然后开始分析。虽然通过每个个体CSV行分析,我抢了所需的数据,并将其插入到数据库中。总共大约有40000行。

有没有什么办法可以加速这一进程?我曾尝试批量插入,但它从来没有真正帮助了(除非我这样做是错误的)。

向下跌破code。

感谢。

DatabaseHelper (我有一个基于数据的每个CSV行中的金额两个插入命令):

  //添加ZIP code
    公共无效add9Zip code(字符串拉链,字符串市,州字符串,字符串纬度,
            字符串茇,字符串DECOM){

        //获取数据库和内容价值
        SQLiteDatabase DB = this.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();

        db.beginTransaction();
        尝试{

            //添加值
            values​​.put(KEY_ZIP,ZIP);
            values​​.put(KEY_STATE,状态);
            values​​.put(KEY_CITY,市);
            values​​.put(KEY_LAT,纬度);
            values​​.put(KEY_LONG,隆基);
            values​​.put(KEY_DECOM,DECOM);

            //执行语句
            db.insert(TABLE_NAME,空,价值观);

            db.setTransactionSuccessful();
        } 最后 {
            db.endTransaction();
        }

        db.close();

    }

    公共无效add12Zip code(字符串拉链,字符串市,州字符串,字符串纬度,
            字符串茇,字符串DECOM,字符串税,字符串流行,字符串工资){

        //获取数据库和内容价值
        SQLiteDatabase DB = this.getWritableDatabase();
        ContentValues​​值=新ContentValues​​();

        db.beginTransaction();
        尝试{
            //添加值
            values​​.put(KEY_ZIP,ZIP);
            values​​.put(KEY_STATE,状态);
            values​​.put(KEY_CITY,市);
            values​​.put(KEY_LAT,纬度);
            values​​.put(KEY_LONG,隆基);
            values​​.put(KEY_DECOM,DECOM);
            values​​.put(KEY_TAX,含税);
            values​​.put(KEY_POP,流行);
            values​​.put(KEY_WAGES,工资);

            //执行语句
            db.insert(TABLE_NAME,空,价值观);

            db.setTransactionSuccessful();
        } 最后{
            db.endTransaction();
        }


        db.close();
}
 

解析文件:

 公共无效解析(ArrayList中<字符串> theArray,数据库处理器DB){

        的String []数据= NULL;

        // while循环来获取数据拆分成新行
        // for循环来分割每个字符串拉链codeS的数组列表中
        为(中间体X = 0 X  - 其中; theArray.size(); X ++){

            如果(X == 10000 || x == 20000 || x == 30000 || x == 40000){
                Log.d(TAG,x是10K,20K,30K,40K);
            }

            //拆分字符串第一个到一个数组
            数据= theArray.get(x)的.split(,);

            //分开​​基于所述阵列的尺寸:9或12
            如果(data.length == 9){

                db.add9Zip code(数据[0],数据[2],数据[2],数据[5],数据[6],
                        数据[8]);

            }否则,如果(data.length == 12){

                db.add12Zip code(数据[0],数据[2],数据[2],数据[5],数据[6],
                        数据[8],数据[9],数据[10],数据[11]);

                / *
                 * theZip.zip =数据[0]; theZip.city =数据[2]; theZip.state =
                 *数据[3]; theZip.lat =数据[5]; theZip.longi =数据[6];
                 * theZip.decom =数据[8]; theZip。数据= [9]; theZip.population
                 * =数据[10]; theZip.wages =数据[11];
                 * /

            }
        }
 

解决方案

请参考这个答案我做了previously:插入百万在sqlite3的数据库

总之,使用 InsertHelper ,做一个以上的插入每次交易 - 除非你做了什么靠不住的,速度的提升应该是显着的。

编辑:
总之:

  1. SQLiteOpenHelper 应该是整个应用程序中使用一个单例。
  2. 请不要到处去调用关闭() SQLiteDatabase 实例 - 它的缓存中的 SQLiteOpenHelper 并在每次关闭你强迫辅助时间重新打开它。
  3. 批量的插入,开始调用 addZip code 方法之外的交易,将其标记为成功,你已经做了所有的插入之后 - 然后提交该交易。
  4. 使用的 InsertHelper - 它可以正确格式插入为prepared说法,是很好的和可重复使用
  5. 要留意同步访问数据库的 - 除非你打算做的UI线程上的所有数据库的工作(不推荐) - 您可能需要能够锁定或保护访问数据库,以避免并发访问。

I currently have a CSV file that I parse and am trying to insert the data into the android database. The problem I am having is that it is taking way too long to insert all of the data. It's a good amount of data but I feel like it shouldn't take 20min or so to complete.

Basically, I create my database, then begin the parsing. While parsing through each individual CSV row, I grab the required data and insert it into the database. In total there are around 40000 rows.

Is there any way I can speed up this process? I have tried batch inserts but it never really helped (unless I did it wrong).

Code down below.

Thanks.

DatabaseHelper (i have two insert commands based on the amount of data in each csv row):

// add zipcode
    public void add9Zipcode(String zip, String city, String state, String lat,
            String longi, String decom) {

        // get db and content values
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        db.beginTransaction();
        try{

            // add the values
            values.put(KEY_ZIP, zip);
            values.put(KEY_STATE, state);
            values.put(KEY_CITY, city);
            values.put(KEY_LAT, lat);
            values.put(KEY_LONG, longi);
            values.put(KEY_DECOM, decom);

            // execute the statement
            db.insert(TABLE_NAME, null, values);

            db.setTransactionSuccessful();
        } finally {
            db.endTransaction();
        }

        db.close();

    }

    public void add12Zipcode(String zip, String city, String state, String lat,
            String longi, String decom, String tax, String pop, String wages) {

        // get db and content values
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        db.beginTransaction();
        try{
            // add the values
            values.put(KEY_ZIP, zip);
            values.put(KEY_STATE, state);
            values.put(KEY_CITY, city);
            values.put(KEY_LAT, lat);
            values.put(KEY_LONG, longi);
            values.put(KEY_DECOM, decom);
            values.put(KEY_TAX, tax);
            values.put(KEY_POP, pop);
            values.put(KEY_WAGES, wages);

            // execute the statement
            db.insert(TABLE_NAME, null, values);

            db.setTransactionSuccessful();
        } finally{
            db.endTransaction();  
        }


        db.close();
}

Parse File:

public void parse(ArrayList<String> theArray, DatabaseHandler db) {

        String[] data = null;

        // while loop to get split the data into new lines
        // for loop to split each string in the array list of zipcodes
        for (int x = 0; x < theArray.size(); x++) {

            if(x == 10000 || x == 20000 || x == 30000 || x == 40000){
                Log.d(TAG, "x is 10k, 20k, 30k, 40k");
            }

            // split string first into an array
            data = theArray.get(x).split(",");

            // separate based on the size of the array: 9 or 12
            if (data.length == 9) {

                db.add9Zipcode(data[0], data[2], data[3], data[5], data[6],
                        data[8]);

            } else if (data.length == 12) {

                db.add12Zipcode(data[0], data[2], data[3], data[5], data[6],
                        data[8], data[9], data[10], data[11]);

                /*
                 * theZip.zip = data[0]; theZip.city = data[2]; theZip.state =
                 * data[3]; theZip.lat = data[5]; theZip.longi = data[6];
                 * theZip.decom = data[8]; theZip. = data[9]; theZip.population
                 * = data[10]; theZip.wages = data[11];
                 */

            }
        }

解决方案

Refer to this answer I made previously: Inserting 1000000 rows in sqlite3 database

In short, use an InsertHelper and do more than one insert per transaction - unless you did something wonky, the speed increase should be noticeable.

Edit:
In short:

  1. Your SQLiteOpenHelper should be a singleton used across your entire application.
  2. Don't go around calling close() on your SQLiteDatabase instance - it's cached in the SQLiteOpenHelper and every time you close you force the helper to reopen it.
  3. Batch your inserts, start a transaction outside the call to the addZipCode methods and mark it as successful after you've done all the inserts - then commit the transaction.
  4. Use an InsertHelper - it will format the insert properly as a prepared statement and is nice and reusable.
  5. Be mindful of synchronizing access to the database - unless you intend to do all your database work on the UI-thread (which is not recommended) - you either need to enable locking or guard access to the database to avoid concurrent access.

这篇关于Android的 - 加快插入在数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆