内部有递归 CTE 的表值函数非常慢
我创建了一个 TVF,它从递归 CTE 返回一个包含父记录的表 此处。 效果非常好,并且可以直接获得结果。现在我想获取子记录(它们与当前记录的 PK 具有相同的 FK)。 问题是获取给定 id 的 22 条子记录需要 1 分 10 分钟。 为什么与查找父记录的相反 TVF 相比如此慢?
这是 ITVF:
CREATE FUNCTION [dbo].[_nextClaimsByIdData] (
@idData INT
)
RETURNS TABLE AS
RETURN(
WITH NextClaims
AS(
SELECT 1 AS relationLevel, child.*
FROM tabData child
WHERE child.fiData = @idData
UNION ALL
SELECT relationLevel+1, parent.*
FROM NextClaims nextOne
INNER JOIN tabData parent ON parent.fiData = nextOne.idData
)
SELECT TOP 100 PERCENT * FROM NextClaims order by relationLevel
)
这是关系:
以下是示例性查询的(正确)结果:
select relationLevel,idData,fiData from dbo._nextClaimsByIdData(30755592);
rl idData fiData
1 30073279 30755592
2 30765260 30073279
3 31942491 30765260
4 30895945 31942491
5 48045119 30895945
6 48342321 48045119
7 48342320 48342321
8 48308966 48342320
9 48308965 48308966
10 47044261 48308965
11 47044260 47044261
12 47253273 47044260
13 47253272 47253273
14 47279292 47253272
15 47279293 47279292
15 47494589 47279292
16 47494588 47494589
17 46051999 47494588
18 46373053 46051999
19 46083426 46373053
20 46099567 46083426
21 46600314 46099567
22 46595167 46600314
性能损失的原因可能是在我的第一个 TVF(上面链接)中我正在寻找主键和在此 TVF 中,我正在搜索(自引用)外键?如果是,我如何优化我的表架构以加速查询?
UPDATE: I've found out that the cause for this performance issue was that there was no Index on fiData(the foreignkey-column on the table's primary key). After creating and reorganizing, the result came immediately.
谢谢。
I've cretaed a TVF which returns a table with parent-records from a recursive CTE here.
That works excellent and the result is available directly. Now i wanted to get the child-records(they have the same FK as the current record's PK).
The problem is that it takes 1:10 minutes to get 22 child-records for a given id.
Why is this so slow compared to the opposite TVF which looks for parent-records?
This is the ITVF:
CREATE FUNCTION [dbo].[_nextClaimsByIdData] (
@idData INT
)
RETURNS TABLE AS
RETURN(
WITH NextClaims
AS(
SELECT 1 AS relationLevel, child.*
FROM tabData child
WHERE child.fiData = @idData
UNION ALL
SELECT relationLevel+1, parent.*
FROM NextClaims nextOne
INNER JOIN tabData parent ON parent.fiData = nextOne.idData
)
SELECT TOP 100 PERCENT * FROM NextClaims order by relationLevel
)
This is the relationship:
And the following the (correct) result for an exemplary query:
select relationLevel,idData,fiData from dbo._nextClaimsByIdData(30755592);
rl idData fiData
1 30073279 30755592
2 30765260 30073279
3 31942491 30765260
4 30895945 31942491
5 48045119 30895945
6 48342321 48045119
7 48342320 48342321
8 48308966 48342320
9 48308965 48308966
10 47044261 48308965
11 47044260 47044261
12 47253273 47044260
13 47253272 47253273
14 47279292 47253272
15 47279293 47279292
15 47494589 47279292
16 47494588 47494589
17 46051999 47494588
18 46373053 46051999
19 46083426 46373053
20 46099567 46083426
21 46600314 46099567
22 46595167 46600314
It it possible that the cause for the performace lost is that in my first TVF(linked above) i'm looking for primary keys and in this TVF i am searching for (self referencing) foreign keys? If yes, how can i optimize my table-schema to accelerate the query?
UPDATE: I've found out that the cause for this performance issue was that there was no Index on fiData(the foreignkey-column on the table's primary key). After creating and reorganizing, the result came immediately.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如前所述 fiData 列上的索引解决了性能问题
As stated Index on fiData columns solved performance issue