大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)  

在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下,哪种分页算法效率最优呢?我们不妨用事实说话。

测试环境

硬件:CPU 酷睿双核T5750  内存:2G

软件:Windows server 2003    +   Sql server 2005

OK,我们首先创建一数据库:data_Test,并在此数据库中创建一表:tb_TestTable

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create database data_Test  --创建数据库data_Test 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 use data_Test 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create table tb_TestTable   --创建表 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     id int identity(1,1) primary key, 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     userName nvarchar(20) not null, 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     userPWD nvarchar(20) not null, 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     userEmail nvarchar(40) null 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

 

然后我们在数据表中插入2000000条数据:

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --插入数据 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set identity_insert tb_TestTable on 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 declare @count int 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set @count=1 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 while @count<=2000000 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin  
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn') 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @count=@count+1 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set identity_insert tb_TestTable off
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

 

我首先写了五个常用存储过程:

1,利用select top 和select not in进行分页,具体代码如下:

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_notin  --利用select top and select not in 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int,  --页索引 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int    --每页记录数 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount on; 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime --耗时 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @sql nvarchar(500) 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @timediff=Getdate() 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     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' 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     execute(@sql)  --因select top后不支技直接接参数,所以写成了字符串@sql 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,GetDate()) as 耗时 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount off; 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

 

2,利用select top 和 select max(列键)

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_selectMax  --利用select top and select max(列) 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int,  --页索引 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int    --页记录数 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount on; 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @sql nvarchar(500) 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @timediff=Getdate() 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     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大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     execute(@sql) 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,GetDate()) as 耗时 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount off; 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

3,利用select top和中间变量--此方法因网上有人说效果最佳,所以贴出来一同测试

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_Midvar  --利用ID>最大ID值和中间变量 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int, 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @count int 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @ID int 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @sql nvarchar(500) 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount on; 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @count=0,@ID=0,@timediff=getdate() 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID) 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     execute(@sql) 
17大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,getdate()) as 耗时 
18大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount off; 
19大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
20大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

4,利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_Rownumber  --利用SQL 2005中的Row_number() 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int, 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount on; 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @timediff=getdate() 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     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) 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,getdate()) as 耗时 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount off; 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

5,利用临时表及Row_number

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_CTE  --利用临时表及Row_number 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int,  --页索引 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int    --页记录数 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount on; 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @ctestr nvarchar(400) 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @strSql nvarchar(400) 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @datediff datetime 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @datediff=GetDate() 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @ctestr='with Table_CTE as 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 (select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)'; 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex) 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
17大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     begin 
18大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         execute sp_executesql @strSql 
19大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         select datediff(ms,@datediff,GetDate()) 
20大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount off; 
21大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     end
22大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

OK,至此,存储过程创建完毕,我们分别在每页10条数据的情况下在第2页,第1000页,第10000页,第100000页,第199999页进行测试,耗时单位:ms  每页测试5次取其平均值

存过 第2页耗时 第1000页耗时 第10000页耗时 第100000页耗时 第199999页耗时 效率排行
1用not in 0ms 16ms 47ms 475ms 953ms 3
2用select max 5ms 16ms 35ms 325ms 623ms 1
3中间变量 966ms 970ms 960ms 945ms 933ms 5
4row_number 0ms 0ms 34ms 365ms 710ms 2
4临时表 780ms 796ms 798ms 780ms 805ms 4

测试结果显示:select max >row_number>not in>临时表>中间变量

于是我对效率最高的select max方法用2分法进行了扩展,代码取自互联网,我修改了ASC排序时取不到值的BUG,测试结果:

2分法 156ms 156ms 180ms 470ms 156ms 1*

从测试结果来看,使用2分法确实可以提高效率并使效率更为稳定,我又增加了第159999页的测试,用时仅296ms,效果相当的不错!

下面是2分法使用select max的代码,已相当完善。

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
  1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/ 
  2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --/*-----存储过程 分页处理 浪尘 2008-9-1修改----------*/ 
  3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/ 
  4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
  5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 alter PROCEDURE proc_paged_2part_selectMax 
  6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
  7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @tblName     nvarchar(200),        ----要显示的表或多个表的连接 
  8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @fldName     nvarchar(500) = '*',    ----要显示的字段列表 
  9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @pageSize    int = 10,        ----每页显示的记录个数 
 10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @page        int = 1,        ----要显示那一页的记录 
 11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @fldSort    nvarchar(200) = null,    ----排序字段列表或条件 
 12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @Sort        bit = 0,        ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ') 
 13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @strCondition    nvarchar(1000) = null,    ----查询条件,不需where 
 14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @ID        nvarchar(150),        ----主表的主键 
 15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @Dist                 bit = 0,           ----是否添加查询字段的 DISTINCT 默认0不添加/1添加 
 16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @pageCount    int = 1 output,            ----查询结果分页后的总页数 
 17大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @Counts    int = 1 output                ----查询到的记录数 
 18大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 19大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 AS 
 20大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 SET NOCOUNT ON 
 21大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @sqlTmp nvarchar(1000)        ----存放动态生成的SQL语句 
 22大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strTmp nvarchar(1000)        ----存放取得查询结果总数的查询语句 
 23大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strID     nvarchar(1000)        ----存放取得查询开头或结尾ID的查询语句 
 24大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 25大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strSortType nvarchar(10)    ----数据排序规则A 
 26大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strFSortType nvarchar(10)    ----数据排序规则B 
 27大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 28大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @SqlSelect nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造 
 29大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @SqlCounts nvarchar(50)          ----对含有DISTINCT的总数查询进行SQL构造 
 30大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 31大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 declare @timediff datetime  --耗时测试时间差 
 32大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 select @timediff=getdate() 
 33大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 34大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @Dist  = 0 
 35大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 36大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlSelect = 'select ' 
 37大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlCounts = 'Count(*)' 
 38大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 39大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 40大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 41大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlSelect = 'select distinct ' 
 42大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlCounts = 'Count(DISTINCT '+@ID+')' 
 43大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 44大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 45大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 46大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @Sort=0 
 47大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 48大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strFSortType=' ASC ' 
 49大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strSortType=' DESC ' 
 50大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 51大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 52大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 53大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strFSortType=' DESC ' 
 54大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strSortType=' ASC ' 
 55大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 56大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 57大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 58大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 59大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --------生成查询语句-------- 
 60大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --此处@strTmp为取得查询结果数量的语句 
 61大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @strCondition is null or @strCondition=''     --没有设置显示条件 
 62大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 63大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @sqlTmp =  @fldName + ' From ' + @tblName 
 64大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName 
 65大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strID = ' From ' + @tblName 
 66大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 67大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 68大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 69大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition 
 70大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition 
 71大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition 
 72大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 73大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 74大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 ----取得查询结果总数量----- 
 75大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 exec sp_executesql @strTmp,N'@Counts int out ',@Counts out 
 76大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 declare @tmpCounts int 
 77大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @Counts = 0 
 78大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @tmpCounts = 1 
 79大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 80大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @tmpCounts = @Counts 
 81大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 82大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     --取得分页总数 
 83大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize 
 84大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 85    /**//**当前页大于总页数 取最后一页**/ 
 86大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     if @page>@pageCount 
 87大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         set @page=@pageCount 
 88大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 89大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     --/*-----数据分页2分处理-------*/ 
 90大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @pageIndex int --总数/页大小 
 91大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @lastcount int --总数%页大小  
 92大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 93大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @pageIndex = @tmpCounts/@pageSize 
 94大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @lastcount = @tmpCounts%@pageSize 
 95大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     if @lastcount > 0 
 96大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         set @pageIndex = @pageIndex + 1 
 97大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     else 
 98大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         set @lastcount = @pagesize 
 99大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
100大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     --//***显示分页 
101大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     if @strCondition is null or @strCondition=''     --没有设置显示条件 
102大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     begin 
103大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理 
104大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             begin  
105大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 if @page=1 
106大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName                         
107大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType 
108大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else 
109大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 begin 
110大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     if @Sort=1 
111大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin                     
112大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
113大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
114大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
115大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType 
116大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
117大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     else 
118大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin 
119大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
120大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
121大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
122大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType  
123大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
124大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 end     
125大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             end 
126大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         else 
127大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             begin 
128大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             set @page = @pageIndex-@page+1 --后半部分数据处理 
129大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 if @page <= 1 --最后一页数据显示                 
130大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
131大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType  
132大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else 
133大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     if @Sort=1 
134大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin 
135大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
136大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
137大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
138大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
139大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
140大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     else 
141大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin 
142大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
143大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
144大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
145大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType  
146大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
147大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             end 
148大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     end 
149大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
150大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     else --有查询条件 
151大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     begin 
152大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理 
153大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         begin 
154大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 if @page=1 
155大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName                         
156大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType 
157大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else if(@Sort=1) 
158大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 begin                     
159大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
160大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
161大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
162大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType 
163大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 end 
164大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else 
165大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 begin 
166大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
167大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
168大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
169大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType  
170大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 end            
171大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         end 
172大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         else 
173大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         begin  
174大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             set @page = @pageIndex-@page+1 --后半部分数据处理 
175大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             if @page <= 1 --最后一页数据显示 
176大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
177大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                      
178大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             else if(@Sort=1) 
179大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
180大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
181大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
182大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType     
183大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             else 
184大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
185大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
186大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
187大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType             
188大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         end     
189大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     end 
190大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
191大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 ------返回查询结果----- 
192大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 exec sp_executesql @strTmp 
193大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 select datediff(ms,@timediff,getdate()) as 耗时 
194大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --print @strTmp 
195大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 SET NOCOUNT OFF 
196大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO
197大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

在项目中,我们经常遇到或用到分页,那么在大数据量(百万级以上)下,哪种分页算法效率最优呢?我们不妨用事实说话。

测试环境

硬件:CPU 酷睿双核T5750  内存:2G

软件:Windows server 2003    +   Sql server 2005

OK,我们首先创建一数据库:data_Test,并在此数据库中创建一表:tb_TestTable

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create database data_Test  --创建数据库data_Test 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 use data_Test 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create table tb_TestTable   --创建表 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     id int identity(1,1) primary key, 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     userName nvarchar(20) not null, 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     userPWD nvarchar(20) not null, 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     userEmail nvarchar(40) null 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

 

然后我们在数据表中插入2000000条数据:

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --插入数据 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set identity_insert tb_TestTable on 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 declare @count int 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set @count=1 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 while @count<=2000000 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin  
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn') 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @count=@count+1 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set identity_insert tb_TestTable off
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

 

我首先写了五个常用存储过程:

1,利用select top 和select not in进行分页,具体代码如下:

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_notin  --利用select top and select not in 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int,  --页索引 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int    --每页记录数 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount on; 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime --耗时 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @sql nvarchar(500) 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @timediff=Getdate() 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     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' 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     execute(@sql)  --因select top后不支技直接接参数,所以写成了字符串@sql 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,GetDate()) as 耗时 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount off; 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

 

2,利用select top 和 select max(列键)

 

大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_selectMax  --利用select top and select max(列) 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int,  --页索引 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int    --页记录数 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount on; 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @sql nvarchar(500) 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @timediff=Getdate() 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     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大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     execute(@sql) 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,GetDate()) as 耗时 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount off; 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

3,利用select top和中间变量--此方法因网上有人说效果最佳,所以贴出来一同测试

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_Midvar  --利用ID>最大ID值和中间变量 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int, 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @count int 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @ID int 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @sql nvarchar(500) 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount on; 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @count=0,@ID=0,@timediff=getdate() 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID) 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     execute(@sql) 
17大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,getdate()) as 耗时 
18大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount off; 
19大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
20大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

4,利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_paged_with_Rownumber  --利用SQL 2005中的Row_number() 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int, 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @timediff datetime 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount on; 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @timediff=getdate() 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     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) 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select datediff(ms,@timediff,getdate()) as 耗时 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 set nocount off; 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

5,利用临时表及Row_number

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 create procedure proc_CTE  --利用临时表及Row_number 
 2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageIndex int,  --页索引 
 4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     @pageSize int    --页记录数 
 5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 as 
 7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount on; 
 8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @ctestr nvarchar(400) 
 9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @strSql nvarchar(400) 
10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @datediff datetime 
11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     select @datediff=GetDate() 
13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @ctestr='with Table_CTE as 
14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 (select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)'; 
15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex) 
16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
17大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     begin 
18大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         execute sp_executesql @strSql 
19大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         select datediff(ms,@datediff,GetDate()) 
20大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set nocount off; 
21大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     end
22大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 

OK,至此,存储过程创建完毕,我们分别在每页10条数据的情况下在第2页,第1000页,第10000页,第100000页,第199999页进行测试,耗时单位:ms  每页测试5次取其平均值

存过 第2页耗时 第1000页耗时 第10000页耗时 第100000页耗时 第199999页耗时 效率排行
1用not in 0ms 16ms 47ms 475ms 953ms 3
2用select max 5ms 16ms 35ms 325ms 623ms 1
3中间变量 966ms 970ms 960ms 945ms 933ms 5
4row_number 0ms 0ms 34ms 365ms 710ms 2
4临时表 780ms 796ms 798ms 780ms 805ms 4

测试结果显示:select max >row_number>not in>临时表>中间变量

于是我对效率最高的select max方法用2分法进行了扩展,代码取自互联网,我修改了ASC排序时取不到值的BUG,测试结果:

2分法 156ms 156ms 180ms 470ms 156ms 1*

从测试结果来看,使用2分法确实可以提高效率并使效率更为稳定,我又增加了第159999页的测试,用时仅296ms,效果相当的不错!

下面是2分法使用select max的代码,已相当完善。

 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
  1大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/ 
  2大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --/*-----存储过程 分页处理 浪尘 2008-9-1修改----------*/ 
  3大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/ 
  4大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
  5大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 alter PROCEDURE proc_paged_2part_selectMax 
  6大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
  7大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @tblName     nvarchar(200),        ----要显示的表或多个表的连接 
  8大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @fldName     nvarchar(500) = '*',    ----要显示的字段列表 
  9大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @pageSize    int = 10,        ----每页显示的记录个数 
 10大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @page        int = 1,        ----要显示那一页的记录 
 11大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @fldSort    nvarchar(200) = null,    ----排序字段列表或条件 
 12大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @Sort        bit = 0,        ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ') 
 13大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @strCondition    nvarchar(1000) = null,    ----查询条件,不需where 
 14大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @ID        nvarchar(150),        ----主表的主键 
 15大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @Dist                 bit = 0,           ----是否添加查询字段的 DISTINCT 默认0不添加/1添加 
 16大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @pageCount    int = 1 output,            ----查询结果分页后的总页数 
 17大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 @Counts    int = 1 output                ----查询到的记录数 
 18大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 19大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 AS 
 20大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 SET NOCOUNT ON 
 21大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @sqlTmp nvarchar(1000)        ----存放动态生成的SQL语句 
 22大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strTmp nvarchar(1000)        ----存放取得查询结果总数的查询语句 
 23大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strID     nvarchar(1000)        ----存放取得查询开头或结尾ID的查询语句 
 24大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 25大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strSortType nvarchar(10)    ----数据排序规则A 
 26大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @strFSortType nvarchar(10)    ----数据排序规则B 
 27大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 28大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @SqlSelect nvarchar(50)         ----对含有DISTINCT的查询进行SQL构造 
 29大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 Declare @SqlCounts nvarchar(50)          ----对含有DISTINCT的总数查询进行SQL构造 
 30大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 31大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 declare @timediff datetime  --耗时测试时间差 
 32大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 select @timediff=getdate() 
 33大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 34大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @Dist  = 0 
 35大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 36大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlSelect = 'select ' 
 37大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlCounts = 'Count(*)' 
 38大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 39大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 40大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 41大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlSelect = 'select distinct ' 
 42大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @SqlCounts = 'Count(DISTINCT '+@ID+')' 
 43大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 44大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 45大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 46大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @Sort=0 
 47大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 48大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strFSortType=' ASC ' 
 49大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strSortType=' DESC ' 
 50大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 51大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 52大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 53大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strFSortType=' DESC ' 
 54大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strSortType=' ASC ' 
 55大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 56大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 57大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 58大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 59大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --------生成查询语句-------- 
 60大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --此处@strTmp为取得查询结果数量的语句 
 61大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @strCondition is null or @strCondition=''     --没有设置显示条件 
 62大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 63大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @sqlTmp =  @fldName + ' From ' + @tblName 
 64大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName 
 65大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strID = ' From ' + @tblName 
 66大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 67大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 68大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 begin 
 69大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition 
 70大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strTmp = @SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName + ' where (1>0) ' + @strCondition 
 71大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition 
 72大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 end 
 73大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 74大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 ----取得查询结果总数量----- 
 75大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 exec sp_executesql @strTmp,N'@Counts int out ',@Counts out 
 76大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 declare @tmpCounts int 
 77大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 if @Counts = 0 
 78大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @tmpCounts = 1 
 79大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 else 
 80大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @tmpCounts = @Counts 
 81大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 82大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     --取得分页总数 
 83大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @pageCount=(@tmpCounts+@pageSize-1)/@pageSize 
 84大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 85    /**//**当前页大于总页数 取最后一页**/ 
 86大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     if @page>@pageCount 
 87大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         set @page=@pageCount 
 88大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 89大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     --/*-----数据分页2分处理-------*/ 
 90大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @pageIndex int --总数/页大小 
 91大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     declare @lastcount int --总数%页大小  
 92大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
 93大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @pageIndex = @tmpCounts/@pageSize 
 94大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     set @lastcount = @tmpCounts%@pageSize 
 95大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     if @lastcount > 0 
 96大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         set @pageIndex = @pageIndex + 1 
 97大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     else 
 98大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         set @lastcount = @pagesize 
 99大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
100大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     --//***显示分页 
101大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     if @strCondition is null or @strCondition=''     --没有设置显示条件 
102大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     begin 
103大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理 
104大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             begin  
105大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 if @page=1 
106大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName                         
107大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType 
108大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else 
109大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 begin 
110大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     if @Sort=1 
111大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin                     
112大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
113大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
114大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
115大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType 
116大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
117大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     else 
118大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin 
119大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
120大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
121大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
122大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strFSortType  
123大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
124大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 end     
125大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             end 
126大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         else 
127大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             begin 
128大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             set @page = @pageIndex-@page+1 --后半部分数据处理 
129大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 if @page <= 1 --最后一页数据显示                 
130大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
131大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType  
132大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else 
133大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     if @Sort=1 
134大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin 
135大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
136大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
137大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
138大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType 
139大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
140大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     else 
141大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     begin 
142大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
143大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
144大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
145大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType  
146大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     end 
147大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             end 
148大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     end 
149大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
150大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     else --有查询条件 
151大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     begin 
152大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         if @pageIndex<2 or @page<=@pageIndex / 2 + @pageIndex % 2   --前半部分数据处理 
153大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         begin 
154大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 if @page=1 
155大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName                         
156大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where 1=1 ' + @strCondition + ' order by '+ @fldSort +' '+ @strFSortType 
157大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else if(@Sort=1) 
158大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 begin                     
159大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
160大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
161大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
162大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType 
163大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 end 
164大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 else 
165大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 begin 
166大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
167大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from ('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-1) as Varchar(20)) +' '+ @ID +' from '+@tblName 
168大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) ' + @strCondition +' order by '+ @fldSort +' '+ @strFSortType+') AS TBMinID)' 
169大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition +' order by '+ @fldSort +' '+ @strFSortType  
170大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                 end            
171大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         end 
172大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         else 
173大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         begin  
174大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             set @page = @pageIndex-@page+1 --后半部分数据处理 
175大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             if @page <= 1 --最后一页数据显示 
176大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@lastcount as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
177大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType                      
178大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             else if(@Sort=1) 
179大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
180大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' >(select max('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
181大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
182大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType     
183大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
             else 
184大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                     set @strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+ CAST(@pageSize as VARCHAR(4))+' '+ @fldName+' from '+@tblName 
185大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where '+@ID+' <(select min('+ @ID +') from('+ @SqlSelect+' top '+ CAST(@pageSize*(@page-2)+@lastcount as Varchar(20)) +' '+ @ID +' from '+@tblName 
186大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' where (1=1) '+ @strCondition +' order by '+ @fldSort +' '+ @strSortType+') AS TBMaxID)' 
187大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
                         +' '+ @strCondition+' order by '+ @fldSort +' '+ @strSortType+') AS TempTB'+' order by '+ @fldSort +' '+ @strFSortType             
188大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
         end     
189大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
     end 
190大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
191大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 ------返回查询结果----- 
192大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 exec sp_executesql @strTmp 
193大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 select datediff(ms,@timediff,getdate()) as 耗时 
194大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 --print @strTmp 
195大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 SET NOCOUNT OFF 
196大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 GO
197大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)
 
大数据量分页存储过程效率测试附代码(转http://www.cnblogs.com/lli0077/archive/2008/09/03/1282862.html)