I want to query my sqlite db for all level values โโgreater than 20, limit the result to 100 and sort by rowid.
When ordering rowid, the query is much slower. The database contains ~ 3 million records and the maximum level value is 50. An index is created for the level.
This statement takes ~ 20ms:
SELECT * FROM log WHERE level > 20 LIMIT 100
This statement takes ~ 100 ms:
SELECT * FROM log WHERE level > 20 ORDER BY rowid LIMIT 100
This statement takes ~ 1000 ms (there are no lines with a level above 50):
SELECT * FROM log WHERE level > 50 ORDER BY rowid LIMIT 100
Is there a way to optimize this for a faster ORDER BY query?
This is the index used:
CREATE INDEX level_idx ON table (level)
source share