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,且引用的所有列都包含在複合式索引中,形成了索引覆蓋,所以它的速度是非常快的。
---- 4.在date,place,amount上的複合式索引
select count(*) from record where date >
'19991201' and date < '19991214' and amount >
2000(< 1秒)
select date,sum(amount) from record group by date
(11秒)
select count(*) from record where date >
'19990901' and place in ('BJ','SH')(< 1秒)
---- 分析:
---- 這是一個合理的複合式索引。它將date作為前置列,使每個SQL都可以利用索引,並且在第一和第三個SQL中形成了索引覆蓋,因而效能達到了最優。
select sum(a.amount) from account a,
card b where a.card_no = b.card_no(20秒)
---- 將SQL改為:
select sum(a.amount) from account a,
card b where a.card_no = b.card_no and a.
account_no=b.account_no(< 1秒)
---- 分析:
---- 在第一個串連條件下,最佳查詢方案是將account作外層表,card作內層表,利用card上的索引,其I/O次數可由以下公式估算為:
select * from record where
substring(card_no,1,4)='5378'(13秒)
select * from record where
amount/30< 1000(11秒)
select * from record where
convert(char(10),date,112)='19991201'(10秒)
---- 分析:
---- where子句中對列的任何操作結果都是在SQL運行時逐列計算得到的,因此它不得不進行表搜尋,而沒有使用該列上面的索引;如果這些結果在查詢編譯時間就能得到,那麼就可以被SQL最佳化器最佳化,使用索引,避免表搜尋,因此將SQL重寫成下面這樣:
select * from record where card_no like
'5378%'(< 1秒)
select * from record where amount
< 1000*30(< 1秒)
select * from record where date= '1999/12/01'
(< 1秒)
---- 你會發現SQL明顯快起來!
---- 2.例:表stuff有200000行,id_no上有非群集索引,請看下面這個SQL:
select count(*) from stuff where id_no in('0','1')
(23秒)
---- 分析:
---- where條件中的'in'在邏輯上相當於'or',所以文法分析器會將in ('0','1')轉化為id_no ='0' or id_no='1'來執行。我們期望它會根據每個or子句分別尋找,再將結果相加,這樣可以利用id_no上的索引;但實際上(根據showplan),它卻採用了"OR策略",即先取出滿足每個or子句的行,存入臨時資料庫的工作表中,再建立唯一索引以去掉重複行,最後從這個暫存資料表中計算結果。因此,實際過程沒有利用id_no上索引,並且完成時間還要受tempdb資料庫效能的影響。
select count(*) from stuff where id_no='0'
select count(*) from stuff where id_no='1'
---- 得到兩個結果,再作一次加法合算。因為每句都使用了索引,執行時間只有3秒,在620000行下,時間也只有4秒。或者,用更好的方法,寫一個簡單的預存程序:
create proc count_stuff as
declare @a int
declare @b int
declare @c int
declare @d char(10)
begin
select @a=count(*) from stuff where id_no='0'
select @b=count(*) from stuff where id_no='1'
end
select @c=@a+@b
select @d=convert(char(10),@c)
print @d
---- 直接算出結果,執行時間同上面一樣快!
---- 總結: