首页 > 代码库 > 常用的SQL分页算法及对比

常用的SQL分页算法及对比

SQL Server 2005引入的新方法。

1 SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY keyField DESC) AS rowNum, * FROM tableName) AS t WHERE rowNum > start[比如:90] AND rowNum <= end[比如:100]=>[返回91-100]2 3 SELECT top (PAGESIZE[比如:10]) FROM (SELECT ROW_NUMBER() OVER(ORDER BY keyField DESC) AS rowNum, * FROM tableName) AS t WHERE rowNum >(PAGEINDEX-1)*(PAGESIZE)[比如:90]4  =>[返回91-100]

其中:
keyField为表tableName的一个字段(最好是主键);
tableName为查询的表名;
DESC可以按需换为ASC;
start为要取的结果集的起始记录
end为要取的结果集的结尾记录,可由:(start + pageSize)计算得出。

一般方法:表中主键必须为标识列,[ID] int IDENTITY (1,1)
  建立表

 1 CREATE TABLE [TestTable] 2     ( 3       [ID] [int] IDENTITY(1, 1) 4                  PRIMARY KEY 5                  NOT NULL , 6       [FirstName] [nvarchar](100) NULL , 7       [LastName] [nvarchar](100) NULL , 8       [Country] [nvarchar](50) NULL , 9       [Note] [nvarchar](2000) NULL10     )11 GO

插入数据:(2万条,用更多的数据测试会明显一些)

SET IDENTITY_INSERT TestTable ONDECLARE @i INTSET @i = 1WHILE @i <= 20000     BEGIN        INSERT  INTO TestTable                ( [id] ,                  FirstName ,                  LastName ,                  Country ,                  Note                )        VALUES  ( @i ,                  FirstName_XXX ,                  LastName_XXX ,                  Country_XXX ,                  Note_XXX                )        SET @i = @i + 1    ENDSET IDENTITY_INSERT TestTable OFF

 1.分页方案一:(利用Not In和SELECT TOP分页)

语句形式:   

SELECT TOP 10 * FROM TestTable WHERE (ID NOT IN           (SELECT TOP 20 id          FROM TestTable          ORDER BY id)) ORDER BY ID SELECT TOP 页大小 * FROMWHERE (ID NOT IN           (SELECT TOP 页大小*页数 id          FROMORDER BY id)) ORDER BY ID

2.分页方案二:(利用ID大于多少和SELECT TOP分页)

语句形式:  

SELECT TOP 10 * FROM TestTable WHERE (ID >           (SELECT MAX(id)          FROM (SELECT TOP 20 id                  FROM TestTable                  ORDER BY id) AS T)) ORDER BY ID SELECT TOP 页大小 * FROMWHERE (ID >           (SELECT MAX(id)          FROM (SELECT TOP 页大小*页数 id                  FROMORDER BY id) AS T)) ORDER BY ID 

3.分页方案三:(利用SQL的游标存储过程分页)

CREATE  PROCEDURE SqlPager    @sqlstr NVARCHAR(4000) , --查询字符串    @currentpage INT , --第N页    @pagesize INT --每页行数AS     SET nocount ON    DECLARE @P1 INT , --P1是游标的id        @rowcount INT    EXEC sp_cursoropen @P1 OUTPUT, @sqlstr, @scrollopt = 1, @ccopt = 1,        @rowcount = @rowcount OUTPUT    SELECT  CEILING(1.0 * @rowcount / @pagesize) AS 总页数--,@rowcount as 总行数,@currentpage as 当前页     SET @currentpage = ( @currentpage - 1 ) * @pagesize + 1    EXEC sp_cursorfetch @P1, 16, @currentpage, @pagesize     EXEC sp_cursorclose @P1    SET nocount OFF


其它的方案:如果没有主键,可以用临时表,也可以用方案三做,但是效率会低。
建议优化的时候,加上主键和索引,查询效率会提高。

通过SQL 查询分析器,显示比较:我的结论是,
分页方案二:(利用ID大于多少和SELECT TOP分页)效率最高,需要拼接SQL语句
分页方案一:(利用Not In和SELECT TOP分页)   效率次之,需要拼接SQL语句
分页方案三:(利用SQL的游标存储过程分页)    效率最差,但是最为通用