How to understand if created index used when select query run?
Specific example for my task:
Index:
events (page,notuniqueid,type,timestamp);
SQL statement:
SELECT *
FROM events
WHERE type = 'comment' AND TIMESTAMP > $time AND PAGE = '$page'
LIMIT 1
Will index be used at this query?
To find out how a query is executed, run it with EXPLAIN QUERY PLAN:
> EXPLAIN QUERY PLAN SELECT * from events WHERE type='comment' AND TIMESTAMP > $time AND PAGE = '$page' LIMIT 1
0|0|0|SEARCH TABLE events USING INDEX MyLittleIndex (page=?)
In this case, only the first column of the index can be used because there is no lookup on the notuniqueuid column.
A better index (for this query) would be on (page,comment,timestamp).
(See The SQLite Query Planner.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With