今天閑來學習了一下SQL效能最佳化方面的知識,有以下學習收穫,歡迎大家指點。
測試環境:90W,單條記錄約3KB,資料庫:MSSQL2005
測試前清除緩衝
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
一、翻頁效能測試
1、Top
select
top 10 * from message where id not in (select top 20 id frommessage
where classid=77 order by id desc ) and classid=77 order by id desc
2、Max/Top
select
top 10 * from message where id <(select min(id) from messagewhere
id in(select top 20 id from message where classid=77 order by iddesc) )
and classid=77 order by id desc
3、row_number
select top 10 * from
(select row_number()over(order by id desc) rownumber,*from message where
classid=77)a where classid=77 and rownumber>20
MsSql翻頁效能測試 |
ID列索引 |
Top |
Max/Top |
row_number() |
無索引 |
cpu |
reads |
duration |
0 |
893 |
65 |
|
cpu |
reads |
duration |
0 |
590 |
70 |
|
cpu |
reads |
duration |
0 |
512 |
67 |
|
聚焦索引 |
cpu |
reads |
duration |
0 |
37 |
66 |
|
cpu |
reads |
duration |
0 |
98 |
64 |
|
cpu |
reads |
duration |
0 |
28 |
67 |
|
非聚焦索引 |
cpu |
reads |
duration |
0 |
895 |
63 |
|
cpu |
reads |
duration |
0 |
592 |
66 |
|
cpu |
reads |
duration |
0 |
514 |
66 |
|
結論:
1)從以上測試結果可以看出,不論是否索引排序欄位,也不管是何種索引,row_number都能得到最高的效能,其次Max/Top的方式測試效能也不錯。
2)在使用非聚焦索引的情況下,效能並無任何提示,甚至要慢於無索引的情況,可能是因為SQL先要去尋找索引表,然後根據索引結果再去尋找實體表,在這過程浪費了資源。
3)聚焦索引也的正確應用才能發揮其該有的優勢啊!
綜合結果:row_number> max/top > top
二、in、or、union關鍵字效能測試
介於網上有很多關於in/or/union等關鍵字的效能討論,本人也小試了一把,測試結果如下。
1、in
select * from video where id in(100,101,102,103,104,105,106,107,108,109)
2、union
select * from video where id =100
union all select * from video where id =101
union all select * from video where id =102
union all select * from video where id =103
union all select * from video where id =104
union all select * from video where id =105
union all select * from video where id =106
union all select * from video where id =107
union all select * from video where id =108
union all select * from video where id =109
3、or
select * from video where id=100 or id=101 or id=102 or id=103or id=104 or id=105 or id=106 or id=107 or id=108 or id=109
in PK or PK union |
|
ID列索引 |
in |
union |
or |
無索引 |
cpu |
reads |
duration |
0 |
37 |
54 |
|
cpu |
reads |
duration |
0 |
58 |
104 |
|
cpu |
reads |
duration |
0 |
41 |
56 |
|
聚焦索引 |
cpu |
reads |
duration |
0 |
44 |
54 |
|
cpu |
reads |
duration |
0 |
54 |
58 |
|
cpu |
reads |
duration |
0 |
40 |
54 |
|
非聚焦索引 |
cpu |
reads |
duration |
0 |
43 |
53 |
|
cpu |
reads |
duration |
16 |
61 |
62 |
|
cpu |
reads |
duration |
0 |
43 |
54 |
|
結論:
1) 網上很多資料說union的效能要高於in/or,但從我這測試的結果來看,不論是有無索引,union的效能都是最低的?不知是何原因?
2) 網上流傳mssql會自己把in解析成or查詢,從這份測試結果來看,貌似不假!
3) 雖然in/or會引起全表掃描,但別無選擇的情況下也是是能勝任很多工作的。
第一次分享技術類的文章,寫的不好歡迎拍磚,謝謝!
http://www.vhaoxue.com
今天閑來學習了一下SQL效能最佳化方面的知識,有以下學習收穫,歡迎大家指點。
測試環境:90W,單條記錄約3KB,資料庫:MSSQL2005
測試前清除緩衝
DBCC FREEPROCCACHE
DBCC DROPCLEANBUFFERS
一、翻頁效能測試
1、Top
select
top 10 * from message where id not in (select top 20 id frommessage
where classid=77 order by id desc ) and classid=77 order by id desc
2、Max/Top
select
top 10 * from message where id <(select min(id) from messagewhere
id in(select top 20 id from message where classid=77 order by iddesc) )
and classid=77 order by id desc
3、row_number
select top 10 * from
(select row_number()over(order by id desc) rownumber,*from message where
classid=77)a where classid=77 and rownumber>20
MsSql翻頁效能測試 |
ID列索引 |
Top |
Max/Top |
row_number() |
無索引 |
cpu |
reads |
duration |
0 |
893 |
65 |
|
cpu |
reads |
duration |
0 |
590 |
70 |
|
cpu |
reads |
duration |
0 |
512 |
67 |
|
聚焦索引 |
cpu |
reads |
duration |
0 |
37 |
66 |
|
cpu |
reads |
duration |
0 |
98 |
64 |
|
cpu |
reads |
duration |
0 |
28 |
67 |
|
非聚焦索引 |
cpu |
reads |
duration |
0 |
895 |
63 |
|
cpu |
reads |
duration |
0 |
592 |
66 |
|
cpu |
reads |
duration |
0 |
514 |
66 |
|
結論:
1)從以上測試結果可以看出,不論是否索引排序欄位,也不管是何種索引,row_number都能得到最高的效能,其次Max/Top的方式測試效能也不錯。
2)在使用非聚焦索引的情況下,效能並無任何提示,甚至要慢於無索引的情況,可能是因為SQL先要去尋找索引表,然後根據索引結果再去尋找實體表,在這過程浪費了資源。
3)聚焦索引也的正確應用才能發揮其該有的優勢啊!
綜合結果:row_number> max/top > top
二、in、or、union關鍵字效能測試
介於網上有很多關於in/or/union等關鍵字的效能討論,本人也小試了一把,測試結果如下。
1、in
select * from video where id in(100,101,102,103,104,105,106,107,108,109)
2、union
select * from video where id =100
union all select * from video where id =101
union all select * from video where id =102
union all select * from video where id =103
union all select * from video where id =104
union all select * from video where id =105
union all select * from video where id =106
union all select * from video where id =107
union all select * from video where id =108
union all select * from video where id =109
3、or
select * from video where id=100 or id=101 or id=102 or id=103or id=104 or id=105 or id=106 or id=107 or id=108 or id=109
in PK or PK union |
|
ID列索引 |
in |
union |
or |
無索引 |
cpu |
reads |
duration |
0 |
37 |
54 |
|
cpu |
reads |
duration |
0 |
58 |
104 |
|
cpu |
reads |
duration |
0 |
41 |
56 |
|
聚焦索引 |
cpu |
reads |
duration |
0 |
44 |
54 |
|
cpu |
reads |
duration |
0 |
54 |
58 |
|
cpu |
reads |
duration |
0 |
40 |
54 |
|
非聚焦索引 |
cpu |
reads |
duration |
0 |
43 |
53 |
|
cpu |
reads |
duration |
16 |
61 |
62 |
|
cpu |
reads |
duration |
0 |
43 |
54 |
|
結論:
1) 網上很多資料說union的效能要高於in/or,但從我這測試的結果來看,不論是有無索引,union的效能都是最低的?不知是何原因?
2) 網上流傳mssql會自己把in解析成or查詢,從這份測試結果來看,貌似不假!
3) 雖然in/or會引起全表掃描,但別無選擇的情況下也是是能勝任很多工作的。
第一次分享技術類的文章,寫的不好歡迎拍磚,謝謝!
http://www.vhaoxue.com