高效的SQLSERVER分页查询(推荐)

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID、YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2

第一种方案、最简单、普通的方法:

代码如下:

[sql] view plaincopyprint?

  1. SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000 ID FROM ARTICLE ORDER BY YEAR DESC, ID DESC) ORDER BY YEAR DESC,ID DESC
SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000 ID FROM ARTICLE ORDER BY YEAR DESC, ID DESC) ORDER BY YEAR DESC,ID DESC

平均查询100次所需时间:45s

第二种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT * FROM (  SELECT TOP 30 * FROM (SELECT TOP 45030 * FROM ARTICLE ORDER BY YEAR DESC, ID DESC) f ORDER BY f.YEAR ASC, f.ID DESC) s ORDER BY s.YEAR DESC,s.ID DESC
SELECT * FROM (  SELECT TOP 30 * FROM (SELECT TOP 45030 * FROM ARTICLE ORDER BY YEAR DESC, ID DESC) f ORDER BY f.YEAR ASC, f.ID DESC) s ORDER BY s.YEAR DESC,s.ID DESC 

平均查询100次所需时间:138S

第三种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT * FROM ARTICLE w1,
  2. (
  3. SELECT TOP 30 ID FROM
  4. (
  5. SELECT TOP 50030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
  6. ) w ORDER BY w.YEAR ASC, w.ID ASC
  7. ) w2 WHERE w1.ID = w2.ID ORDER BY w1.YEAR DESC, w1.ID DESC
SELECT * FROM ARTICLE w1,
(
    SELECT TOP 30 ID FROM
    (
        SELECT TOP 50030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
    ) w ORDER BY w.YEAR ASC, w.ID ASC
) w2 WHERE w1.ID = w2.ID ORDER BY w1.YEAR DESC, w1.ID DESC

平均查询100次所需时间:21S

第四种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT * FROM ARTICLE w1
  2. WHERE ID in
  3. (
  4. SELECT top 30 ID FROM
  5. (
  6. SELECT top 45030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
  7. ) w ORDER BY w.YEAR ASC, w.ID ASC
  8. )
  9. ORDER BY w1.YEAR DESC, w1.ID DESC
SELECT * FROM ARTICLE w1
    WHERE ID in
        (
            SELECT top 30 ID FROM
            (
                SELECT top 45030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
            ) w ORDER BY w.YEAR ASC, w.ID ASC
        )
    ORDER BY w1.YEAR DESC, w1.ID DESC

平均查询100次所需时间:20S

第五种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT w2.n, w1.* FROM ARTICLE w1, (  SELECT TOP 50030 row_number() OVER (ORDER BY YEAR DESC, ID DESC) n, ID FROM ARTICLE ) w2 WHERE w1.ID = w2.ID AND w2.n > 50000 ORDER BY w2.n ASC
SELECT w2.n, w1.* FROM ARTICLE w1, (  SELECT TOP 50030 row_number() OVER (ORDER BY YEAR DESC, ID DESC) n, ID FROM ARTICLE ) w2 WHERE w1.ID = w2.ID AND w2.n > 50000 ORDER BY w2.n ASC 

平均查询100次所需时间:15S

查询第1000-1030条记录

第一种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 1000 ID FROM ARTICLE ORDER BY YEAR DESC, ID DESC) ORDER BY YEAR DESC,ID DESC
SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 1000 ID FROM ARTICLE ORDER BY YEAR DESC, ID DESC) ORDER BY YEAR DESC,ID DESC 

平均查询100次所需时间:80s

第二种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT * FROM  (   SELECT TOP 30 * FROM (SELECT TOP 1030 * FROM ARTICLE ORDER BY YEAR DESC, ID DESC) f ORDER BY f.YEAR ASC, f.ID DESC) s ORDER BY s.YEAR DESC,s.ID DESC
SELECT * FROM  (   SELECT TOP 30 * FROM (SELECT TOP 1030 * FROM ARTICLE ORDER BY YEAR DESC, ID DESC) f ORDER BY f.YEAR ASC, f.ID DESC) s ORDER BY s.YEAR DESC,s.ID DESC 

平均查询100次所需时间:30S

第三种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT * FROM ARTICLE w1,
  2. (
  3. SELECT TOP 30 ID FROM
  4. (
  5. SELECT TOP 1030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
  6. ) w ORDER BY w.YEAR ASC, w.ID ASC
  7. ) w2 WHERE w1.ID = w2.ID ORDER BY w1.YEAR DESC, w1.ID DESC
SELECT * FROM ARTICLE w1,
(
    SELECT TOP 30 ID FROM
    (
        SELECT TOP 1030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
    ) w ORDER BY w.YEAR ASC, w.ID ASC
) w2 WHERE w1.ID = w2.ID ORDER BY w1.YEAR DESC, w1.ID DESC

平均查询100次所需时间:12S

第四种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT * FROM ARTICLE w1
  2. WHERE ID in
  3. (
  4. SELECT top 30 ID FROM
  5. (
  6. SELECT top 1030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
  7. ) w ORDER BY w.YEAR ASC, w.ID ASC
  8. )
  9. ORDER BY w1.YEAR DESC, w1.ID DESC
SELECT * FROM ARTICLE w1
    WHERE ID in
        (
            SELECT top 30 ID FROM
            (
                SELECT top 1030 ID, YEAR FROM ARTICLE ORDER BY YEAR DESC, ID DESC
            ) w ORDER BY w.YEAR ASC, w.ID ASC
        )
    ORDER BY w1.YEAR DESC, w1.ID DESC

平均查询100次所需时间:13S

第五种方案:

代码如下:

[sql] view plaincopyprint?

  1. SELECT w2.n, w1.* FROM ARTICLE w1,(   SELECT TOP 1030 row_number() OVER (ORDER BY YEAR DESC, ID DESC) n, ID FROM ARTICLE) w2 WHERE w1.ID = w2.ID AND w2.n > 1000 ORDER BY w2.n ASC
SELECT w2.n, w1.* FROM ARTICLE w1,(   SELECT TOP 1030 row_number() OVER (ORDER BY YEAR DESC, ID DESC) n, ID FROM ARTICLE) w2 WHERE w1.ID = w2.ID AND w2.n > 1000 ORDER BY w2.n ASC 

平均查询100次所需时间:14S

由此可见在查询页数靠前时,效率3>4>5>2>1,页码靠后时5>4>3>1>2,再根据用户习惯,一般用户的检索只看最前面几页,因此选择3 4 5方案均可,若综合考虑方案5是最好的选择,但是要注意SQL2000不支持row_number()函数,由于时间和条件的限制没有做更深入、范围更广的测试,有兴趣的可以仔细研究下。

以下是根据第四种方案编写的一个分页存储过程:

代码如下:

[sql] view plaincopyprint?

  1. if exists (select * from dbo.sysobjects where id = object_id(N‘[dbo].[sys_Page_v2]‘) and OBJECTPROPERTY(id, N‘IsProcedure‘) = 1)
  2. drop procedure [dbo].[sys_Page_v2]
  3. GO
  4. CREATE PROCEDURE [dbo].[sys_Page_v2]
  5. @PCount int output,    --总页数输出
  6. @RCount int output,    --总记录数输出
  7. @sys_Table nvarchar(100),    --查询表名
  8. @sys_Key varchar(50),        --主键
  9. @sys_Fields nvarchar(500),    --查询字段
  10. @sys_Where nvarchar(3000),    --查询条件
  11. @sys_Order nvarchar(100),    --排序字段
  12. @sys_Begin int,        --开始位置
  13. @sys_PageIndex int,        --当前页数
  14. @sys_PageSize int        --页大小
  15. AS
  16. SET NOCOUNT ON
  17. SET ANSI_WARNINGS ON
  18. IF @sys_PageSize < 0 OR @sys_PageIndex < 0
  19. BEGIN
  20. RETURN
  21. END
  22. DECLARE @new_where1 NVARCHAR(3000)
  23. DECLARE @new_order1 NVARCHAR(100)
  24. DECLARE @new_order2 NVARCHAR(100)
  25. DECLARE @Sql NVARCHAR(4000)
  26. DECLARE @SqlCount NVARCHAR(4000)
  27. DECLARE @Top int
  28. if(@sys_Begin <=0)
  29. set @sys_Begin=0
  30. else
  31. set @[email protected]_Begin-1
  32. IF ISNULL(@sys_Where,‘‘) = ‘‘
  33. SET @new_where1 = ‘ ‘
  34. ELSE
  35. SET @new_where1 = ‘ WHERE ‘ + @sys_Where
  36. IF ISNULL(@sys_Order,‘‘) <> ‘‘
  37. BEGIN
  38. SET @new_order1 = ‘ ORDER BY ‘ + Replace(@sys_Order,‘desc‘,‘‘)
  39. SET @new_order1 = Replace(@new_order1,‘asc‘,‘desc‘)
  40. SET @new_order2 = ‘ ORDER BY ‘ + @sys_Order
  41. END
  42. ELSE
  43. BEGIN
  44. SET @new_order1 = ‘ ORDER BY ID DESC‘
  45. SET @new_order2 = ‘ ORDER BY ID ASC‘
  46. END
  47. SET @SqlCount = ‘SELECT @RCount=COUNT(1),@PCount=CEILING((COUNT(1)+0.0)/‘
  48. + CAST(@sys_PageSize AS NVARCHAR)+‘) FROM ‘ + @sys_Table + @new_where1
  49. EXEC SP_EXECUTESQL @SqlCount,N‘@RCount INT OUTPUT,@PCount INT OUTPUT‘,
  50. @RCount OUTPUT,@PCount OUTPUT
  51. IF @sys_PageIndex > CEILING((@RCount+0.0)/@sys_PageSize)    --如果输入的当前页数大于实际总页数,则把实际总页数赋值给当前页数
  52. BEGIN
  53. SET @sys_PageIndex =  CEILING((@RCount+0.0)/@sys_PageSize)
  54. END
  55. set @sql = ‘select ‘+ @sys_fields +‘ from ‘ + @sys_Table + ‘ w1 ‘
  56. + ‘ where ‘+ @sys_Key +‘ in (‘
  57. +‘select top ‘+ ltrim(str(@sys_PageSize)) +‘ ‘ + @sys_Key + ‘ from ‘
  58. +‘(‘
  59. +‘select top ‘ + ltrim(STR(@sys_PageSize * @sys_PageIndex + @sys_Begin)) + ‘ ‘ + @sys_Key + ‘ FROM ‘
  60. + @sys_Table + @new_where1 + @new_order2
  61. +‘) w ‘ + @new_order1
  62. +‘) ‘ + @new_order2
  63. print(@sql)
  64. Exec(@sql)
  65. GO
时间: 2024-10-17 09:17:16

高效的SQLSERVER分页查询(推荐)的相关文章

高效的SQLSERVER分页查询的几种示例分析

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案.最简单.普通的方法: 1 SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000

真正高效的SQLSERVER分页查询(多种方案)

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案.最简单.普通的方法: 复制代码代码如下: SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TO

高效的SQLSERVER分页查询(转载)

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案.最简单.普通的方法: SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000 ID

高效的SQLSERVER分页查询

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案.最简单.普通的方法: SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN (SELECT TOP 45000 I

SQLserver分页查询实例

Sqlserver数据库分页查询一直是Sqlserver的短板,闲来无事,想出几种方法,假设有表ARTICLE,字段ID.YEAR...(其他省略),数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案.最简单.普通的方法: SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000 ID

SQLServer分页查询存储过程

项目中用到的SQLServer分页查询存储过程. [存储过程] create PROCEDURE prcPageResult -- 获得某一页的数据 -- @currPage int = 1,                         --当前页页码 (即Top currPage) @showColumn varchar(2000) = '*',           --需要得到的字段 (即 column1,column2,......) @strCondition varchar(200

sqlserver分页查询

SQL Server 2005引入的新方法. SELECT * FROM (SELECT ROW_NUMBER() OVER(ORDER BY keyField DESC) AS rowNum, * FROM tableName) AS t WHERE rowNum > start AND rowNum <= end 其中: keyField为表tableName的一个字段(最好是主键): tableName为查询的表名: DESC可以按需换为ASC: start为要取的结果集的起始记录 en

高效的SQLSERVER分页方案

Sqlserver数据库分页查询一直是Sqlserver的短板,假设有表ARTICLE,字段ID.YEAR,数据53210条(客户真实数据,量不大),分页查询每页30条,查询第1500页(即第45001-45030条数据),字段ID聚集索引,YEAR无索引,Sqlserver版本:2008R2 第一种方案:SELECT TOP 30 * FROM ARTICLE WHERE ID NOT IN(SELECT TOP 45000 ID FROM ARTICLE ORDER BY YEAR DESC

sqlserver 分页查询总结

sqlserver2008不支持关键字limit ,所以它的分页sql查询语句将不能用mysql的方式进行,幸好sqlserver2008提供了top,rownumber等关键字,这样就能通过这几个关键字实现分页. 下面是本人在网上查阅到的几种查询脚本的写法: 几种sqlserver2008高效分页sql查询语句top方案:sql code: select top 10 * from table1where id not in(select top 开始的位置 id from table1) m