三种SQL分页方法性能分析

--下面是三种分页方法性能测试。
--与网上所说的测试结果不一致
--(网上有人认为颠倒顺序top法 性能要比Row_number函数编号分页法要快10倍。。。)
--测试的表只有3000多行数据这可能是个问题

set statistics profile on
set statistics io on
set statistics time on
go

--第一名
--Row_number函数编号分页法(SQLServer新提供的函数)
--按SQLServer每一步的执行过程来看:
--用Row_number分页的执行过程步骤要比用top分页的执行步骤要多
--但用Row_number分页第三步(前两步是一样的是结果集的查询)
----也就是分页开始后的开销基本上等于0。
----可能是row_Number函数在查询的结果集上建了一个索引。
--而用top如果排序不是按索引来排的话每次排序都会花费不少时间
select * from (
select
--top 3020 --加不加top执行过程是一样的。。。
ROW_NUMBER()over(order by ProductCode DESC ) as rowNum,*
from Clothes where Sex='男')
results
--where rowNum between 3001 and 3020
--改成下面的写法要快一些。上面的写法在实际执行的时候是与下面写法执行是一样的。
where rowNum >= 3001 and rowNum <= 3020


--第二名
--用颠倒顺序top法
select * from (
select top 20 * from (
select top 3020 *
from Clothes where Sex='男'
order by ProductCode DESC
) pageResult order by ProductCode ASC )
pageSort order by ProductCode DESC



--第三名
--用top加outer join 法执行12步
select toPage.* from
(
select top 3020 *
from Clothes where Sex='男'
order by ProductCode DESC
)
toPage
left outer join
(
select top 3000 * from Clothes where Sex='男'
order by ProductCode DESC
) before
on toPage.clothescode=before.clothescode
where before.clothescode is null


go
set statistics profile off
set statistics io off
set statistics time off

posted @ 2009-03-20 12:15  BB_Coder  阅读(684)  评论(1编辑  收藏  举报