sql server 海量数据速度提升:SQL优化-索引(9)
来源:互联网 发布:计算机编程培训学校 编辑:程序博客网 时间:2024/04/29 16:10
泛东协同,赢在执行!
8、union并不绝对比or的执行效率高
我们前面已经谈到了在where子句中使用or会引起全表扫描,一般的,我所见过的资料都是推荐这里用union来代替or。事实证明,这种说法对于大部分都是适用的。
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi='2004-9-16' or gid>9990000
用时:68秒。扫描计数 1,逻辑读 404008 次,物理读 283 次,预读 392163 次。
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi='2004-9-16'
union
select gid,fariqi,neibuyonghu,reader,title from Tgongwen where gid>9990000
用时:9秒。扫描计数 8,逻辑读 67489 次,物理读 216 次,预读 7499 次。
看来,用union在通常情况下比用or的效率要高的多。
但经过试验,笔者发现如果or两边的查询列是一样的话,那么用union则反倒和用or的执行速度差很多,虽然这里union扫描的是索引,而or扫描的是全表。
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi='2004-9-16' or fariqi='2004-2-5'
用时:6423毫秒。扫描计数 2,逻辑读 14726 次,物理读 1 次,预读 7176 次。
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi='2004-9-16'
union
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi='2004-2-5'
用时:11640毫秒。扫描计数 8,逻辑读 14806 次,物理读 108 次,预读 1144 次。
9、字段提取要按照“需多少、提多少”的原则,避免“select *”
我们来做一个试验:
select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc
用时:4673毫秒
select top 10000 gid,fariqi,title from tgongwen order by gid desc
用时:1376毫秒
select top 10000 gid,fariqi from tgongwen order by gid desc
用时:80毫秒
由此看来,我们每少提取一个字段,数据的提取速度就会有相应的提升。提升的速度还要看您舍弃的字段的大小来判断。
- sql server 海量数据速度提升:SQL优化-索引(9)
- sql server 海量数据速度提升:SQL优化-索引(1)
- sql server 海量数据速度提升:SQL优化-索引(2)
- sql server 海量数据速度提升:SQL优化-索引(3)
- sql server 海量数据速度提升:SQL优化-索引(4)
- sql server 海量数据速度提升:SQL优化-索引(5)
- sql server 海量数据速度提升:SQL优化-索引(6)
- sql server 海量数据速度提升:SQL优化-索引(7)
- sql server 海量数据速度提升:SQL优化-索引(8)
- sql server 海量数据速度提升:SQL优化-索引(10)
- sql server 海量数据速度提升:SQL优化-索引(11)
- sql server 海量数据速度提升:SQL优化-索引(12)
- sql server 海量数据速度提升:SQL优化-索引(13)
- sql server 海量数据速度提升:SQL优化-索引(14)
- sql server 海量数据速度提升:SQL优化-索引(15)
- 整理索引碎片,提升SQL Server速度
- 整理索引碎片,提升SQL Server速度
- 整理索引碎片,提升SQL Server速度
- priority_queue用法小结
- sql server 海量数据速度提升:SQL优化-索引(6)
- sql server 海量数据速度提升:SQL优化-索引(7)
- 一步一步
- sql server 海量数据速度提升:SQL优化-索引(8)
- sql server 海量数据速度提升:SQL优化-索引(9)
- 正版金山毒霸下载多少钱
- 图的割点、桥与双连通分支
- 【WM】谈Phone Canvas函数为何不会返回错误值RPC_E_WRONG_THREAD
- sql server 海量数据速度提升:SQL优化-索引(10)
- sql server 海量数据速度提升:SQL优化-索引(11)
- sql server 海量数据速度提升:SQL优化-索引(12)
- STL map常用操作简介
- sql server 海量数据速度提升:SQL优化-索引(13)