views:

424

answers:

4

I am having problem with performance of SQLite database in Android application in Samsung Galaxy GT-I9000 device. The database contains 10 tables with 3 - 8 columns each.

As soon as a database file gains about 400 KB update/instert queries need much more time to execute - 2 seconds! In comparision to other devices (HTC Magic, HTC Desire) update/insert queries take about 40 msec !

The database file is located in application data directory. Moving database file to sd card increases the speed significantly.

Periodic ANALYZE and VACUUM commands do not affect the speed. As well as PRAGMA cache_size command.

Do you have any ideas how to improve the performance?

Any idea is welcome except for keeping the database in sd card or splitting the database into many files containing one table each.

+1  A: 

Bulk insert commands should be done within a transaction:

BEGIN TRANSACTION;
// INSERTS
COMMIT;
MPelletier
+3  A: 

Use transactions and parameterized queries. Sqlite has to parse every sql statement if you don't use parameterized queries. All this unecessary parsing makes a simple insert statement 2 to 3 times slower. See here: http://stackoverflow.com/questions/904796/926251#926251

Using transactions is also very important because Sqlite can do an insert very fast but it commits are slow so you have to bundle you inserts/updates and deletes into one big transaction.

TTT
Good point. Timings with transactions are as follows:(number of queries in a transaction - average time of a transaction in seconds)40 - 3.5 s, 20 - 3.0 s, 10 - 2.8 s, 5 - 2.5 s,
radek-k
A: 

This helped a lot and and motivated me to search even more. And I found a solution which gives a huge boost:

PRAGMA synchronous=OFF

INSERT/UPDATE Queries run 4 times faster, now.

It has however a small disadvantage.

More details here:
http://web.utk.edu/~jplyon/sqlite/SQLite_optimization_FAQ.html#pragmas

radek-k
A: 

Access to the internal SD card is very slow on the Samsung Galaxy S. There are a variety of inofficial lag fixes available, it seems the RFS file system from Samsung is responsible for the delays.

Hopefully Samsung will release an Update that fixes this problem, until then your best bet is to avoid I/O if possible.

Fabian
Also mentioned here: http://kollerie.wordpress.com/2010/08/03/samsung-galaxy-s-peculiarities-02-lags/
radek-k