为什么窗口聚合函数的逻辑读取如此之高?

时间:2010-11-20 02:08:40

标签: sql sql-server sql-server-2008

我发现在使用公共子表达式假脱机的执行计划中,报告的逻辑读取对于大型表来说非常高。

经过一些试验和错误后,我发现了一个似乎适用于下面的测试脚本和执行计划的公式。 Worktable logical reads = 1 + NumberOfRows * 2 + NumberOfGroups * 4

我不明白这个公式的原因。这比我想象的更有必要看一下这个计划。任何人都可以通过吹嘘这个问题来解释这个问题吗?

或者失败那是否有任何方法可以追踪每个逻辑读取中读取的页面,以便我可以为自己解决这个问题?

SET STATISTICS IO OFF; SET NOCOUNT ON;

IF Object_id('tempdb..#Orders') IS NOT NULL
  DROP TABLE #Orders;

CREATE TABLE #Orders
  (
     OrderID    INT IDENTITY(1, 1) NOT NULL PRIMARY KEY CLUSTERED,
     CustomerID NCHAR(5) NULL,
     Freight    MONEY NULL,
  );

CREATE NONCLUSTERED INDEX ix
  ON #Orders (CustomerID)
  INCLUDE (Freight);

INSERT INTO #Orders
VALUES (N'ALFKI', 29.46), 
       (N'ALFKI', 61.02), 
       (N'ALFKI', 23.94), 
       (N'ANATR', 39.92), 
       (N'ANTON', 22.00);

SELECT PredictedWorktableLogicalReads = 
        1 + 2 * Count(*) + 4 * Count(DISTINCT CustomerID)
FROM   #Orders;

SET STATISTICS IO ON;

SELECT OrderID,
       Freight,
       Avg(Freight) OVER (PARTITION BY CustomerID) AS Avg_Freight
FROM   #Orders; 

输出

PredictedWorktableLogicalReads
------------------------------
23

Table 'Worktable'. Scan count 3, logical reads 23, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table '#Orders___________000000000002'. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

Execution Plan

其他信息:

Query Tuning and Optimization书的第3章和this blog post by Paul White对这些线轴有一个很好的解释。

总之,计划顶部的段迭代器为它发送的行添加一个标志,指示它是新分区的开始。主段假脱机从段迭代器一次获取一行,并将其插入tempdb中的工作表。一旦获得标志说新组已经启动它就会返回一行到嵌套循环运算符的顶部输入。这会导致在工作表中的行上调用流聚合,计算平均值,然后在工作表被截断为新组准备好之前,将该值与工作表中的行连接起来。段假脱机发出一个虚拟行,以便处理最后一组。

据我所知,工作表是一个堆(或者它将在计划中表示为索引假脱机)。但是,当我尝试复制相同的进程时,它只需要11次逻辑读取。

CREATE TABLE #WorkTable
  (
     OrderID    INT,
     CustomerID NCHAR(5) NULL,
     Freight    MONEY NULL,
  )

DECLARE @Average MONEY

PRINT 'Insert 3 Rows'

INSERT INTO #WorkTable
VALUES      (1, N'ALFKI', 29.46) /*Scan count 0, logical reads 1*/

INSERT INTO #WorkTable
VALUES      (2, N'ALFKI', 61.02) /*Scan count 0, logical reads 1*/

INSERT INTO #WorkTable
VALUES      (3, N'ALFKI', 23.94) /*Scan count 0, logical reads 1*/
PRINT 'Calculate AVG'

SELECT @Average = Avg(Freight)
FROM   #WorkTable /*Scan count 1, logical reads 1*/
PRINT 'Return Rows - With the average column included'

/*This convoluted query is just to force a nested loops plan*/
SELECT *
FROM   (SELECT @Average AS Avg_Freight) T /*Scan count 1, logical reads 1*/
       OUTER APPLY #WorkTable
WHERE  COALESCE(Freight, OrderID) IS NOT NULL
       AND @Average IS NOT NULL

PRINT 'Clear out work table'

TRUNCATE TABLE #WorkTable

PRINT 'Insert 1 Row'

INSERT INTO #WorkTable
VALUES      (4, N'ANATR', 39.92) /*Scan count 0, logical reads 1*/
PRINT 'Calculate AVG'

SELECT @Average = Avg(Freight)
FROM   #WorkTable /*Scan count 1, logical reads 1*/
PRINT 'Return Rows - With the average column included'

SELECT *
FROM   (SELECT @Average AS Avg_Freight) T /*Scan count 1, logical reads 1*/
       OUTER APPLY #WorkTable
WHERE  COALESCE(Freight, OrderID) IS NOT NULL
       AND @Average IS NOT NULL

PRINT 'Clear out work table'

TRUNCATE TABLE #WorkTable

PRINT 'Insert 1 Row'

INSERT INTO #WorkTable
VALUES      (5, N'ANTON', 22.00) /*Scan count 0, logical reads 1*/
PRINT 'Calculate AVG'

SELECT @Average = Avg(Freight)
FROM   #WorkTable /*Scan count 1, logical reads 1*/
PRINT 'Return Rows - With the average column included'

SELECT *
FROM   (SELECT @Average AS Avg_Freight) T /*Scan count 1, logical reads 1*/
       OUTER APPLY #WorkTable
WHERE  COALESCE(Freight, OrderID) IS NOT NULL
       AND @Average IS NOT NULL

PRINT 'Clear out work table'

TRUNCATE TABLE #WorkTable

PRINT 'Calculate AVG'

SELECT @Average = Avg(Freight)
FROM   #WorkTable /*Scan count 1, logical reads 0*/
PRINT 'Return Rows - With the average column included'

SELECT *
FROM   (SELECT @Average AS Avg_Freight) T
       OUTER APPLY #WorkTable
WHERE  COALESCE(Freight, OrderID) IS NOT NULL
       AND @Average IS NOT NULL

DROP TABLE #WorkTable 

2 个答案:

答案 0 :(得分:21)

对于工作表,逻辑读取的计数方式不同:每个读取有一个“逻辑读取”。这并不意味着工作表在某种程度上比“真正的”假脱机表效率低(完全相反);逻辑读取只是在不同的单元中。

我认为,计算工作表逻辑读取的哈希页面并不是非常有用,因为这些结构是服务器的内部结构。在逻辑读取计数器中假脱机的报告行使得该数字对于分析目的更有意义。

这种洞察力应该是你的公式清晰的原因。两个辅助线轴完全读取两次(2 * COUNT(*)),主卷轴发出(组值数+ 1)行,如我的博客条目中所述,给出(COUNT(DISTINCT CustomerID)+ 1)组件。加号是主线轴发出的额外行,表示最后一组已经结束。

答案 1 :(得分:0)

在公式中,您给NumberOfRows * 2将保持为true,因为您的执行图中的Sort函数和Stream Aggregate show都需要所有行来完成处理。当为“:”添加“where”子句时,您能否确认逻辑读取的减少:

  1. 运费价值
  2. 客户id