mercredi 7 janvier 2015

Android SQLite query super slow

I have a database with one table and about 350.000 rows in there. When I try to query them it's super slow on my Nexus4. The cursor.moveToFirst() method call takes about 500ms. If I run the exact same query on the same database in my terminal using sqlite3 it takes about 10ms. So I suppose that my Cursor is not using my index...



String table = "table";

String[] columns = {
"col1",
"col2"
};

String predicate = "col1 like '?' and col2 = ?";

String[] values = {
value1+"%%",
value2
};

String orderBy = "col3 ASC";

String limit = "5";

Cursor c = this.mDatabase.query(table, columns, predicate, values, null, null, orderBy, limit);

if (c != null) {
long start = System.currentTimeMillis();
c.moveToFirst();
long duration = System.currentTimeMillis() - start;
// The duration here is super slow
....
}


The database has an index on col1 and col2. And like I said... when executing the sql in the command line it's super fast... about 10ms vs 500 on my phone...


Any ideas on how I can improve the speed on that query? Is seems as if the index isn't used?!?


Thanks


Aucun commentaire:

Enregistrer un commentaire