Home  >  Article  >  Database  >  分页查询 效率最高

分页查询 效率最高

WBOY
WBOYOriginal
2016-06-07 17:58:291048browse

给大家分享个效率最高的分页查询 5000万级别有效 比 ROWNUMBER 和Top效率高

代码如下:
/*

日期:2009-03-19
功能:根据各种条件获取 游戏国家任务 列表数据
*/
Create procedure [dbo].[PrGs_Nation_Task_GetList]

@PageSize int = 100, -- 每页显示记录条数,默认为100
@PageIndex int = 1, -- 当前提取要显示的页码,默认为1,数据库根据PageSize,PageIndex 计算返回一页数据

@RetTotal int output, -- 记录总数
@RetCount int output, -- 返回记录数
@RetPageIndex int output, -- 输出当前页码
@ReturnDesc varchar(128) output -- 返回操作结果描述
as
begin

set nocount on
set xact_abort on

set @RetTotal = 0
set @RetCount = 0
set @RetPageIndex = @PageIndex




-- 多条件取值
declare @Err int -- 错误
declare @PageCount int -- 总页数
declare @BeginRID int -- 开始行 Rid
declare @MaxRow int -- 最后行

select @RetTotal = count(*)
from NationTask

select @Err = @@ERROR

if @Err 0
begin
set @ReturnDesc = '提取国家任务总数失败!'
return -1
end

-- 如果无数据, 则返回空结果集
if @RetTotal = 0
begin
set @ReturnDesc = '当前条件无国家任务记录!'
return 1
end

-- 计算总页数
set @PageCount = @RetTotal / @PageSize

if @RetTotal % @PageSize > 0
begin
set @PageCount = @PageCount + 1
end

-- 超过总页数,则返回空结果集
if @PageIndex > @PageCount
begin
set @ReturnDesc = '当前条件无国家任务记录!'
return 1
end

-- 获取 要返回页面的 第一行纪录的 Rid
set @MaxRow = @PageSize * (@PageIndex - 1) + 1

set rowcount @MaxRow

select @BeginRID = Rid
from NationTask
order by Rid desc


-- 返回数据列表
set rowcount @PageSize

select Rid
,TaskName
,TaskTitle
,ImageID
,EffectID
,StartTime
from NationTask
where Rid order by Rid desc

set @RetCount = @@rowcount

-- 结束
set @ReturnDesc = '提取国家任务列表成功!'
return 1
end
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn