0

I have an SQLite db with about 400 000 entries. To query the db I am using the following method:

public double lookUpBigramFrequency(String bigram) throws SQLException {

        SQLiteDatabase db = dbh.getReadableDatabase();
        double frequency = 0;
        bigram = bigram.toLowerCase();

        String select = "SELECT frequency FROM bigrams WHERE bigram = '"
                + bigram + "'";

        Cursor mCursor = db.rawQuery(select, null);
        if (mCursor != null) {

            if (mCursor.moveToFirst()) {
                frequency = Double.parseDouble(mCursor.getString(0));
            } else {
                frequency = 0;
            }
        }

        return frequency;


    }

but it takes about 0.5 sec to retrieve a single entry and having few queries, it builds up and the method is executing for 10 secs. How to speeed it up?

0

2 Answers 2

5

Firstly, use an INDEX

http://www.sqlite.org/lang_createindex.html

in your case that will be something like:

CREATE INDEX idx_bigram ON bigrams (bigram)

Secondly, use '?' instead of literal query. It helps sqlite for caching requests:

String select = "SELECT frequency FROM bigrams WHERE bigram = ?";
Cursor mCursor = db.rawQuery(select, new String[]{ bigram });

Thirdly, I trust query is more efficient than rawQuery:

mCursor = dq.query("bigrams", new String[] { "frequency" }, "bigram = ?",
          new String[]{ bigram }, null, null, null, null);

Fourthly, you can query several values at once (not compatible with point 2):

SELECT frequency FROM bigrams WHERE bigrams IN ('1', '2', '3')

Fifthly, you don't need to open your database every time. You should consider leaving it open.

Edit

After seeing this question IN clause and placeholders it appears you can combine 2 and 4 after all (not sure it is useful, though)

Sign up to request clarification or add additional context in comments.

5 Comments

#1 and #5 are the most significant in terms of performance; #2 is important to avoid SQL injections, and simply looks better
+1 for the non-literal querys. -1 for assuming that query() would be faster then rawQuery().
it improved the speed by about 10-20 ms but that's still not enough
Well actually I messed up the indexes at first. Did it again and it helped. Thanks
Yes, using selection args works a bit faster. every millisecond count
-1

Always use transaction mechanism when you want to do lots of database operations

public static void doLotDBOperations() {
    try {
        // Code to Open Database

        // Start transaction
        sqlDb.beginTransaction();

        // Code to Execute all queries

        sqlDb.setTransactionSuccessful();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // End all transaction
        sqlDb.endTransaction();

        // Code to Close Database
    }
}

1 Comment

which would be relevant for write operations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.