程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> MSSQL >> 高效的SQLSERVER分頁查詢(推舉)

高效的SQLSERVER分頁查詢(推舉)

編輯:MSSQL

高效的SQLSERVER分頁查詢(推舉)。本站提示廣大學習愛好者:(高效的SQLSERVER分頁查詢(推舉))文章只能為提供參考,不一定能成為您想要的結果。以下是高效的SQLSERVER分頁查詢(推舉)正文


第一種計劃、最簡略、通俗的辦法:


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

第二種計劃:


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

第三種計劃:


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

第四種計劃:

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

第五種計劃:


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筆記錄

第一種計劃:


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

第二種計劃:

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

第三種計劃:


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

第四種計劃:


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

第五種計劃:

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()函數,因為時光和前提的限制沒有做更深刻、規模更廣的測試,有興致的可以細心研討下。

以下是依據第四種計劃編寫的一個分頁存儲進程:


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sys_Page_v2]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sys_Page_v2]
GO

CREATE PROCEDURE [dbo].[sys_Page_v2]
@PCount int output,    --總頁數輸入
@RCount int output,    --總記載數輸入
@sys_Table nvarchar(100),    --查詢表名
@sys_Key varchar(50),        --主鍵
@sys_Fields nvarchar(500),    --查詢字段
@sys_Where nvarchar(3000),    --查詢前提
@sys_Order nvarchar(100),    --排序字段
@sys_Begin int,        --開端地位
@sys_PageIndex int,        --以後頁數
@sys_PageSize int        --頁年夜小
AS

SET NOCOUNT ON
SET ANSI_WARNINGS ON

IF @sys_PageSize < 0 OR @sys_PageIndex < 0
BEGIN       
RETURN
END

DECLARE @new_where1 NVARCHAR(3000)
DECLARE @new_order1 NVARCHAR(100)
DECLARE @new_order2 NVARCHAR(100)
DECLARE @Sql NVARCHAR(4000)
DECLARE @SqlCount NVARCHAR(4000)

DECLARE @Top int

if(@sys_Begin <=0)
    set @sys_Begin=0
else
    set @sys_Begin=@sys_Begin-1

IF ISNULL(@sys_Where,'') = ''
    SET @new_where1 = ' '
ELSE
    SET @new_where1 = ' WHERE ' + @sys_Where

IF ISNULL(@sys_Order,'') <> ''
BEGIN
    SET @new_order1 = ' ORDER BY ' + WordStr(@sys_Order,'desc','')
    SET @new_order1 = WordStr(@new_order1,'asc','desc')

    SET @new_order2 = ' ORDER BY ' + @sys_Order
END
ELSE
BEGIN
    SET @new_order1 = ' ORDER BY ID DESC'
    SET @new_order2 = ' ORDER BY ID ASC'
END

SET @SqlCount = 'SELECT @RCount=COUNT(1),@PCount=CEILING((COUNT(1)+0.0)/'
            + CAST(@sys_PageSize AS NVARCHAR)+') FROM ' + @sys_Table + @new_where1

EXEC SP_EXECUTESQL @SqlCount,N'@RCount INT OUTPUT,@PCount INT OUTPUT',
               @RCount OUTPUT,@PCount OUTPUT

IF @sys_PageIndex > CEILING((@RCount+0.0)/@sys_PageSize)    --假如輸出確當前頁數年夜於現實總頁數,則把現實總頁數賦值給以後頁數
BEGIN
    SET @sys_PageIndex =  CEILING((@RCount+0.0)/@sys_PageSize)
END

set @sql = 'select '+ @sys_fields +' from ' + @sys_Table + ' w1 '
    + ' where '+ @sys_Key +' in ('
        +'select top '+ ltrim(str(@sys_PageSize)) +' ' + @sys_Key + ' from '
        +'('
            +'select top ' + ltrim(STR(@sys_PageSize * @sys_PageIndex + @sys_Begin)) + ' ' + @sys_Key + ' FROM '
        + @sys_Table + @new_where1 + @new_order2
        +') w ' + @new_order1
    +') ' + @new_order2

print(@sql)

Exec(@sql)

GO

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved