Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android contentprovider insert takes a lot of time

I made my own contentprovider where I put a lot of data in at once with multiple inserts.

The app will receive the data from an external source and at this moment I receive about 30 items (therefor 30 times an insert).

Now I noticed that this takes a lot of precious time (about 3 seconds, 100ms on each insert).

How can I improve the speed of the contentprovider? I already tried to bulkInsert them all together but them it will take up to 5 sec.

Thanks in advance.

like image 575
Ion Aalbers Avatar asked Dec 01 '11 14:12

Ion Aalbers


2 Answers

wrap all that in insertBulk into transactions.

Example:

    SQLiteDatabase sqlDB = mDB.getWritableDatabase();
    sqlDB.beginTransaction();
    try {

        for (ContentValues cv : values) {
            long newID = sqlDB.insertOrThrow(table, null, cv);
            if (newID <= 0) {
                throw new SQLException("Failed to insert row into " + uri);
            }
        }
        sqlDB.setTransactionSuccessful();
        getContext().getContentResolver().notifyChange(uri, null);
        numInserted = values.length;
    } finally {
        sqlDB.endTransaction();
    }

bulkInsert does not use transactions by default, since the default behavior just calls insert:

Override this to handle requests to insert a set of new rows, or the default implementation will iterate over the values and call insert(Uri, ContentValues) on each of them.

like image 187
davidcesarino Avatar answered Oct 23 '22 06:10

davidcesarino


doing the inserts in a transaction greatly improves the speed, since only one write to the actuall database takes place:

db.beginTransaction();
try {
    // do the inserts
    db.setTransactionSuccessful()
} finally {
    db.endTransaction();
}

I was once experimenting trying to improve the write speed of about ~2000 writes, and this was the only big improvement I found.

Doing db.setLockingEnabled(false) i think gave about 1% improvement, but then you must also make sure no other threads write to the db. Removing redundant indexes can also give a minor boost if the table is huge.

like image 43
pgsandstrom Avatar answered Oct 23 '22 05:10

pgsandstrom