Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any faster way to iterate through rows from Sqlite query?

I am using the following lines for looping on rows of a Sqlite query.

this.open(); // opening db
Cursor cursor = db.rawQuery(strQuery, null);

cursor.moveToFirst();
do {    

    // do something    

} while (cursor.moveToNext());

cursor.close();

When the number of rows is about 15000 it takes long time. It takes about 4 seconds for empty while block and about 6 second for while block that has some codes. It shows that iterating on rows in this way is time consuming.

Is there any faster way for looping on rows in android and Sqlite?

Thanks,

like image 316
Bobs Avatar asked Sep 02 '12 06:09

Bobs


People also ask

Does SQLite optimize queries?

7.2.SQLite provides the ability for advanced programmers to exercise control over the query plan chosen by the optimizer.

Which method is used to run the SQLite query?

In FULL sync mode the SQLite database engine will use the xSync method of the VFS to ensure that all content is safely written to the disk surface prior to continuing.


1 Answers

Optimizing what you do inside the loop is the only way of improving the speed of the whole operation. For instance if you're doing getColumnIndex calls on every iteration, you will be loosing precious time. Do it once, store the value.

Use traceView to locate where you're loosing time and improve it there. Sadly I can't give a concrete answer, since I don't know what you're doing inside the loop.


Traceview Debugging
like image 145
Juan Cortés Avatar answered Sep 20 '22 13:09

Juan Cortés