artem
artem

Reputation: 16777

SQLite Cursor freezes for about 10 seconds at first call

I have 10 mb database and this code:

cursor = mDatabase.query(CitySQLiteHelper.TABLE_CITIES, ALL_COLUMNS, null, null, null, null, "name DESC", "20");
if (cursor.getCount() > 0) {
//
}

Execution freezes for about 10 seconds at the if statement. I tried getCount and moveToFirst - it freezes in both cases. Query and all the calls to the cursor (getString() etc) after the first call work fine. Why?

Update: Schema

sqlite> .schema cities
CREATE TABLE cities (region text, name text, latitude float, longitude float);
sqlite> select count(*) from cities;
130070

Upvotes: 1

Views: 263

Answers (2)

artem
artem

Reputation: 16777

As suggested in a comment, I created unique index and this helped.

Upvotes: 0

Graham Borland
Graham Borland

Reputation: 60681

You are simply trying to read many megabytes of data when you first read from the DB. Subsequent database access will benefit from caching. If your database is stored on the SD card, try moving it to internal storage. Otherwise, try doing an initial read from the database on app startup (in a background thread) to "prime" it for later speedy access.

Upvotes: 3

Related Questions