标签:创建数据库 大数 环境 prim exec .com -- rank test
在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下,哪种分页算法效率最优呢?我们不妨用事实说话。
测试环境
硬件:CPU 酷睿双核T5750 内存:2G
软件:Windows server 2003 + Sql server 2005
OK,我们首先创建一数据库:data_Test,并在此数据库中创建一表:tb_TestTable
1create database data_Test --创建数据库data_Test 2GO 3use data_Test 4GO 5create table tb_TestTable --创建表 6( id int identity(1,1) primary key, userName nvarchar(20) not null, userPWD nvarchar(20) not null, userEmail nvarchar(40) null 11) 12GO
1--插入数据 2set identity_insert tb_TestTable on 3declare @count int 4set @count=1 5while @count<=2000000 6begin insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,‘admin‘,‘admin888‘,‘lli0077@yahoo.com.cn‘) set @count=@count+1 9end 10set identity_insert tb_TestTable off
利用select top 和select not in进行分页,具体代码如下:
1create procedure proc_paged_with_notin --利用select top and select not in 2( @pageIndex int, --页索引 @pageSize int --每页记录数 5) 6as 7begin set nocount on; declare @timediff datetime --耗时 declare @sql nvarchar(500) select @timediff=Getdate() set @sql=‘select top ‘+str(@pageSize)+‘ * from tb_TestTable where(ID not in(select top ‘+str(@pageSize*@pageIndex)+‘ id from tb_TestTable order by ID ASC)) order by ID‘ execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql select datediff(ms,@timediff,GetDate()) as 耗时 set nocount off; 16end
利用select top 和 select max(列键)
create procedure proc_paged_with_selectMax --利用select top and select max(列) 2( 3 @pageIndex int, --页索引 4 @pageSize int --页记录数 5) 6as 7begin 8set nocount on; 9 declare @timediff datetime 10 declare @sql nvarchar(500) 11 select @timediff=Getdate() 12 set @sql=‘select top ‘+str(@pageSize)+‘ * From tb_TestTable where(ID>(select max(id) From (select top ‘+str(@pageSize*@pageIndex)+‘ id From tb_TestTable order by ID) as TempTable)) order by ID‘ 13 execute(@sql) 14 select datediff(ms,@timediff,GetDate()) as 耗时 15set nocount off; 16end
利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引
1create procedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number() 2( @pageIndex int, @pageSize int 5) 6as declare @timediff datetime 8begin 9set nocount on; select @timediff=getdate() select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1) select datediff(ms,@timediff,getdate()) as 耗时 13set nocount off; 14end 15
标签:创建数据库 大数 环境 prim exec .com -- rank test
原文地址:http://www.cnblogs.com/ZxGs/p/13-12-24.html