Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sqlite determine if index is used

  1. How to understand if created index used when select query run?

  2. 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?

like image 962
abrahab Avatar asked Dec 11 '25 01:12

abrahab


1 Answers

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.)

like image 189
CL. Avatar answered Dec 13 '25 15:12

CL.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!