Databases in Android
I need to improve the speed of this query,it takes too long(16992ms) and the SQLite Admin makes it much faster
public Cursor getContacts(String search)
{
Cursor c;
String[]columns = {Constants.USER_NAME,Constants.P开发者_C百科HONE_NUMBER};
String Limit = "0,20";
String query = search != null ? Constants.USER_NAME + " LIKE '" + search + "%' " : "";
c = myDataBase.query(Constants.TABLE_NAME,columns, query, null, null, null, null, Limit);
return c;
}
Depending on the size of your Database, a LIKE
-statement takes some time.
Form your method-name I guess you're implementing a search for your application? There is a nice tutorial on how to do that: Link. This also covers how you can speed up the search.
LIKE
statements are very intensive in SQLite in large databases, especially with large strings.
You can try a quick trick and turn pragma off to see if it speeds anything up. Do not do this if your database is constantly updating, as it may interfere with atomicity. If its a static DB, this is excellent. Speeds up my mass queries and inserts by at least double, but they weren't using like
. I'd be interested to hear how this effects your speed.
rawQuery('PRAGMA synchronous = OFF');
精彩评论