今天闲来学习了一下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() |
||||||||||||||||||
无索引 |
|
|
|
||||||||||||||||||
聚焦索引 |
|
|
|
||||||||||||||||||
非聚焦索引 |
|
|
|