Android - Speed up inserting data in database

2019-02-28 05:09发布

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];
                 */

            }
        }

1条回答
The star\"
2楼-- · 2019-02-28 05:42

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.
查看更多
登录 后发表回答