在公用表值表达式(CTE),OFFSET / FETCH和内部SELECT中进行寻呼

时间:2014-04-16 11:18:35

标签: sql sql-server sql-server-2012

我有2个查询。首先是,

SELECT TOP (10)
    Products.*,
    Images.[ID] AS [ImageID],
    Images.[Path] AS [ImagePath]
FROM (SELECT
    Products.[ID] AS [Id1],
    Products.[Name] AS [Name],
    Products.[Price] AS [Price],
    Products.[NewPrice] AS [NewPrice],
    Products.[ShortDescription] AS [ShortDescription],
    Products.[SKU] AS [SKU],
    Products.[ProductTypeID] AS [ProductTypeID],
    Products.[ImageID] AS [ImageID],
    Products.[Promotion] AS [Promotion],
    Products.[ParentID] AS [ParentID],
    Products.[Attributes] AS [Attributes],
    ROW_NUMBER() OVER (ORDER BY Products.[ID] ASC) AS [row_number]
FROM [dbo].[Products] AS Products) AS Products
INNER JOIN [dbo].[Images] AS Images
    ON Products.[ImageID] = Images.[ID]
WHERE Products.[row_number] > (1 - 1) * 10
ORDER BY Products.[ID1] ASC

给了我,

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 0 ms.

(10 row(s) affected)
Table 'Worktable'. Scan count 0, logical reads 0, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Products'. Scan count 1, logical reads 1518, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Images'. Scan count 1, logical reads 3, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

 SQL Server Execution Times:
   CPU time = 156 ms,  elapsed time = 145 ms.

和第二个查询,

WITH CTEPage
AS (SELECT
    Products.[ID] AS [Id1],
    Products.[Name] AS [Name],
    Products.[Price] AS [Price],
    Products.[NewPrice] AS [NewPrice],
    Products.[ShortDescription] AS [ShortDescription],
    Products.[SKU] AS [SKU],
    Products.[ProductTypeID] AS [ProductTypeID],
    Products.[ImageID] AS [ImageID],
    Products.[Promotion] AS [Promotion],
    Products.[ParentID] AS [ParentID],
    Products.[Attributes] AS [Attributes],
    ROW_NUMBER() OVER (ORDER BY Products.[ID] ASC) AS [row_number]
FROM [dbo].[Products] AS Products)
SELECT TOP(10)
    Products.*
FROM CTEPage AS Products
INNER JOIN [dbo].[Images] AS Images
    ON Products.[ImageID] = Images.[ID]
WHERE Products.[row_number] > (1 - 1) * 10
ORDER BY Products.[ID1] ASC

给了我,

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 1 ms.

(10 row(s) affected)
Table 'Products'. Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

并使用Fetch和OFFSET,

SELECT
        Products.[ID] AS [Id1],
        Products.[Name] AS [Name],
        Products.[Price] AS [Price],
        Products.[NewPrice] AS [NewPrice],
        Products.[ShortDescription] AS [ShortDescription],
        Products.[SKU] AS [SKU],
        Products.[ProductTypeID] AS [ProductTypeID],
        Products.[ImageID] AS [ImageID],
        Products.[Promotion] AS [Promotion],
        Products.[ParentID] AS [ParentID],
        Products.[Attributes] AS [Attributes],
        ROW_NUMBER() OVER (ORDER BY Products.[ID] ASC) AS [row_number]
    FROM [dbo].[Products] AS Products
    INNER JOIN [dbo].[Images] AS Images
        ON Products.[ImageID] = Images.[ID]
ORDER BY Products.[ID] ASC
OFFSET 0 ROWS
FETCH NEXT 10 ROWS ONLY;

给了我,

SQL Server parse and compile time: 
   CPU time = 0 ms, elapsed time = 1 ms.

(10 row(s) affected)
Table 'Products'. Scan count 1, logical reads 4, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

 SQL Server Execution Times:
   CPU time = 0 ms,  elapsed time = 0 ms.

我应该使用哪一种,对我更好?

2 个答案:

答案 0 :(得分:1)

无论如何,查询处理器都会内联CTE。它们根本没有性能影响。

使用OFFSET和FETCH是否更快取决于具体的查询计划。没有合理的理由说明为什么它应该更慢或更快,但它往往产生不同的计划。我认为这实际上是查询优化器中的一个缺陷。

我认为这个问题不会对你有用。 You should probably research how to optimize paging in general.你太专注于CTE和OFFSET / FETCH这些没有意义的特定问题。

答案 1 :(得分:-1)

在SQL Server 2012中使用OFFSET和FETCH命令以获得更好的性能。

http://dbadiaries.com/new-t-sql-features-in-sql-server-2012-offset-and-fetch