分层表和最高级别

时间:2015-07-05 09:00:53

标签: sql-server-2008 c#-4.0 common-table-expression hierarchical

我在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

0 个答案:

没有答案