sql server 海量数据速度提升:SQL优化-索引(9)
2009-08-30 11:30
357 查看
泛东协同,赢在执行!
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毫秒
由此看来,我们每少提取一个字段,数据的提取速度就会有相应的提升。提升的速度还要看您舍弃的字段的大小来判断。
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优化-索引(4)
- sql server 海量数据速度提升:SQL优化-索引(13)
- sql server 海量数据速度提升:SQL优化-索引(7) 【转】
- sql server 海量数据速度提升:SQL优化-索引(14) 【转】
- sql server 海量数据速度提升:SQL优化-索引(15) 【转】
- sql server 海量数据速度提升:SQL优化-索引(6)
- sql server 海量数据速度提升:SQL优化-索引(15)
- sql server 海量数据速度提升:SQL优化-索引(1) 【转】
- sql server 海量数据速度提升:SQL优化-索引(8) 【转】
- sql server 海量数据速度提升:SQL优化-索引(7)
- sql server 海量数据速度提升:SQL优化-索引(2) 【转】
- sql server 海量数据速度提升:SQL优化-索引(9) 【转】
- sql server 海量数据速度提升:SQL优化-索引(1)
- sql server 海量数据速度提升:SQL优化-索引(8)
- sql server 海量数据速度提升:SQL优化-索引(3) 【转】
- sql server 海量数据速度提升:SQL优化-索引(10) 【转】
- sql server 海量数据速度提升:SQL优化-索引(2)
- sql server 海量数据速度提升:SQL优化-索引(10)
- sql server 海量数据速度提升:SQL优化-索引(4) 【转】
- sql server 海量数据速度提升:SQL优化-索引(11) 【转】