SQL Server 三种分页方式性能比较
2022-11-12 09:48:50
内容摘要
这篇文章主要为大家详细介绍了SQL Server 三种分页方式性能比较,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!Liwu_Items表,CreateTime列
文章正文
这篇文章主要为大家详细介绍了SQL Server 三种分页方式性能比较,具有一定的参考价值,可以用来参考一下。
对此感兴趣的朋友,看看idc笔记做的技术笔记!
Liwu_Items表,CreateTime列建立聚集索引第一种,sqlserver2005特有的分页语法代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <code> declare @page int declare @pagesize int set @page = 2 set @pagesize = 12 SET STATISTICS IO on SELECT a.* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY b.CreateTime DESC) AS [ROW_NUMBER], b.* FROM [dbo].[Liwu_Items] AS b ) AS a WHERE a.[ROW_NUMBER] BETWEEN @pagesize + 1 AND (@page*@pagesize) ORDER BY a.[ROW_NUMBER] </code> |
执行计划:
【图片暂缺】
主要开销都在聚集索引扫描了。
第二种,用两个top分别正序和倒序排列,共另个子查询来实现分页,
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 | <code> declare @page int declare @pagesize int set @page = 2 set @pagesize = 12 SET STATISTICS IO on select * from ( select top (@pagesize) * from (select top (@page*@pagesize) * from Liwu_Items order by CreateTime desc) a order by CreateTime asc) b order by CreateTime desc </code> |
【图片暂缺】
执行计划和第一种差不多,但两个排序占的资源挺多的。
第三种,最垃圾的一种,用not in字句实现的,如下
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 | <code> declare @page int declare @pagesize int set @page = 2 set @pagesize = 12 SET STATISTICS IO on select top(@pagesize) * from Liwu_Items where ItemId not in( select top((@page-1)*@pagesize) ItemId from Liwu_Items order by CreateTime desc) order by CreateTime Desc </code> |
执行计划
【图片暂缺】
这执行计划都看不懂,嵌套循环和表假脱机占了很大的资源。
总结:第二种分页方法和第一种分页方法效率差不多,但第二种可用于老版本的sqlserver甚至access,最后一种别用。
注:关于SQL Server 三种分页方式性能比较的内容就先介绍到这里,更多相关文章的可以留意
代码注释