1--插入数据 2setidentity_insert tb_TestTable on 3declare@countint 4set@count=1 5while@count<=2000000 6begin 7insertinto tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','lli0077@yahoo.com.cn') 8set@count=@count+1 9end 10setidentity_insert tb_TestTable off
我首先写了五个常用存储过程:
1,利用select top 和select not in进行分页,具体代码如下:
1createprocedure proc_paged_with_notin --利用select top and select not in 2( 3@pageIndexint, --页索引 4@pageSizeint--每页记录数 5) 6as 7begin 8set nocount on; 9declare@timediffdatetime--耗时 10declare@sqlnvarchar(500) 11select@timediff=Getdate() 12set@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' 13execute(@sql) --因select top后不支技直接接参数,所以写成了字符串@sql 14selectdatediff(ms,@timediff,GetDate()) as 耗时 15set nocount off; 16end
2,利用select top 和 select max(列键)
1createprocedure proc_paged_with_selectMax --利用select top and select max(列) 2( 3@pageIndexint, --页索引 4@pageSizeint--页记录数 5) 6as 7begin 8set nocount on; 9declare@timediffdatetime 10declare@sqlnvarchar(500) 11select@timediff=Getdate() 12set@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' 13execute(@sql) 14selectdatediff(ms,@timediff,GetDate()) as 耗时 15set nocount off; 16end
3,利用select top和中间变量--此方法因网上有人说效果最佳,所以贴出来一同测试
1createprocedure proc_paged_with_Midvar --利用ID>最大ID值和中间变量 2( 3@pageIndexint, 4@pageSizeint 5) 6as 7declare@countint 8declare@IDint 9declare@timediffdatetime 10declare@sqlnvarchar(500) 11begin 12set nocount on; 13select@count=0,@ID=0,@timediff=getdate() 14select@count=@count+1,@ID=casewhen@count<=@pageSize*@pageIndexthen ID else@IDendfrom tb_testTable orderby id 15set@sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID) 16execute(@sql) 17selectdatediff(ms,@timediff,getdate()) as 耗时 18set nocount off; 19end 20
4,利用Row_number() 此方法为SQL server 2005中新的方法,利用Row_number()给数据行加上索引
1createprocedure proc_paged_with_Rownumber --利用SQL 2005中的Row_number() 2( 3@pageIndexint, 4@pageSizeint 5) 6as 7declare@timediffdatetime 8begin 9set nocount on; 10select@timediff=getdate() 11select*from (select*,Row_number() over(orderby ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndexand IDRank<@pageSize*(@pageIndex+1) 12selectdatediff(ms,@timediff,getdate()) as 耗时 13set nocount off; 14end 15
5,利用临时表及Row_number
1createprocedure proc_CTE --利用临时表及Row_number 2( 3@pageIndexint, --页索引 4@pageSizeint--页记录数 5) 6as 7set nocount on; 8declare@ctestrnvarchar(400) 9declare@strSqlnvarchar(400) 10declare@datediffdatetime 11begin 12select@datediff=GetDate() 13set@ctestr='with Table_CTE as 14 (select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)'; 15set@strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex) 16end 17begin 18execute sp_executesql @strSql 19selectdatediff(ms,@datediff,GetDate()) 20set nocount off; 21end 22
1--/*-----存储过程 分页处理 孙伟 2005-03-28创建 -------*/ 2--/*-----存储过程 分页处理 浪尘 2008-9-1修改----------*/ 3--/*----- 对数据进行了2分处理使查询前半部分数据与查询后半部分数据性能相同 -------*/ 4 5alterPROCEDURE proc_paged_2part_selectMax 6( 7@tblNamenvarchar(200), ----要显示的表或多个表的连接 8@fldNamenvarchar(500) ='*', ----要显示的字段列表 9@pageSizeint=10, ----每页显示的记录个数 10@pageint=1, ----要显示那一页的记录 11@fldSortnvarchar(200) =null, ----排序字段列表或条件 12@Sortbit=0, ----排序方法,0为升序,1为降序(如果是多字段排列Sort指代最后一个排序字段的排列顺序(最后一个排序字段不加排序标记)--程序传参如:' SortA Asc,SortB Desc,SortC ') 13@strConditionnvarchar(1000) =null, ----查询条件,不需where 14@IDnvarchar(150), ----主表的主键 15@Distbit=0, ----是否添加查询字段的 DISTINCT 默认0不添加/1添加 16@pageCountint=1 output, ----查询结果分页后的总页数 17@Countsint=1 output ----查询到的记录数 18) 19AS 20SET NOCOUNT ON 21Declare@sqlTmpnvarchar(1000) ----存放动态生成的SQL语句 22Declare@strTmpnvarchar(1000) ----存放取得查询结果总数的查询语句 23Declare@strIDnvarchar(1000) ----存放取得查询开头或结尾ID的查询语句 24 25Declare@strSortTypenvarchar(10) ----数据排序规则A 26Declare@strFSortTypenvarchar(10) ----数据排序规则B 27 28Declare@SqlSelectnvarchar(50) ----对含有DISTINCT的查询进行SQL构造 29Declare@SqlCountsnvarchar(50) ----对含有DISTINCT的总数查询进行SQL构造 30 31declare@timediffdatetime--耗时测试时间差 32select@timediff=getdate() 33 34if@Dist=0 35begin 36set@SqlSelect='select ' 37set@SqlCounts='Count(*)' 38end 39else 40begin 41set@SqlSelect='select distinct ' 42set@SqlCounts='Count(DISTINCT '+@ID+')' 43end 44 45 46if@Sort=0 47begin 48set@strFSortType=' ASC ' 49set@strSortType=' DESC ' 50end 51else 52begin 53set@strFSortType=' DESC ' 54set@strSortType=' ASC ' 55end 56 57 58 59--------生成查询语句-------- 60--此处@strTmp为取得查询结果数量的语句 61if@strConditionisnullor@strCondition=''--没有设置显示条件 62begin 63set@sqlTmp=@fldName+' From '+@tblName 64set@strTmp=@SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName 65set@strID=' From '+@tblName 66end 67else 68begin 69set@sqlTmp=+@fldName+'From '+@tblName+' where (1>0) '+@strCondition 70set@strTmp=@SqlSelect+' @Counts='+@SqlCounts+' FROM '+@tblName+' where (1>0) '+@strCondition 71set@strID=' From '+@tblName+' where (1>0) '+@strCondition 72end 73 74----取得查询结果总数量----- 75exec sp_executesql @strTmp,N'@Counts int out ',@Counts out 76declare@tmpCountsint 77if@Counts=0 78set@tmpCounts=1 79else 80set@tmpCounts=@Counts 81 82--取得分页总数 83set@pageCount=(@tmpCounts+@pageSize-1)/@pageSize 84 85/**//**//**//**当前页大于总页数 取最后一页**/ 86if@page>@pageCount 87set@page=@pageCount 88 89--/*-----数据分页2分处理-------*/ 90declare@pageIndexint--总数/页大小 91declare@lastcountint--总数%页大小 92 93set@pageIndex=@tmpCounts/@pageSize 94set@lastcount=@tmpCounts%@pageSize 95if@lastcount>0 96set@pageIndex=@pageIndex+1 97else 98set@lastcount=@pagesize 99 100--//***显示分页 101if@strConditionisnullor@strCondition=''--没有设置显示条件 102begin 103if@pageIndex<2or@page<=@pageIndex/2+@pageIndex%2--前半部分数据处理 104begin 105if@page=1 106set@strTmp=@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 107+' order by '+@fldSort+''+@strFSortType 108else 109begin 110if@Sort=1 111begin 112set@strTmp=@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 113+' where '+@ID+' <(select min('+@ID+') from ('+@SqlSelect+' top '+CAST(@pageSize*(@page-1) asVarchar(20)) +''+@ID+' from '+@tblName 114+' order by '+@fldSort+''+@strFSortType+') AS TBMinID)' 115+' order by '+@fldSort+''+@strFSortType 116end 117else 118begin 119set@strTmp=@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 120+' where '+@ID+' >(select max('+@ID+') from ('+@SqlSelect+' top '+CAST(@pageSize*(@page-1) asVarchar(20)) +''+@ID+' from '+@tblName 121+' order by '+@fldSort+''+@strFSortType+') AS TBMinID)' 122+' order by '+@fldSort+''+@strFSortType 123end 124end 125end 126else 127begin 128set@page=@pageIndex-@page+1--后半部分数据处理 129if@page<=1--最后一页数据显示 130set@strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+CAST(@lastcountasVARCHAR(4))+''+@fldName+' from '+@tblName 131+' order by '+@fldSort+''+@strSortType+') AS TempTB'+' order by '+@fldSort+''+@strFSortType 132else 133if@Sort=1 134begin 135set@strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 136+' where '+@ID+' >(select max('+@ID+') from('+@SqlSelect+' top '+CAST(@pageSize*(@page-2)+@lastcountasVarchar(20)) +''+@ID+' from '+@tblName 137+' order by '+@fldSort+''+@strSortType+') AS TBMaxID)' 138+' order by '+@fldSort+''+@strSortType+') AS TempTB'+' order by '+@fldSort+''+@strFSortType 139end 140else 141begin 142set@strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 143+' where '+@ID+' <(select min('+@ID+') from('+@SqlSelect+' top '+CAST(@pageSize*(@page-2)+@lastcountasVarchar(20)) +''+@ID+' from '+@tblName 144+' order by '+@fldSort+''+@strSortType+') AS TBMaxID)' 145+' order by '+@fldSort+''+@strSortType+') AS TempTB'+' order by '+@fldSort+''+@strFSortType 146end 147end 148end 149 150else--有查询条件 151begin 152if@pageIndex<2or@page<=@pageIndex/2+@pageIndex%2--前半部分数据处理 153begin 154if@page=1 155set@strTmp=@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 156+' where 1=1 '+@strCondition+' order by '+@fldSort+''+@strFSortType 157elseif(@Sort=1) 158begin 159set@strTmp=@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 160+' where '+@ID+' <(select min('+@ID+') from ('+@SqlSelect+' top '+CAST(@pageSize*(@page-1) asVarchar(20)) +''+@ID+' from '+@tblName 161+' where (1=1) '+@strCondition+' order by '+@fldSort+''+@strFSortType+') AS TBMinID)' 162+''+@strCondition+' order by '+@fldSort+''+@strFSortType 163end 164else 165begin 166set@strTmp=@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 167+' where '+@ID+' >(select max('+@ID+') from ('+@SqlSelect+' top '+CAST(@pageSize*(@page-1) asVarchar(20)) +''+@ID+' from '+@tblName 168+' where (1=1) '+@strCondition+' order by '+@fldSort+''+@strFSortType+') AS TBMinID)' 169+''+@strCondition+' order by '+@fldSort+''+@strFSortType 170end 171end 172else 173begin 174set@page=@pageIndex-@page+1--后半部分数据处理 175if@page<=1--最后一页数据显示 176set@strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+CAST(@lastcountasVARCHAR(4))+''+@fldName+' from '+@tblName 177+' where (1=1) '+@strCondition+' order by '+@fldSort+''+@strSortType+') AS TempTB'+' order by '+@fldSort+''+@strFSortType 178elseif(@Sort=1) 179set@strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 180+' where '+@ID+' >(select max('+@ID+') from('+@SqlSelect+' top '+CAST(@pageSize*(@page-2)+@lastcountasVarchar(20)) +''+@ID+' from '+@tblName 181+' where (1=1) '+@strCondition+' order by '+@fldSort+''+@strSortType+') AS TBMaxID)' 182+''+@strCondition+' order by '+@fldSort+''+@strSortType+') AS TempTB'+' order by '+@fldSort+''+@strFSortType 183else 184set@strTmp=@SqlSelect+' * from ('+@SqlSelect+' top '+CAST(@pageSizeasVARCHAR(4))+''+@fldName+' from '+@tblName 185+' where '+@ID+' <(select min('+@ID+') from('+@SqlSelect+' top '+CAST(@pageSize*(@page-2)+@lastcountasVarchar(20)) +''+@ID+' from '+@tblName 186+' where (1=1) '+@strCondition+' order by '+@fldSort+''+@strSortType+') AS TBMaxID)' 187+''+@strCondition+' order by '+@fldSort+''+@strSortType+') AS TempTB'+' order by '+@fldSort+''+@strFSortType 188end 189end 190 191------返回查询结果----- 192exec sp_executesql @strTmp 193selectdatediff(ms,@timediff,getdate()) as 耗时 194--print @strTmp 195SET NOCOUNT OFF 196GO 197