SQL Server代码
GO
use data_Test
GO
create table tb_TestTable --创建表
(
id int identity(1,1) primary key ,
userName nvarchar(20) not null,
userPWD nvarchar(20) not null,
userEmail nvarchar(40) null
)
GO
然后我们在数据表中插入2000000条数据:
-- 插入数据
set identity_insert tb_TestTable on
declare @count int
set @ count=1
而@count<=2000000
开始
插入到 tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn')
set @count=@计数+1
结束
set identity_insert tb_TestTable off
我首先写了五个常用存储过程如下:
1,利用select top 和select not in进行分页,具体代码:
create procedure proc_paged_with_notin --利用select top and select not in
(
@pageIndex int, --页索引
@pageSize int --每页记录数
)
as
begin
set nocount on;
声明@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 超时
设置无计数;
结尾
2、利用select top 和select max(列键)
create procedure proc_paged_with_selectMax --利用select top and select max(列)
(
@pageIndex int, --页索引
@pageSize int --页记录数
)
as
begin
set nocount on;
声明@timediff datetime
声明@sql nvarchar(500)
select @timediff=Getdate()
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'
execute(@sql)
select datediff(ms,@timediff,GetDate()) as 耗时
set nocount离开;
end
3、利用选择顶尖和中间人--此方法因网上有人说最好的效果,所以贴出来一起测试
create procedure proc_paged_with_Midvar --利用ID>最大ID值和中间变量
(
@pageIndex int,
@pageSize int
)
as
declare @count int
declare @ID int
declare @timediff datetime
declare @sql nvarchar(500)
begin
set nocount on;
select @count=0,@ID=0,@timediff=getdate()
select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id
set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID)
execute(@sql)
select datediff(ms,@timediff,getdate()) 作为耗时
取消计数;
结束
4,利用ROW_NUMBER()此方法为SQL服务器2005年中新的方法,利用ROW_NUMBER()给数据行加上索引
create procedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number()
(
@pageIndex int,
@pageSize int
)
as
declare @timediff datetime
begin
set nocount on;
select @timediff=getdate()
select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber 其中 IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1 )
选择 datediff(ms,@timediff,getdate()) 作为持续时间
set nocount off;
端
5,利用临时表及ROW_NUMBER
创建过程proc_CTE -利用临时表及ROW_NUMBER
(
@pageIndex INT, -页索引
@pageSize INT -页记录数
)
为
上组NOCOUNT;
声明@ctestr nvarchar(400)
声明@strSql nvarchar(400)
声明@datediff 日期时间
开始
选择@datediff=GetDate()
设置@ctestr='with Table_CTE as
(选择天花板((Row_number() over(order by ID ASC))) /'+str(@pageSize)+') as page_num,* from tb_TestTable)';
set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex)
end
begin
execute sp_executesql @strSql
select datediff(ms,@datediff,GetDate())
set nocount off;
end