如何分析Sqlite查询执行?

Phi*_*ent 24 sql sqlite query-optimization sql-execution-plan

我有一个Sqlite数据库,我想检查索引是否正确.MS SQL Analyzer非常适合分解查询执行和利用索引.

Sqlite有类似的工具吗?

Chr*_*ris 20

正如外面所说:

EXPLAIN QUERY PLAN SELECT * FROM FOO
Run Code Online (Sandbox Code Playgroud)

具有更易读的输出的技巧,如果像我一样,你只是用它来确保你的指数(指数?)

  • 这里对该命令的输出有一个很好的解释:https://www.sqlite.org/eqp.html (2认同)

Dav*_*haw 15

我知道没有漂亮的图形工具,但您所寻求的所有信息都可以从EXPLAIN关键字中获得.

考虑这个数据库:

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

基于查询的查询email将不使用索引:

sqlite> explain select * from users where email='foo';
0|Trace|0|0|0||00|
1|String8|0|1|0|foo|00|
2|Goto|0|13|0||00|
3|OpenRead|0|2|0|2|00|
4|Rewind|0|11|0||00|
5|Column|0|1|2||00|
6|Ne|1|10|2|collseq(BINARY)|6a|
7|Column|0|0|4||00|
8|Column|0|1|5||00|
9|ResultRow|4|2|0||00|
10|Next|0|5|0||01|
11|Close|0|0|0||00|
12|Halt|0|0|0||00|
13|Transaction|0|0|0||00|
14|VerifyCookie|0|5|0||00|
15|TableLock|0|2|0|users|00|
16|Goto|0|3|0||00|

而基于名称的查询将使用user_names索引:

sqlite> explain select * from users where name='foo';
0|Trace|0|0|0||00|
1|String8|0|1|0|foo|00|
2|Goto|0|18|0||00|
3|OpenRead|0|2|0|2|00|
4|OpenRead|1|3|0|keyinfo(1,BINARY)|00|
5|IsNull|1|15|0||00|
6|Affinity|1|1|0|bb|00|
7|SeekGe|1|15|1|1|00|
8|IdxGE|1|15|1|1|01|
9|IdxRowid|1|2|0||00|
10|Seek|0|2|0||00|
11|Column|1|0|3||00|
12|Column|0|1|4||00|
13|ResultRow|3|2|0||00|
14|Next|1|8|0||00|
15|Close|0|0|0||00|
16|Close|1|0|0||00|
17|Halt|0|0|0||00|
18|Transaction|0|0|0||00|
19|VerifyCookie|0|5|0||00|
20|TableLock|0|2|0|users|00|
21|Goto|0|3|0||00|

使用EXPLAIN确实需要掌握SQLite的虚拟机VDBE:

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

但这并不像它看起来那么难,并且为您提供有关查询的完整故事.

  • `EXPLAIN QUERY PLAN`将在没有VM指令的情况下提供有关索引使用的信息. (32认同)