利用存储过程实现分页
原文地址:http://dzsnowdrop.blog.163.com/blog/static/164932582201062792841678/
存储过程实现分页
1 -------------------------------------------------------以下代码为存储过程代码 2 3 CREATE procedure News_Class 4 5 --资讯新闻百万级分页 6 7 ( 8 9 @StrWhere varchar(100),--条件 10 11 @PageSize int, --页面大小 12 13 @PageIndex int --页面索引 14 15 ) 16 17 AS 18 19 declare @strSQL varchar(2000) -- 主语句 20 21 declare @strCountSQL varchar(2000) -- 总记录主语句 22 23 declare @strTmp varchar(1000) -- 临时变量 24 25 Set @strTmp =" Select top " + str(@PageSize) + " Title,AddTime from Tb_News " --此处注意,需几个字段读几个字段 26 27 if @StrWhere<>'' 28 29 Begin 30 31 Set @strSQL=@strTmp + " where ID < (select min(ID) from (select top " + str((@PageIndex-1)*@PageSize)+" ID from Tb_News Where "+@StrWhere+" order by ID desc) as tblTmp ) and "+@StrWhere+" order by ID desc" 32 33 set @strCountSQL="select count(ID) as countx from Tb_News Where "+@StrWhere+" " 34 35 End 36 37 else 38 39 Begin 40 41 Set @strSQL=@strTmp + " where ID < (select min(ID) from (select top " + str((@PageIndex-1)*@PageSize)+" ID from Tb_News order by ID desc) as tblTmp ) order by ID desc" 42 43 set @strCountSQL="select count(ID) as countx from Tb_News " 44 45 End 46 47 if @PageIndex = 1 48 49 if @StrWhere<>'' 50 51 Begin 52 53 Set @strSQL=@strTmp +" Where "+@StrWhere+" order by ID desc" 54 55 End 56 57 else 58 59 Begin 60 61 Set @strSQL=@strTmp +" order by ID desc" 62 63 End 64 65 exec (@strSQL) 66 67 exec (@strCountSQL) 68 69 GO
分页的几种方案,及思路
分页方案一:(利用Not In和SELECT TOP分页)
语句形式:
1 SELECT TOP 10 * 2 3 FROM TestTable 4 5 WHERE (ID NOT IN 6 7 (SELECT TOP 20 id 8 9 FROM TestTable 10 11 ORDER BY id)) 12 13 ORDER BY ID 14 15 SELECT TOP 页大小 * 16 17 FROM TestTable 18 19 WHERE (ID NOT IN 20 21 (SELECT TOP 页大小*页数 id 22 23 FROM 表 24 25 ORDER BY id)) 26 27 ORDER BY ID
-------------------------------------
分页方案二:(利用ID大于多少和SELECT TOP分页)
语句形式:
1 SELECT TOP 10 * 2 3 FROM TestTable 4 5 WHERE (ID > 6 7 (SELECT MAX(id) 8 9 FROM (SELECT TOP 20 id 10 11 FROM TestTable 12 13 ORDER BY id) AS T)) 14 15 ORDER BY ID 16 17 SELECT TOP 页大小 * 18 19 FROM TestTable 20 21 WHERE (ID > 22 23 (SELECT MAX(id) 24 25 FROM (SELECT TOP 页大小*页数 id 26 27 FROM 表 28 29 ORDER BY id) AS T)) 30 31 ORDER BY ID
-------------------------------------
分页方案三:(利用SQL的游标存储过程分页)
1 create procedure XiaoZhengGe 2 3 @sqlstr nvarchar(4000), --查询字符串 4 5 @currentpage int, --第N页 6 7 @pagesize int --每页行数 8 9 as 10 11 set nocount on 12 13 declare @P1 int, --P1是游标的id 14 15 @rowcount int 16 17 exec sp_cursoropen @P1 output,@sqlstr,@scrollopt=1,@ccopt=1,@rowcount=@rowcount output 18 19 select ceiling(1.0*@rowcount/@pagesize) as 总页数--,@rowcount as 总行数,@currentpage as 当前页 20 21 set @currentpage=(@currentpage-1)*@pagesize+1 22 23 exec sp_cursorfetch @P1,16,@currentpage,@pagesize 24 25 exec sp_cursorclose @P1 26 27 set nocount off
其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
建议优化的时候,加上主键和索引,查询效率会提高。
通过SQL 查询分析器,显示比较:结论是:
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页) 效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页) 效率最差,但是最为通用
漫漫人生,唯有激流勇进,不畏艰险,奋力拼搏,方能中流击水,抵达光明的彼岸