有志者 事竟成

天行健,君子以自强不息,地势坤,君子以厚德载物。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

sqlite 索引优化方法

Posted on 2011-05-29 11:13  水寒  阅读(998)  评论(0编辑  收藏  举报

本文摘自:http://www.cnblogs.com/analyzer/articles/1400122.html

速度测试结果:

1) select count(*) from t1,t3 where t1.word2=t3.word2;
很慢(t3.word2上没有索引)
2) select count(*) from t3,t1 where t1.word2=t3.word2;
很慢(t1.word2上没有独立索引)
3) select count(*) from t1,t2 where t1.word2=t2.word2;
很快(t2.word2上有索引)
4) select count(*) from t2,t1 where t1.word2=t2.word2;
很慢(t1.word2上没有独立索引)
5) select count(*) from t1,t2 where t1.num=t2.num;
很快(t2.num上有索引)
6) select count(*) from t2,t1 where t1.num=t2.num;
很快(t1的复合索引中,第一个列是num)
7) select count(*) from t1,t3 where t1.num=t3.num;
很慢(t3.num上没有索引)
8) select count(*) from t3,t1 where t1.num=t3.num;
很快(t1的复合索引中,第一个列是num)

 

结论:

1、索引可以大大加快查询速度

2、当有交叉查询时,from a,b两个表,取决于b表是否有索引

3、当b上的索引不是独立索引时,查询速度取决于非独立索引的第一个字段