索引对提高查询速度的影响

比较数据库的一种新的思路:

http://topic.csdn.net/u/20090910/16/f79371f3-f72c-461f-ae01-de0c8a3ffa5d.html

 

采用attach database 的方法

 

引用于这个博客(谢谢这位专家朋友):

http://blog.csdn.net/chu_qun/article/details/3719532

 

在SQLite中使用索引优化查询速度

分类: databases 456人阅读 评论(0) 收藏 举报
在进行多个表联合查询的时候,使用索引可以显著的提高速度,刚才用SQLite做了一下测试。

建立三个表:
create table t1
(id integer primary key,
num integer not null,
word1 text not null,
word2 text not null);
create table t2
(id integer primary key,
num integer not null,
word1 text not null,
word2 text not null);
create table t3
(id integer primary key,
num integer not null,
word1 text not null,
word2 text not null);


建立若干索引:
t1表:在num,word1,word2上有复合索引
t2表:在num,word1,word2上各有一个索引
t3表:在word1上有一个索引
create index idxT1 on t1(num,word1,word2);
create index idxT2Num on t2(num);
create index idxT2Word1 on t2(word1);
create index idxT2Word2 on t2(word2);
create index idxT3Word1 on t2(word1);


向三个表中各插入10000行数据,其中num项为随机数字,word1和word2中是英文单词,三个表中对应的num,word1和word2列中都包含有部分相同值,但是它们在表中出现的顺序不同。

速度测试结果:
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)


结论: 在from子句后面的两个表中,如果第2个表中要查询的列里面带有索引,这个查询的速度就快,反之就慢。
比如第三个查询,from后面的第2个表是 t2,t2在word2上有索引,所以这个查询就快,当输入SQL命令并回车后,查询结果就立即显示出来了
,但是如果使用第4个查询命令(即把t1和t2 的位置互换),查询起来却用了1分零6秒。

可见索引的建立对于提高数据库查询的速度是非常重要的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值