Alfa
Alfa

Reputation: 619

Android improve SQL processing speed

I'm processing 31000 Insert SQL statements reading from a file and it takes more than 2 minutes to complete.. need some advice in improving performance..

Here is the code:

while((line=reader.readLine())!=null){

            dbs.execSQL(line);
            k=(int)(i*0.0032);
            if(k%10==0){fdb.publishProgress(k);}
            i++;
}

Upvotes: 0

Views: 83

Answers (3)

M-Wajeeh
M-Wajeeh

Reputation: 17304

Do a batch insert using technique mentioned here Is it possible to insert multiple rows at a time in an SQLite database?

Please note that you can only insert 500 rows at a time in a query using this technique. So insert 31000 records in queries containing 500 records per query. And you can publish progress after every query i.e. after every 500 inserts.

Upvotes: 1

Biraj Zalavadia
Biraj Zalavadia

Reputation: 28484

DO this trick will work;

dbs.beginTransaction();
while((line=reader.readLine())!=null){

            dbs.execSQL(line);
            k=(int)(i*0.0032);
            if(k%10==0){fdb.publishProgress(k);}
            i++;
}
dbs.setTransactionSuccessful();
dbs.endTransaction();

Upvotes: 0

Simon Dorociak
Simon Dorociak

Reputation: 33515

m processing 31000 Insert SQL statements reading from a file and it takes more than 2 minutes to complete.. need some advice in improving performance..

All what you are able to do is to use BufferedReader if you actually not using it for reading from file.

And concerning to SQLite if you are processing hundreds and thousands of insert, update or delete statements a usage of transaction will rapidly increase speed performance and also it has other benefits like security and there is not danger of losing database integrity.

Upvotes: 1

Related Questions