Android SQLite database: slow insertion
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
Android Sqlite Performance
Use SQLite transaction
for speed up
Use BEGIN TRANSACTION & END TRANSACTION for SQLite Optimization
Each SQL statement is enclosed in a new transaction block by SQLite runtime, by default. Sowhen you perform a basic DB operation such as INSERT, a transaction block will be created and wrapped around it.
Letting SQLite runtime manage the transaction for you is advisable only if your routine performs only one DB operation on a data set. However, if you are doing numerous DB operations (say INSERT inside for loop), this becomes very expensive, since it requires reopening, writing to, and closing the journal file for each statement.
You may refer
Android SQLite database: slow insertion
http://www.androidcode.ninja/android-sqlite-transaction-tutorial/
http://www.techrepublic.com/blog/software-engineer/turbocharge-your-sqlite-inserts-on-android/
http://www.android-app-market.com/sqlite-optimization-in-android-programming-sqlite-optimization-in-android-apps.html
Android, SQLite: Inserting data in while clause is very slow
I would suggest to do batch inserts as explained in the following post:
Android SQLite database: slow insertion
It gives you multiple examples. Personally I have noticed that SQLite inserts are pretty slow, and for 3000 inserts its normal to take a long time unless you do batch inserts.
So idea is the same as the other poster posted, i.e. do it all in one transaction:
db.beginTransaction();
for (entry : listOfEntries) {
db.insert(entry);
}
db.setTransactionSuccessful();
db.endTransaction();
Sqlite batch INSERT is slow
There isn't much you can to speed up this code; handling 200,000 rows on a mobile device always is slow. (Just doing the flash I/O for that amount of data will take lots of time.)
If possible, ship the complete database with your app.
If you need to get the data from elsewhere, try to replace the JSON file with a database file, which can be ATTACHed to your main database and just copied over:
INSERT INTO main.MyTable SELECT * FROM downloadDB.MyTable;
executing INSERT commands in a row very slow
You need to put the list of INSERT statements into a transaction:
BEGIN TRANSACTION;
CREATE TABLE ...;
INSERT INTO ...;
INSERT INTO ...;
...
COMMIT;
This will dramatically reduce the execution time.
Related Topics
Example: Communication Between Activity and Service Using Messaging
How to Define Dimens.Xml For Every Different Screen Size in Android
How to Get Extra Data from Intent on Android
How to Set the Part of the Text View Is Clickable
Get/Pick an Image from Android'S Built-In Gallery App Programmatically
Open Another Application from Your Own (Intent)
How Do We Use Runonuithread in Android
Difference Between Gravity and Layout_Gravity in Android
How to Correctly Save Instance State of Fragments in Back Stack
Sending and Parsing Json Objects in Android
How to Send Emails from My Android Application
Making Textview Scrollable on Android
Why 'Pageradapter::Notifydatasetchanged' Is Not Updating the View
How to Prevent Custom Views from Losing State Across Screen Orientation Changes
How to Get a List of Installed Android Applications and Pick One to Run
How to Remove All Debug Logging Calls Before Building the Release Version of an Android App