How can I analyse a Sqlite query execution? How can I analyse a Sqlite query execution? sql sql

How can I analyse a Sqlite query execution?


As outis said:

EXPLAIN QUERY PLAN SELECT * FROM FOO

Does the trick with a more readable output, if like me you're simply using it to ensure you're hitting your indexes (indices?)


I know of no pretty graphical tools, but all of the information you seek is available from the EXPLAIN keyword.

Consider this database:

sqlite> create table users (name, email);sqlite> create index user_names on users (name);

A query predicated on email will not use an index:

sqlite> explain select * from users where email='foo';
addropcodep1p2p3p4p5comment
0Trace00000
1String8010foo00
2Goto013000
3OpenRead020200
4Rewind011000
5Column01200
6Ne1102collseq(BINARY)6a
7Column00400
8Column01500
9ResultRow42000
10Next05001
11Close00000
12Halt00000
13Transaction00000
14VerifyCookie05000
15TableLock020users00
16Goto03000

Whereas a query predicated on name will use the user_names index:

sqlite> explain select * from users where name='foo';
addropcodep1p2p3p4p5comment
0Trace00000
1String8010foo00
2Goto018000
3OpenRead020200
4OpenRead130keyinfo(1,BINARY)00
5IsNull115000
6Affinity110bb00
7SeekGe1151100
8IdxGE1151101
9IdxRowid12000
10Seek02000
11Column10300
12Column01400
13ResultRow32000
14Next18000
15Close00000
16Close10000
17Halt00000
18Transaction00000
19VerifyCookie05000
20TableLock020users00
21Goto03000

Using EXPLAIN does require coming to grips with SQLite's virtual machine, VDBE:

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

But this is not as hard as it looks, and gives you the complete story about your query.