有人在用邹建的分页的TOP n 实现的通用分页存储过吗?
大数据量时候
EXEC sp_PageView 'bzzlxx','zlbs' ,222,100,'*','djh','1=1',22293页大小为100,显示第222页时
执行这句的时候就用了4秒页数越大消耗的时间就越长不知道其它的分页方式是不是也这样慢?我见别人系统显示大页数时最多也只用了2秒 ,不知道是怎么解决的
大数据量时候
EXEC sp_PageView 'bzzlxx','zlbs' ,222,100,'*','djh','1=1',22293页大小为100,显示第222页时
执行这句的时候就用了4秒页数越大消耗的时间就越长不知道其它的分页方式是不是也这样慢?我见别人系统显示大页数时最多也只用了2秒 ,不知道是怎么解决的
http://www.cnblogs.com/nzperfect/archive/2009/06/14/1503237.html
这个试试??
-- 获取指定页的数据CREATE PROCEDURE pagination3@tblName varchar(255), -- 表名@strGetFields varchar(1000) = '*', -- 需要返回的列 @fldName varchar(255)='', -- 排序的字段名@PageSize int = 10, -- 页尺寸@PageIndex int = 1, -- 页码@doCount bit = 0, -- 返回记录总数, 非 0 值则返回@OrderType bit = 0, -- 设置排序类型, 非 0 值则降序@strWhere varchar(1500) = '' -- 查询条件 (注意: 不要加 where)ASdeclare @strSQL varchar(5000) -- 主语句declare @strTmp varchar(110) -- 临时变量declare @strOrder varchar(400) -- 排序类型 if @doCount != 0 begin if @strWhere !='' set @strSQL = "select count(*) as Total from [" + @tblName + "] where "+@strWhere else set @strSQL = "select count(*) as Total from [" + @tblName + "]"end --以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况elsebegin if @OrderType != 0begin set @strTmp = "<(select min"set @strOrder = " order by [" + @fldName +"] desc"--如果@OrderType不是0,就执行降序,这句很重要!endelsebegin set @strTmp = ">(select max" set @strOrder = " order by [" + @fldName +"] asc"end if @PageIndex = 1begin if @strWhere != '' set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where " + @strWhere + " " + @strOrder else set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from ["+ @tblName + "] "+ @strOrder--如果是第一页就执行以上代码,这样会加快执行速度endelsebegin--以下代码赋予了@strSQL以真正执行的SQL代码set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where [" + @fldName + "]" + @strTmp + "(["+ @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " ["+ @fldName + "] from [" + @tblName + "]" + @strOrder + ") as tblTmp)"+ @strOrder if @strWhere != '' set @strSQL = "select top " + str(@PageSize) +" "+@strGetFields+ " from [" + @tblName + "] where [" + @fldName + "]" + @strTmp + "([" + @fldName + "]) from (select top " + str((@PageIndex-1)*@PageSize) + " [" + @fldName + "] from [" + @tblName + "] where " + @strWhere + " " + @strOrder + ") as tblTmp) and " + @strWhere + " " + @strOrderend end exec (@strSQL)GO上面的这个存储过程是一个通用的存储过程,其注释已写在其中了。在大数据量的情况下,特别是在查询最后几页的时候,查询时间一般不会超过9秒;而用其他存储过程,在实践中就会导致超时,所以这个存储过程非常适用于大容量数据库的查询。笔者希望能够通过对以上存储过程的解析,能给大家带来一定的启示,并给工作带来一定的效率提升,同时希望同行提出更优秀的实时数据分页算法。