0

我正在寻找某种从如下定义的树表中提取数据的方法。

表树定义为:-
TreeID uniqueidentifier
TreeParent uniqueidentifier
TreeCode varchar(50)
TreeDesc varchar(100)

一些数据(23k 行),Parent Refs 回到表中的 ID

以下 SQL 呈现整个树(大约需要 2 分钟 30 分钟)

我需要执行以下操作。

1) 使用其 LVL 1 父级渲染每个树节点
2) 渲染所有具有与 TreeDesc 匹配的描述的节点,例如“SomeText%”
3) 渲染所有用于单个树 id 的父节点。

第 2 项和第 3 项需要 2 分钟 30 分钟,所以这需要更快!
第 1 项,只是不知道如何在不杀死 SQL 或永远占用的情况下做到这一点

任何建议都会有所帮助

谢谢

朱利安

WITH TreeCTE(TreeCode, TreeDesc, depth, TreeParent, TreeID)
AS
(
  -- anchor member
  SELECT cast('' as varchar(50)) as TreeCode , 
   cast('Trees'  as varchar(100)) as TreeDesc, 
   cast('0' as Integer) as depth, 
   cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier) as TreeParent, 
   cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier) as TreeID

  UNION ALL

  -- recursive member
  SELECT s.TreeCode, 
   s.TreeDesc, 
   cte.depth+1, 
   isnull(s.TreeParent, cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier)), 
   isnull(s.TreeID, cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier)) 
  FROM pdTrees AS S
    JOIN TreeCTE AS cte
      ON isnull(s.TreeParent, cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier)) = isnull( cte.TreeID , cast('00000000-0000-0000-0000-000000000000' as uniqueidentifier))
)

-- outer query

SELECT
s.TreeID, s.TreeCode, s.TreeDesc, s.depth, s.TreeParent    
FROM TreeCTE s
4

2 回答 2

0

Have a look at the HIerarchyID Data type - that is done exactly for that stuff.

Besides that - your recursion is about the worst way to get along with that. You should go into that procedureally, possibly, aggregating data into a temporary table as needed. Or - just forget about it. Seriously - Tree structures should not be put up on program start, but on demand, 23.000 items should just not be loaded without need.

THat STILL being said - 2:30 minutes is too long either. For something that is to be compuited in memory. Are you sure you have proper indices on your tables? Can you publish the query plan for the above query so we can check? Looks to me like you run into a SQL Design issue that forces lots of table scans.

于 2010-04-15T09:51:01.010 回答
0

谢谢,主要问题是数据已经存在并且已经完成了很长时间

没有问题直到老板要求在屏幕上显示每个项目时显示主父级(即root + 1),当在树模式下不是问题,因为它按需加载节点,当我需要列出时选定的 noes(即 90 岁以上)与他们的主要父母。

目前,其中一个“研究生开发者”使用临时表并由 paent 扫描父表,直到找到正确的表,每个节点大约需要 30 秒。

我试图想出一种更好的方法来获取此信息,而无需重新调整表格,然后必须将更改脚本部署到所有客户端。

甚至更糟糕的是,在进行 ajax 过滤查找时,我们不必显示主父级,因此它必须非常快 < 1 秒!当我们在您输入时进行过滤。

我看起来可能需要重新设计表格:(

此外,我认为包含超过 8.5m 行的 GeoPlantData 也会遇到同样的问题!!!!

谢谢(你的)信息

朱利安

于 2010-04-15T12:50:13.397 回答