欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SQL Server 索引結(jié)構(gòu)及其使用(二) 改善SQL語句第2/3頁

 更新時(shí)間:2009年04月09日 00:38:23   作者:  
很多人不知道SQL語句在SQL SERVER中是如何執(zhí)行的,他們擔(dān)心自己所寫的SQL語句會(huì)被SQL SERVER誤解。

5、盡量少用NOT

6、exists 和 in 的執(zhí)行效率是一樣的
  很多資料上都顯示說,exists要比in的執(zhí)行效率要高,同時(shí)應(yīng)盡可能的用not exists來代替not in。但事實(shí)上,我試驗(yàn)了一下,發(fā)現(xiàn)二者無論是前面帶不帶not,二者之間的執(zhí)行效率都是一樣的。因?yàn)樯婕白硬樵儯覀冊(cè)囼?yàn)這次用SQL SERVER自帶的pubs數(shù)據(jù)庫。運(yùn)行前我們可以把SQL SERVER的statistics I/O狀態(tài)打開:
(1)select title,price from titles where title_id in (select title_id from sales where qty>30)
該句的執(zhí)行結(jié)果為:
表 ''sales''。掃描計(jì)數(shù) 18,邏輯讀 56 次,物理讀 0 次,預(yù)讀 0 次。
表 ''titles''。掃描計(jì)數(shù) 1,邏輯讀 2 次,物理讀 0 次,預(yù)讀 0 次。

(2)select title,price from titles
where exists (select * from sales
where sales.title_id=titles.title_id and qty>30)
第二句的執(zhí)行結(jié)果為:
表 ''sales''。掃描計(jì)數(shù) 18,邏輯讀 56 次,物理讀 0 次,預(yù)讀 0 次。
表 ''titles''。掃描計(jì)數(shù) 1,邏輯讀 2 次,物理讀 0 次,預(yù)讀 0 次。

我們從此可以看到用exists和用in的執(zhí)行效率是一樣的。

7、用函數(shù)charindex()和前面加通配符%的LIKE執(zhí)行效率一樣
  前面,我們談到,如果在LIKE前面加上通配符%,那么將會(huì)引起全表掃描,所以其執(zhí)行效率是低下的。但有的資料介紹說,用函數(shù)charindex()來代替LIKE速度會(huì)有大的提升,經(jīng)我試驗(yàn),發(fā)現(xiàn)這種說明也是錯(cuò)誤的:
select gid,title,fariqi,reader from tgongwen
where charindex(''刑偵支隊(duì)'',reader)>0 and fariqi>''2004-5-5''
用時(shí):7秒,另外:掃描計(jì)數(shù) 4,邏輯讀 7155 次,物理讀 0 次,預(yù)讀 0 次。

select gid,title,fariqi,reader from tgongwen
where reader like ''%'' + ''刑偵支隊(duì)'' + ''%'' and fariqi>''2004-5-5''
用時(shí):7秒,另外:掃描計(jì)數(shù) 4,邏輯讀 7155 次,物理讀 0 次,預(yù)讀 0 次。

8、union并不絕對(duì)比or的執(zhí)行效率高
  我們前面已經(jīng)談到了在where子句中使用or會(huì)引起全表掃描,一般的,我所見過的資料都是推薦這里用union來代替or。事實(shí)證明,這種說法對(duì)于大部分都是適用的。
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi=''2004-9-16'' or gid>9990000
用時(shí):68秒。掃描計(jì)數(shù) 1,邏輯讀 404008 次,物理讀 283 次,預(yù)讀 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
用時(shí):9秒。掃描計(jì)數(shù) 8,邏輯讀 67489 次,物理讀 216 次,預(yù)讀 7499 次。

看來,用union在通常情況下比用or的效率要高的多。

  但經(jīng)過試驗(yàn),筆者發(fā)現(xiàn)如果or兩邊的查詢列是一樣的話,那么用union則反倒和用or的執(zhí)行速度差很多,雖然這里union掃描的是索引,而or掃描的是全表。
select gid,fariqi,neibuyonghu,reader,title from Tgongwen
where fariqi=''2004-9-16'' or fariqi=''2004-2-5''
用時(shí):6423毫秒。掃描計(jì)數(shù) 2,邏輯讀 14726 次,物理讀 1 次,預(yù)讀 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''
用時(shí):11640毫秒。掃描計(jì)數(shù) 8,邏輯讀 14806 次,物理讀 108 次,預(yù)讀 1144 次。

9、字段提取要按照“需多少、提多少”的原則,避免“select *”
我們來做一個(gè)試驗(yàn):
select top 10000 gid,fariqi,reader,title from tgongwen order by gid desc
用時(shí):4673毫秒

select top 10000 gid,fariqi,title from tgongwen order by gid desc
用時(shí):1376毫秒

select top 10000 gid,fariqi from tgongwen order by gid desc
用時(shí):80毫秒

  由此看來,我們每少提取一個(gè)字段,數(shù)據(jù)的提取速度就會(huì)有相應(yīng)的提升。提升的速度還要看您舍棄的字段的大小來判斷。

相關(guān)文章

最新評(píng)論