我在sql中使用以下函数来获取我的节点级别,但目前我的节点变得超过20000,所以运行此函数需要50秒,我该如何优化它?
WITH cteLevels
AS
(
SELECT
TopicId
,ParentId
,1 AS [Level]
FROM Accounting.Topics
WHERE parentId = 0
UNION ALL
SELECT
Accounting.Topics.topicId
,Accounting.Topics.Parentid
,[Level] + 1 AS [Level]
FROM Accounting.Topics
INNER JOIN cteLevels
ON ( Accounting.Topics.ParentId = cteLevels.TopicID )
)
-- Viewing Data
SELECT
*
FROM cteLevels