Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android SQLite database: slow insertion

I need to parse a fairly large XML file (varying between about a hundred kilobytes and several hundred kilobytes), which I'm doing using Xml#parse(String, ContentHandler). I'm currently testing this with a 152KB file.

During parsing, I also insert the data in an SQLite database using calls similar to the following: getWritableDatabase().insert(TABLE_NAME, "_id", values). All of this together takes about 80 seconds for the 152KB test file (which comes down to inserting roughly 200 rows).

When I comment out all insert statements (but leave in everything else, such as creating ContentValues etc.) the same file takes only 23 seconds.

Is it normal for the database operations to have such a big overhead? Can I do anything about that?

like image 872
benvd Avatar asked Aug 17 '10 10:08

benvd


1 Answers

You should do batch inserts.

Pseudocode:

db.beginTransaction(); for (entry : listOfEntries) {     db.insert(entry); } db.setTransactionSuccessful(); db.endTransaction(); 

That increased the speed of inserts in my apps extremely.

Update:
@Yuku provided a very interesting blog post: Android using inserthelper for faster insertions into sqlite database

like image 162
WarrenFaith Avatar answered Sep 26 '22 09:09

WarrenFaith