(转)常见存储过程分页PK赛——简单测试分析常见存储过程分页速度
原文地址:http://www.cnblogs.com/yangyy753/archive/2013/01/23/2872753.html
数据的分页是我们再熟悉不过的功能了,各种各样的分页方式层出不穷。今天我把一些常见的存储过程分页列出来,再简单地测一下性能,算是对知识的总结,也是对您好想法的抛钻引玉。废话不多说,开始吧~~
1.首先建立一张测试表
--创建测试表 SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[testTable]( [id] [int] IDENTITY(1,1) NOT NULL, [testDate] [datetime] NOT NULL CONSTRAINT [DF_testTable_testDate] DEFAULT (getdate()), [name] [nvarchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL, [description] [nchar](50) COLLATE Chinese_PRC_CI_AS NOT NULL, [orderColum] [float] NOT NULL, CONSTRAINT [PK_testTable] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY] ) ON [PRIMARY]
2.循环插入1000000条测试数据
declare @i int set @i = 1 while @i < 1000001 begin INSERT INTO testTable([name],[description],[orderColum]) VALUES('PageTest', 'http://www.3ymao.com', @i * rand()) set @i = @i + 1 end
3.晒出我的系统硬件和软件(测试环境)
好吧,准备工作完成,开始进入主题(为了方便,以下代码我就不写成存储过程的方式来测试展示了)~~噔噔!首先登场的是我最常用的Not IN
1)NOT IN
declare @timediff datetime declare @pageIndex int declare @pageSize int declare @sql varchar(500) set @pageIndex=1 set @pageSize=10 set @timediff=GetDATE() set @sql='select top ('+cast(@pageSize as varchar)+') * from testTable where (id not in ( select top '+cast(@pageSize*(@pageIndex-1) as varchar)+' id from testTable order by id)) order by id' exec(@sql) select datediff(ms,@timediff,Getdate())
@pageIndex=1时,运行:0ms(给力啊!)
@pageIndex=50000时,运行:346ms(怎么50000页就不给力了)
@pageIndex=100000时,运行:326ms(怎么比50000页时还少了?)
2)MAX()
declare @timediff datetime declare @pageIndex int declare @pageSize int declare @sql varchar(500) set @timediff=GetDATE() set @pageIndex=1 set @pageSize=10 set @sql='select top ('+cast(@pageSize as varchar)+') * from testTable where (id >= (select MAX(id) from (select top '+cast((@pageSize*(@pageIndex-1)+1) as varchar)+' id from testTable order by id) as a)) order by id' exec(@sql) select datediff(ms,@timediff,Getdate())
@pageIndex=1时,运行:0ms(也是很给力啊!)
@pageIndex=50000时,运行:123ms(不错)
@pageIndex=100000时,运行:220ms(页数和查询时间成正比)
3)Row_Number()
declare @timediff datetime declare @pageIndex int declare @pageSize int declare @sql varchar(500) set @timediff=GetDATE() set @pageIndex=1 set @pageSize=10 set @sql='select * from (select *,row_number() over (order by id asc) as RowIndex from testTable) as IDWithRowNumber where RowIndex between '+cast(((@pageIndex-1)*@pageSize)+1 as varchar)+' and '+cast(@pageIndex*@pageSize as varchar)+'' exec(@sql) select datediff(ms,@timediff,getdate())
@pageIndex=1时,运行:0ms(好吧……数据量小的时候都是这尿性)
@pageIndex=50000时,运行:280ms(略逊色)
@pageIndex=100000时,运行:580ms(这货居然也是页数和查询时间成正比!坑爹吧!)
4)临时表
declare @timediff datetime declare @pageIndex int declare @pageSize int declare @sql varchar(500) declare @str varchar(500) set @timediff=GetDATE() set @pageIndex=1 set @pageSize=10 set @str='with tempTable as (select ceiling((Row_number() over (order by id asc))/'+cast(@pageSize as varchar)+') as page_num,* from testTable)' set @sql=@str+'select * from tempTable where page_num='+cast(@pageIndex-1 as varchar)+'' exec(@sql) select datediff(ms,@timediff,getdate())
@pageIndex=1时,运行:280ms(不咧个是吧!这非主流啊)
@pageIndex=50000时,运行:280ms(这不科学……)
@pageIndex=100000时,运行:280ms(好吧,这货不受页数的影响,永远都这速度)
5)中间变量
declare @timediff datetime declare @pageIndex int declare @pageSize int declare @count int declare @id int declare @sql varchar(500) set @pageIndex=1 set @pageSize=10 select @id=0,@count=0,@timediff=GetDATE() select @count=@count+1,@id=case when @count=(@pageIndex-1)*@pageSize then id else @id end from testTable order by id set @sql='select top '+cast(@pageSize as varchar)+' * from testTable where id>'+cast(@id as varchar)+'' exec(@sql) select datediff(ms,@timediff,getdate())
@pageIndex=1时,运行:360ms(哥,不是吧,才第一页你就这速度)
@pageIndex=50000时,运行:360ms(我大概猜到100000页时的速度了……)
@pageIndex=100000时,运行:360ms(好吧,又是不受页数影响的货)
从以上数据,我最后简单分析总结一下:
NOT IN:数据量小的时候,速度不错,但是数据量大的时候速度就有点逊色了,但是好在随着查询页数增大,他的速度还是不会改变多少。
MAX:小数据量的时候,它的速度是最快的,但是遗憾的是查询页数越大,速度则越慢。
Row_Number():性质与MAX相似,但是不比MAX速度快
临时表:不会因为查询页数而改变查询速度,只和数据量大小有关,个人觉得适合大数据量而且可能会查很大的页数时使用
中间变量:性质和临时表相似,但是逊色于临时表