简体   繁体   English

MS SQL服务器和树

[英]MS SQL server and Trees

Im looking for some way of extrating data form a tree table as defined below. 我正在寻找某种从树表中提取数据的方法,如下所示。

Table Tree Defined as :- 表树定义为:
TreeID uniqueidentifier TreeID唯一标识符
TreeParent uniqueidentifier TreeParent唯一标识符
TreeCode varchar(50) TreeCode varchar(50)
TreeDesc varchar(100) TreeDesc varchar(100)

Data some (23k rows), Parent Refs back into ID in table 记录一些数据(23k行),父引用返回表中的ID

The following SQL renders the whole tree (takes arround 2 mins 30) 以下SQL渲染整个树(耗时约2分钟30)

I need to do the following. 我需要执行以下操作。

1) Render each Tree Node with its LVL 1 parent 1)渲染每个树节点及其LVL 1父级
2) Render all nodes that have a Description that matches a TreeDesc like 'SomeText%' 2)渲染所有具有与TreeDesc匹配的Description的节点,例如'SomeText%'
3) Render all parent nodes that are for a single tree id. 3)渲染所有用于单个树ID的父节点。

Items 2 and 3 take 2mins30 so this needs to be a lot faster! 第2项和第3项需要2分钟30,因此需要快很多!
Item 1, just cant work out how to do it with out killing SQL or taking forever 项目1,无法解决如何终止SQL或永久使用

any sugestions would be helpfull 任何建议都会有所帮助

Thanks 谢谢

Julian 朱利安

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

Have a look at the HIerarchyID Data type - that is done exactly for that stuff. 看一下HIerarchyID数据类型-正是针对该东西完成的。

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. 认真地说-树形结构不应在程序启动时就建立起来,而应随需应变,不要随意加载23.000个项目。

THat STILL being said - 2:30 minutes is too long either. 仍然说-2:30分钟也太长。 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. 像你碰上,迫使大量的表扫描的SQL设计问题,在我看来。

Thanks, The main issue is that the data alreay exists and has done for a long time 谢谢,主要的问题是数据拖欠存在并且已经做了很长时间了

The was no problems utill the boss asked for the main parent (ie root + 1) to be displayed be each item when displayed on the screen, When in tree mode not a problem as it loads nodes on demand, its when I need to list the selected noes, (ie 90+) with their main parent. 没问题,直到老板要求在屏幕上显示时,每个项目都显示主要的父级(即root + 1)。在树状模式下,因为它按需加载节点时没有问题,当我需要列出时选定的noes(即90岁以上)及其主要父母。

Currenty one of the 'Graduate developes' used temp tables and scanned back thro the the table parent by paent untill the right ones were found, this took like 30 seconds per node. 当前,其中一个“毕业生发展”使用的临时表并通过专利在表父上进行扫描,直到找到正确的表为止,每个节点花费了大约30秒的时间。

I trying to think of a better way of getting this info with out redisinge the tables and then having to deploy change scripts to all the clients. 我试图想掌握了redisinge表此信息的更好的方法,然后不得不更改脚本部署到所有客户端。

even worce we no have to display the main parent when doing an ajax filetered lookup so its got to to be very fast < 1 second! 甚至担心我们在执行Ajax过滤查询时也不必显示主要父对象,因此它必须非常快地<1秒! as we filter as you type. 在您键入时进行过滤。

I is looking like I may have to redesign the tables :( 我看起来我可能不得不重新设计桌子了:(

Also I think I am going to have the same problems with GeoPlantData which contains over 8.5m rows !!!! 我也认为GeoPlantData会遇到同样的问题,其中包含超过850万行!

Thanks for the info 谢谢(你的)信息

Julian 朱利安

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM