|
一、不合理的索引设计
例:表record有620000行,试看在不同的索引下,下面几个 SQL的运行情况:
1.在date上建有一非个群集索引 select count(*) from record where date >
′19991201′ and date < ′19991214′and amount >
2000 (25秒)
select date,sum(amount) from record group by date
(55秒)
select count(*) from record where date >
′19990901′ and place in (′BJ′,′SH′) (27秒)
分析:
date上有大量的重复值,在非群集索引下,数据在物理上随机存放在数据页上,在范围查找时,必须执行一次表扫描才能找到这一范围内的全部行。
2.在date上的一个群集索引
select count(*) from record where date >
′19991201′ and date < ′19991214′ and amount >
2000 (14秒)
select date,sum(amount) from record group by date
(28秒)
select count(*) from record where date >
′19990901′ and place in (′BJ′,′SH′)(14秒)
分析:
在群集索引下,数据在物理上按顺序在数据页上,重复值也排列在一起,因而在范围查找时,可以先找到这个范围的起末点,且只在这个范围内扫描数据页,避免了大范围扫描,提高了查询速度。
3.在place,date,amount上的组合索引
select count(*) from record where date >
′19991201′ and date < ′19991214′ and amount >
2000 (26秒)
select date,sum(amount) from record group by date
(27秒)
select count(*) from record where date >
′19990901′ and place in (′BJ, ′SH′)(< 1秒)
分析:
这是一个不很合理的组合索引,因为它的前导列是place,第一和第二条SQL没有引用place,因此也没有利用上索引;第三个SQL使用了place,且引用的所有列都包含在组合索引中,形成了索引覆盖,所以它的速度是非常快的。
| 电脑网络超级技巧!让一切皆有可 | 01-31 |
| 电脑网络超级技巧!让一切皆有可 | 01-31 |
| XP急速启动最新技巧 | 09-04 |
| 让老机器也跑Vista系统 | 09-01 |
| Vista看高清电影声音太小 看我轻 | 09-01 |
| 简单几招让您的Windows系统不再假 | 08-27 |
| 收录大家解决系统内存不能为 Rea | 08-06 |
| 解决删除文件或文件夹出错故障 | 07-19 |
| 解决优化后XP不能自动更新的问题 | 07-16 |
| 虚拟内存介绍和及其详细和优化设 | 07-09 |
| PQ分区出错,用Ghost来补救的绝妙 | 07-02 |
| 复制Windows信息框文字到剪切板 | 06-18 |