天涯之外

导航

高效分页存储过程

分页存储过程很多,高效的没有几个:
------------------------------------
--用途:分页存储过程(对有主键的表效率极高) 
--说明:
------------------------------------

CREATE PROCEDURE UP_GetRecordByPage
    @tblName      varchar(255),       -- 表名
    @fldName      varchar(255),       -- 主键字段名
    @PageSize     int = 10,           -- 页尺寸
    @PageIndex    int = 1,            -- 页码
    @IsReCount    bit = 0,            -- 返回记录总数, 非 0 值则返回
    @OrderType    bit = 0,            -- 设置排序类型, 非 0 值则降序
    @strWhere     varchar(1000) = ''  -- 查询条件 (注意: 不要加 where)
AS

declare @strSQL   varchar(6000)       -- 主语句
declare @strTmp   varchar(100)        -- 临时变量
declare @strOrder varchar(400)        -- 排序类型

if @OrderType != 0
begin
    set @strTmp = '<(select min'
    set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
    set @strTmp = '>(select max'
    set @strOrder = ' order by [' + @fldName +'] asc'
end

set @strSQL = 'select top ' + str(@PageSize) + ' * 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) + ' * from ['
        + @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
        + @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
        + @fldName + '] from [' + @tblName + '] where ' + @strWhere + ' '
        + @strOrder + ') as tblTmp) and ' + @strWhere + ' ' + @strOrder

if @PageIndex = 1
begin
    set @strTmp =''
    if @strWhere != ''
        set @strTmp = ' where ' + @strWhere

    set @strSQL = 'select top ' + str(@PageSize) + ' * from ['
        + @tblName + ']' + @strTmp + ' ' + @strOrder
end

if @IsReCount != 0
    set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+' where ' + @strWhere

exec (@strSQL)

GO

前几天又有一个新的分页存储,据说要比我现在(不是上面那个)用的分页存储过程的效率要高,我比较怀疑,但是没有测试。一并贴出来

CREATE PROCEDURE WapWapIndexGetRows
(
  @PageIndex int,
  @PageSize int,
  @RecordCount int out,
  @PageCount int out,
   @type  int
)
 AS
declare @sqlstr nvarchar (1000)
 select @RecordCount = count(*) from WapIndex where Mediatype=@type
set @PageCount = ceiling(@RecordCount*1.0/@PageSize)
if(@PageIndex=0 or @PageCount<=1)
  set @sqlstr = ' select top ' + str(@PageSize) +
                        ' * from WapIndex where mediatype= '+str(@type) +' order by addtime desc '
else if(@PageIndex = @PageCount - 1)
         set @sqlstr = ' select * from (select top  ' + str(@RecordCount-@PageSize*@PageIndex)+
                             ' * from WapIndex where mediaType = '+ str(@type)+' order by id asc ) TmepTable order by addtime desc'
else
  set @sqlstr = ' select top ' + str(@PageSize) + ' * from (select top '+ str(@RecordCount-@PageSize*@PageIndex) +
  '* from WapIndex where Mediatype = '+ str(@type) +  ' order by ID asc) TempTable  order by addtime desc'
exec(@sqlstr)

GO

posted on 2009-05-23 14:37  天涯之外  阅读(434)  评论(0编辑  收藏  举报