SQLite Tutorial on SQLite EXPLAIN

sqlite statement can be preceded by the keyword "explain" or by the phrase "explain query plan" used for describing the details of a table.

either modification causes the sqlite statement to behave as a query and to return information about how the sqlite statement would have operated if the explain keyword or phrase had been omitted.

  • the output from explain and explain query plan is intended for interactive analysis and troubleshooting only.

  • the details of the output format are subject to change from one release of sqlite to the next.

  • applications should not use explain or explain query plan since their exact behavior is variable and only partially documented.

syntax

syntax for explain is as follows −

explain [sqlite query]

syntax for explain query plan is as follows −

explain  query plan [sqlite query]

example

consider company table with the following records −

id          name        age         address     salary
----------  ----------  ----------  ----------  ----------
1           paul        32          california  20000.0
2           allen       25          texas       15000.0
3           teddy       23          norway      20000.0
4           mark        25          rich-mond   65000.0
5           david       27          texas       85000.0
6           kim         22          south-hall  45000.0
7           james       24          houston     10000.0

now, let us check the following sub-query with select statement −

sqlite> explain select * from company where salary &gt= 20000;

this will produce the following result.

addr        opcode      p1          p2          p3
----------  ----------  ----------  ----------  ----------
0           goto        0           19
1           integer     0           0
2           openread    0           8
3           setnumcolu  0           5
4           rewind      0           17
5           column      0           4
6           realaffini  0           0
7           integer     20000       0
8           lt          357         16          collseq(bi
9           rowid       0           0
10          column      0           1
11          column      0           2
12          column      0           3
13          column      0           4
14          realaffini  0           0
15          callback    5           0
16          next        0           5
17          close       0           0
18          halt        0           0
19          transactio  0           0
20          verifycook  0           38
21          goto        0           1
22          noop        0           0

now, let us check the following explain query plan with select statement −

sqlite> explain query plan select * from company where salary &gt= 20000;

order       from        detail
----------  ----------  -------------
0           0           table company