与WHCH子句

时间:2017-01-10 07:57:17

标签: sql sql-server-2008 stored-procedures sql-server-2012

我有一个程序,用户可以使用该程序查找过去7天内发生的所有数据流量。我使用存储过程来获取数据 - 一次250条记录(用户可以翻阅该数据)。问题是,当用户想要查看数据时,会有很多超时。

以下是我尝试优化ist。

之前的存储过程
@MaxRecCount INT,
@PageOffset INT,
@IncludeData BIT

SELECT [Client], [Schema], [Version], [Records], [Fetched], [Receipted], [ProvidedAt], [FetchedAt], [ReceiptedAt],[PacketIds], [Record] FROM (
    SELECT TOP(@MaxRecCount) MAX(bai_ExportPendingArchive.[UserName]) AS Client,
    MAX(bai_ExportPendingArchive.Category) AS [Schema],
    MAX(bai_ExportPendingArchive.ContractVersion) AS [Version],
    COUNT(*) AS [Records],
    SUM (CASE WHEN bai_ExportPendingAckArchive.ExportPendingId IS NULL THEN 0 ELSE 1 END) as [Fetched],
    SUM (CASE WHEN bai_ExportPendingAckArchive.Receipted IS NULL THEN 0 ELSE 1 END) as [Receipted],
    MAX(bai_ExportArchive.Inserted) AS [ProvidedAt],
    MAX(CASE WHEN bai_ExportPendingAckArchive.ExportPendingId IS NULL THEN NULL ELSE bai_ExportPendingAckArchive.Inserted END) AS [FetchedAt],
    MAX(CASE WHEN bai_ExportPendingAckArchive.Receipted IS NULL THEN NULL ELSE bai_ExportPendingAckArchive.Receipted END) AS [ReceiptedAt],
    bai_ExportArchive.PacketIds AS [PacketIds],
    NULL AS [Record],
    ROW_NUMBER() Over (Order By MAX(bai_ExportArchive.Inserted) desc) as [RowNumber]
FROM bai_ExportArchive
INNER JOIN bai_ExportPendingArchive ON bai_ExportArchive.Id = bai_ExportPendingArchive.ExportId
LEFT OUTER JOIN bai_ExportPendingAckArchive ON bai_ExportPendingAckArchive.ExportPendingId = bai_ExportPendingArchive.Id
GROUP BY bai_ExportPendingArchive.[UserName], bai_ExportArchive.PacketIds, bai_ExportPendingArchive.Category
) AS InnerTable WHERE RowNumber > (@PageOffset * @MaxRecCount)  and RowNumber <= (@PageOffset * @MaxRecCount + @MaxRecCount)
ORDER BY RowNumber

@MaxRecCount,@ PageOffset和@IncludeData是来自我的C#-method的参数。 这个版本需要大约1:35分钟才能获得我想要的数据。为了使存储过程更快,我插入一个WHERE子句来过滤Inserted col(我也在这个列上做了一个索引)并使用OFFSET FETCH:

优化后的存储过程:

@MaxRecCount INT, 
@PageOffset INT, 
@IncludeData BIT 

Declare @pageStart int
Declare @pageEnd int

SET @pageStart = @PageOffset * @MaxRecCount
SET @pageEnd = @pageStart + @MaxRecCount + 50

IF @IncludeData = 0
    BEGIN
        SELECT [Client], [Schema], [Version], [Records], [Fetched], [Receipted], [ProvidedAt], [FetchedAt], [ReceiptedAt],[PacketIds], [Record] FROM (
            SELECT TOP(@MaxRecCount) bai_ExportPendingArchive.[UserName] AS Client,
            bai_ExportPendingArchive.Category AS [Schema],
            MAX(bai_ExportPendingArchive.ContractVersion) AS [Version],
            COUNT(*) AS [Records],
            SUM (CASE WHEN bai_ExportPendingAckArchive.ExportPendingId IS NULL THEN 0 ELSE 1 END) as [Fetched],
            SUM (CASE WHEN bai_ExportPendingAckArchive.Receipted IS NULL THEN 0 ELSE 1 END) as [Receipted],
            MAX(bai_ExportArchive.Inserted) AS [ProvidedAt],
            MAX(CASE WHEN bai_ExportPendingAckArchive.ExportPendingId IS NULL THEN NULL ELSE bai_ExportPendingAckArchive.Inserted END) AS [FetchedAt],
            MAX(CASE WHEN bai_ExportPendingAckArchive.Receipted IS NULL THEN NULL ELSE bai_ExportPendingAckArchive.Receipted END) AS [ReceiptedAt],
            bai_ExportArchive.PacketIds AS [PacketIds],
            NULL AS [Record],
            ROW_NUMBER() Over (Order By MAX(bai_ExportArchive.Inserted) desc) as [RowNumber]
            FROM bai_ExportArchive
            INNER JOIN bai_ExportPendingArchive ON bai_ExportArchive.Id = bai_ExportPendingArchive.ExportId
            LEFT OUTER JOIN bai_ExportPendingAckArchive ON bai_ExportPendingAckArchive.ExportPendingId = bai_ExportPendingArchive.Id
            Where bai_ExportArchive.Inserted <= (Select bai_ExportArchive.Inserted from bai_ExportArchive Order by bai_ExportArchive.Inserted DESC Offset @pageStart ROWS FETCH NEXT 1 ROWS Only)
            And bai_ExportArchive.Inserted > (Select bai_ExportArchive.Inserted from bai_ExportArchive Order by bai_ExportArchive.Inserted DESC Offset @pageEnd ROWS FETCH NEXT 1 ROWS Only)
            GROUP BY bai_ExportPendingArchive.[UserName], bai_ExportArchive.PacketIds, bai_ExportPendingArchive.Category
            ) AS InnerTable 
        ORDER BY RowNumber

此版本为我提供了大约2s的数据。唯一的问题是,我在Microsoft SQL Server 2014上工作,但我的用户使用SQL Server 2008+。现在的问题是,OFFSET FETCH在Server 2008中不起作用。现在我无法理解如何优化我的存储过程,它很快就可以在SQl Server 2008上运行。

我感谢任何帮助:)

1 个答案:

答案 0 :(得分:0)

尝试使用此方法处理SQL Server 2005/2008中的分页。

首先使用CTE为您的选择查询添加ROW_NUMBER()列,以标识记录编号/计数。之后,您可以使用CTEPAGE_NUMBER从此PAGE_COUNT中选择一系列记录。示例如下

DECLARE @P_PAGE_NUM     INT =    0
        ,@P_PAGE_SIZE   INT =   20

    ;WITH CTE
    AS
    (   /*SELECT    ROW_NUMBER() OVER (ORDER BY COL_to_SORT DESC)   AS  [ROW_NO]
                ,...
        WHERE   ....
        */ -- You can replace your select query here, but column [ROW_NO] should be there in your select list.
           --ie ROW_NUMBER() OVER (ORDER BY put_column-to-sort-here DESC)   AS  [ROW_NO] 
    )

    SELECT  *
            --,(    SELECT COUNT(*) FROM CTE) AS    [TOTAL_ROW_COUNT]
    FROM    CTE
    WHERE   (   
                ISNULL(@P_PAGE_NUM,0)   =   0   OR
                [ROW_NO]    BETWEEN (   @P_PAGE_NUM - 1) *  @P_PAGE_SIZE + 1  
                                AND     @P_PAGE_NUM      *  @P_PAGE_SIZE
            )
    ORDER BY    [ROW_NO]