TSQL-递归CTE效率低下-需要替代方法 [英] TSQL - Recursive CTE inefficient - Need an alternative
问题描述
这是一个带有示例数据的表:
Here is a table with sample data:
DECLARE @TestTable TABLE (
ItemID INT,
A INT,
B INT,
Month INT)
INSERT INTO @TestTable VALUES (1234, 5, 9, 1)
INSERT INTO @TestTable VALUES (1234, 6, 9, 2)
INSERT INTO @TestTable VALUES (4321, 5, 11, 1)
INSERT INTO @TestTable VALUES (4321, 12, 11, 2)
INSERT INTO @TestTable VALUES (1324, 14, 6, 1)
INSERT INTO @TestTable VALUES (1324, 5, 6, 2)
INSERT INTO @TestTable VALUES (1234, 1, 9, 3)
INSERT INTO @TestTable VALUES (1324, 9, 6, 3)
需要注意的是B列始终与在此计算中只使用过一次相同,但在初始计算中是必需的。
Something to note is that the B column is always the same as it's only used once in this calculation, but is needed for the initial calculation.
我试图在第一个计算中从A减去B行,然后在随后的行中从A减去前一行的差。实际上,在第一个 B-A = C
上,然后在 C-A
在所有后续行上对于相关的ItemID。
I am attempting to subtract B from A on the first row, then on subsequent rows subtract the previous rows difference from A. Effectively, B - A = C
on the first then C - A
on all subsequent rows FOR THE RELATED ItemID.
以下是我期望的结果:
ItemID A B C Month RowNumber
1234 5 9 4 1 1
1234 6 9 -2 2 2
1234 1 9 -3 3 3
1324 14 6 -8 1 1
1324 5 6 -13 2 2
1324 9 6 -22 3 3
4321 5 11 6 1 1
4321 12 11 -6 2 2
这就是我要完成的步骤。
Here is how I am accomplishing this.
;WITH CTE_TestValue AS (
SELECT
Main.ItemID,
Main.A,
Main.B,
Main.Month,
ROW_NUMBER() OVER (Partition BY Main.ItemID ORDER BY Main.Month) AS RowNumber
FROM @TestTable AS Main
),
CTE_TestColumnC AS (
SELECT
MainA.ItemID,
MainA.A,
MainA.B,
(MainA.B - MainA.A) AS C,
MainA.Month,
MainA.RowNumber
FROM CTE_TestValue AS MainA
WHERE MainA.Rownumber = 1
UNION ALL
SELECT
MainB.ItemID,
MainB.A,
MainB.B,
(Sub.C - MainB.A) AS C,
MainB.Month,
MainB.RowNumber
FROM CTE_TestValue AS MainB
INNER JOIN CTE_TestColumnC AS Sub
ON MainB.RowNumber - 1 = Sub.RowNumber
AND MainB.ItemID = Sub.ItemID
-- CROSS JOIN CTE_TestColumnC AS Sub
-- WHERE Sub.RowNumber + 1 = MainB.RowNumber
-- AND MainB.ItemID = Sub.ItemID
)
SELECT
Main.ItemID,
Main.A,
Main.B,
Main.C,
Main.Month,
Main.RowNumber
FROM CTE_TestColumnC AS Main
ORDER BY ItemID, Month, RowNumber
这在一个较小的数据样本上效果很好,但是我正在处理大约20,000个ItemId,每个重复10次。
This works fine on a small data-sample, but I'm dealing with about 20,000 ItemId's each repeating 10 times. It finishes all the first row calculations instantly, as expected, and then the calculation times go up DRASTICALLY.
如您所见,我已经尝试了两个<$ c $,它会立即按预期完成所有第一行的计算,然后计算时间急剧增加。
As you can see I've tried both an INNER JOIN
and a CROSS JOIN
. I believe they have the same execution plan with the parameters that I've given the CROSS JOIN
.
昨天我让它运行了5个小时,看它是否结束了。.它没有结束。
I allowed this to run for 5 hours yesterday to see if it ever ended.. it did not.
另一注:当我在测试数据上使用它时,我 SELECT
而没有使用 ORDER
希望有助于加快进度。 ORDER
只是为了方便我查看事实。
Another note: When I'm using this on the test data I SELECT
WITHOUT using ORDER
to hopefully help speed things along. The ORDER
is just for my convenience when I'm fact checking.
推荐答案
您的问题是您使用CTE作为递归CTE的来源。对于递归CTE的每次迭代,您的第一个CTE将执行一次。使用您的测试数据,这意味着将创建8次 CTE_TestValue
。
Your problem is that you are using a CTE as the source of a recursive CTE. Your first CTE will be executed once for each iteration of your recursive CTE. With your test data that means that CTE_TestValue
is created 8 times.
将 CTE_TestValue
在(RowNumber,ItemID)
上具有集群主键,并将该临时表用作该表的数据源递归CTE CTE_TestColumnC
。
Put the result of CTE_TestValue
in a temp table that has a clustered primary key on (RowNumber, ItemID)
and use that temporary table as the source of data for the recursive CTE CTE_TestColumnC
.
还要将递归部分的连接条件更改为 ON MainB.RowNumber = Sub.RowNumber +1
。这将使查询能够使用临时表上的索引。
Also change the join condition in the recursive part to ON MainB.RowNumber = Sub.RowNumber + 1
. That will make the query able to use the index on the temporary table.
DECLARE @TestTable TABLE (
ItemID INT,
A INT,
B INT,
Month INT)
INSERT INTO @TestTable VALUES (1234, 5, 9, 1)
INSERT INTO @TestTable VALUES (1234, 6, 9, 2)
INSERT INTO @TestTable VALUES (4321, 5, 11, 1)
INSERT INTO @TestTable VALUES (4321, 12, 11, 2)
INSERT INTO @TestTable VALUES (1324, 14, 6, 1)
INSERT INTO @TestTable VALUES (1324, 5, 6, 2)
INSERT INTO @TestTable VALUES (1234, 1, 9, 3)
INSERT INTO @TestTable VALUES (1324, 9, 6, 3)
CREATE TABLE #TestValue
(
ItemID INT,
A INT,
B INT,
Month INT,
RowNumber INT,
primary key(RowNumber, ItemID)
)
INSERT INTO #TestValue
SELECT
Main.ItemID,
Main.A,
Main.B,
Main.Month,
ROW_NUMBER() OVER (Partition BY Main.ItemID ORDER BY Main.Month) AS RowNumber
FROM @TestTable AS Main
;WITH CTE_TestColumnC AS (
SELECT
MainA.ItemID,
MainA.A,
MainA.B,
(MainA.B - MainA.A) AS C,
MainA.Month,
MainA.RowNumber
FROM #TestValue AS MainA
WHERE MainA.Rownumber = 1
UNION ALL
SELECT
MainB.ItemID,
MainB.A,
MainB.B,
(Sub.C - MainB.A) AS C,
MainB.Month,
MainB.RowNumber
FROM #TestValue AS MainB
INNER JOIN CTE_TestColumnC AS Sub
ON MainB.RowNumber = Sub.RowNumber + 1
AND MainB.ItemID = Sub.ItemID
)
SELECT
Main.ItemID,
Main.A,
Main.B,
Main.C,
Main.Month,
Main.RowNumber
FROM CTE_TestColumnC AS Main
ORDER BY ItemID, Month, RowNumber
DROP TABLE #TestValue
在查询计划中,问题显示在右下角的表格扫描中。使用此测试数据,它将执行8次,总共返回64行:
In the query plan for your query the problem is shown in the table scan in the lower right corner. with this test data it is executed 8 times with a total of 64 rows returned:
查询计划用于带有临时表的查询:
The query plans for the query with a temporary table:
这篇关于TSQL-递归CTE效率低下-需要替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!