如何分析一个Sqlite查询执行?

前端之家收集整理的这篇文章主要介绍了如何分析一个Sqlite查询执行?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个sqlite数据库,我想检查索引是否正确。 MS sql Analyzer很好地打破了查询执行和利用的索引。

是否有类似的sqlite工具?

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

考虑这个数据库

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

基于电子邮件查询不会使用索引:

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

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

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

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

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

但这并不像看起来那么难,而是给你关于你的查询的完整的故事。

猜你在找的Sqlite相关文章