在SQLite中使用索引优化查询速度
来源:互联网 发布:js replace 正则替换 编辑:程序博客网 时间:2024/05/21 03:25
本文来自 sqlitechina.org 作者:工友
在进行多个表联合查询的时候,使用索引可以显著的提高速度:
建立三个表:
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);
(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 t3(word1);
create index idxT2Num on t2(num);
create index idxT2Word1 on t2(word1);
create index idxT2Word2 on t2(word2);
create index idxT3Word1 on t3(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)
很慢(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秒。
可见索引的建立对于提高数据库查询的速度是非常重要的。
更多关于SQLite查询优化的知识可以参考《Chris Newman》写的《SQLite》一书的第四章:《Query Optimization》
- 在SQLite中使用索引优化查询速度
- 在SQLite中使用索引优化查询速度
- 在SQLite中使用索引优化查询速度(转)
- 在SQLite中使用索引优化查询速度
- 在SQLite中使用索引优化查询速度
- Sqlite优化记录:使用全文索引加快检索速度-转
- Sqlite优化记录:使用全文索引加快检索速度-转
- mysql在数据库表中,使用索引可以大大提高查询速度
- SQLite对字符串字段建立索引后查询速度无优化的问题
- 如何在数据库中创建索引,提高查询速度
- 如何优化sqlite的查询速度
- SQLite查询优化,提高获取数据速度
- 使用索引提高查询速度
- 使用索引提高查询速度
- 数据库查询速度优化 1 建立索引
- 数据库的索引大幅度优化查询速度
- postgresql 函数索引优化查询速度
- 通过建立索引优化MySQL查询速度
- ASP.NET报错:当使用VS2005打开已有项目时常会出现“此安装不支持该项目类型”。
- ServletConfig与ServletContext的关系及使用
- 嵌入式程序员必须知道的16个问题(C语言方面)
- ASP.NET报错:未能加载类型“_Default”
- (NOT CLEAR) Question 14: What is the correct declaration for a file stream insertion operator for a class my_stuff::my_class a
- 在SQLite中使用索引优化查询速度
- 多线程服务器的适用场合
- Access数据库打包发布
- CURL 模拟登录
- Linux十五年病毒简史
- Mysql select默认不区分大小写
- Question 15: Which of the following statements describe the results of executing the code snippet below in C++?
- js实现页面跳转的几种方式普通方式
- session保存登录