Paste Android content takes time

I made my own content content, where I immediately put a lot of data with multiple inserts.

The application will receive data from an external source, and at this moment I get about 30 points (therefore, insert 30 times).

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

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

Thanks in advance.

+7
source share
2 answers

insertBulk all transactions in insertBulk .

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 default transactions, as the default behavior simply calls insert :

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

+21
source

inserting into a transaction significantly improves speed, because there is only one record in the actall database:

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

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

By doing db.setLockingEnabled(false) , I think it gave a 1% improvement, but then you should also make sure that no other thread writes to db. Removing redundant indexes can also lead to a slight increase if the table is huge.

+3
source

All Articles