根据相关节点从层次结构中选择行

时间:2015-04-07 16:20:34

标签: sql sql-server hierarchy

我有一个自引用表Foo

[Id] int NOT NULL,
[ParentId] int NULL,   --Foreign key to [Id]
[Type] char(1) NOT NULL

[Id]是群集主键,[ParentId][Type]列上的索引。

假设层次结构的最大深度为1(子节点不能有子节点)。


我希望得到满足以下条件的所有Foo行:

  • 类型为 A
  • 在其家谱中有 B
  • 在其系列树中有 C D

以下查询使用JOIN' s返回所需的结果,但性能很差

SELECT DISTINCT [Main].*

FROM Foo AS [Main]

--[Main] may not be root node
LEFT OUTER JOIN Foo AS [Parent]
    ON [Parent].[Id] = [Main].[ParentId]

--Must have a B in tree
INNER JOIN Foo AS [NodeB]
    ON (
        [NodeB].[Pid] = [Main].[Pid]            --Sibling
            OR [NodeB].[ParentId] = [Main].[Id] --Child
            OR [NodeB].[Id] = [Parent].[Id]     --Parent
    )
        AND [NodeB].[Type] = 'B'

--Must have a C or D in tree
INNER JOIN Foo AS [NodeCD]
    ON (
        [NodeCD].[Pid] = [Main].[Pid]            --Sibling
            OR [NodeCD].[ParentId] = [Main].[Id] --Child
            OR [NodeCD].[Id] = [Parent].[Id]     --Parent
    )
        AND [NodeCD].[Type] IN ('C', 'D')

WHERE [Main].[Type] = 'A'

从实际执行计划仅限于仅查看650,000行的前10,000个 Query execution plan


如果我从查询中删除--Parent行

OR [NodeB].[Id] = [Parent].[Id]  --Parent
OR [NodeCD].[Id] = [Parent].[Id] --Parent

然后执行变得几乎是瞬间的,但它错过了A是孩子并且只有一个兄弟的情况

Misses this:    Catches this:
B               B
├A              ├A
└C              ├B
                └C

我试图想出一个CTE来做这件事,因为它在性能方面看起来更有希望,但我无法想象如何排除那些不符合标准的树。

到目前为止CTE

WITH [Parent] AS 
(
SELECT  *
FROM    [Foo]
WHERE   [ParentId] IS NULL

UNION ALL
SELECT  [Child].*
FROM    Foo AS [Child]
JOIN    [Parent]
ON      [Child].[ParentId] = [Parent].Id
WHERE   [Child].[Type] = 'P'

UNION ALL
SELECT  [ChildCD].*
FROM    Foo AS [ChildCD]
JOIN    [Parent]
ON      [ChildCD].[ParentId] = [Parent].Id
WHERE   [ChildCD].[Type] IN ('C', 'D')
)

SELECT  *
FROM [Parent]
WHERE [Type] = 'I';

但是,如果我尝试添加Sibling-Child-Parent OR语句,我的最大递归级别为100。


SQL Fiddle with test data

6 个答案:

答案 0 :(得分:4)

这样的事情怎么样?

select
    [F].[Id]
from
    [Foo] [F]
where
    [F].[Type] = 'A' and
    (
        (
            [F].[ParentId] is null and
            exists (select 1 from [Foo] [Child] where [F].[Id] = [Child].[ParentId] and [Child].[Type] = 'B') and
            exists (select 1 from [Foo] [Child] where [F].[Id] = [Child].[ParentId] and [Child].[Type] in ('C', 'D'))
        ) or
        (
            [F].[ParentId] is not null and
            exists (select 1 from [Foo] [ParentOrSibling] where [F].[ParentId] in ([ParentOrSibling].[Id], [ParentOrSibling].[ParentId]) and [ParentOrSibling].[Type] = 'B') and
            exists (select 1 from [Foo] [ParentOrSibling] where [F].[ParentId] in ([ParentOrSibling].[Id], [ParentOrSibling].[ParentId]) and [ParentOrSibling].[Type] in ('C', 'D'))
        )
    );

答案 1 :(得分:4)

正在检查的节点是根节点的情况与它是子节点的情况完全不同,您可能最好分别查询这两个节点并形成两个集合中的UNION ALL 。但是,您可以使用公用表表达式来简化,该表达式标识包含您之后所拥有的节点的树。总的来说,这可能是这样的:

WITH [TargetFamilies] AS (
    SELECT
      COALESCE(ParentId, Id) AS FamilyId
    FROM Foo
    GROUP BY COALESCE(ParentId, Id)
    HAVING 
      COUNT(CASE Type WHEN 'B' THEN 1 END) > 0
      AND COUNT(CASE Type WHEN 'C' THEN 1 WHEN 'D' THEN 1 END) > 0
)

-- root nodes
SELECT [Main].*
FROM
  Foo AS [Main]
  JOIN [TargetFamilies] ON [Main].Id = [TargetFamilies].FamilyId
WHERE
  [Main].Type = 'A'

UNION ALL

-- child nodes
SELECT 
  [Main].*
FROM
  Foo AS [Main]
  JOIN [TargetFamilies] ON [Main].ParentId = [TargetFamilies].FamilyId
WHERE
  [Main].Type = 'A'

答案 2 :(得分:4)

我无法预测效率,但这是另一种解决方案:

SELECT *
FROM Foo AS f
WHERE Type = 'A'
  AND ParentId IS NULL
  AND EXISTS 
      ( SELECT *
        FROM Foo AS ch
        WHERE ch.ParentId = f.Id
          AND ch.Type = 'B'
      )
  AND EXISTS 
      ( SELECT *
        FROM Foo AS ch
        WHERE ch.ParentId = f.Id
          AND ch.Type IN ('C', 'D')
      ) 
UNION ALL

SELECT *
FROM Foo AS f
WHERE Type = 'A'
  AND ParentId IS NOT NULL
  AND EXISTS
    ( SELECT 1
      FROM
          ( SELECT *
            FROM (VALUES ('B'), ('C'), ('D')) AS x (Type)
          EXCEPT
            SELECT p.Type
            FROM Foo AS p
            WHERE f.ParentId = p.Id
          EXCEPT
            SELECT sib.Type
            FROM Foo AS sib
            WHERE f.ParentId = sib.ParentId
          ) AS x
       HAVING MIN(Type) = MAX(Type) AND MIN(Type) <> 'B'
           OR MIN(Type) IS NULL
    ) ; 

SQLfiddle

中测试过

答案 3 :(得分:1)

使用这组数据对其进行优化并不容易,但也许可以尝试一下。 LEFT OUTER JOIN似乎是多余的。此外,执行计划在内循环中没有显示96%的命中率。

SELECT DISTINCT [Main].*
FROM Foo AS [Main]


--Must have a B in tree
INNER JOIN Foo AS [NodeB]
    ON (
        [NodeB].[ParentId] = [Main].[ParentId]            --Sibling
            OR [NodeB].[ParentId] = [Main].[Id] --Child
            OR [NodeB].[Id] = [Main].[ParentId]     --Parent
    )
        AND [NodeB].[Type] = 'B'

--Must have a C or D in tree
INNER JOIN Foo AS [NodeCD]
    ON (
        [NodeCD].[ParentId] = [Main].[ParentId]            --Sibling
            OR [NodeCD].[ParentId] = [Main].[Id] --Child
            OR [NodeCD].[Id] = [Main].[ParentId]     --Parent
    )
        AND [NodeCD].[Type] IN ('C', 'D')

WHERE [Main].[Type] = 'A'

请发布您的结果。希望这会有所帮助。

答案 4 :(得分:1)

Jeez,这花了比我想象的更长的时间,肯定有更好的方法,但这是我的看法:

WITH CTE AS
(
    SELECT Id, ParentId FamilyId, [Type]
    FROM dbo.Foo
    UNION
    SELECT A.Id, B.Id, B.[Type]
    FROM dbo.Foo A
    INNER JOIN dbo.Foo B
        ON A.ParentId = B.Id
)
SELECT DISTINCT B.Id
FROM CTE A
INNER JOIN dbo.Foo B
    ON A.Id = B.Id
    OR A.FamilyId = B.Id
WHERE B.[Type] = 'A'
AND EXISTS( SELECT 1 FROM CTE
            WHERE FamilyId = A.FamilyId
            AND [Type] = 'B')
AND EXISTS( SELECT 1 FROM CTE
            WHERE FamilyId = A.FamilyId
            AND [Type] IN ('C','D'));

Here is the modified sqlfiddle。

答案 5 :(得分:0)

使用递归CTE。这适用于任何多级层次结构:

DECLARE @t TABLE
    (
      ID INT ,
      ParentID INT ,
      Type CHAR(1)
    )

INSERT  INTO @t
VALUES  ( 1, NULL, 'A' ),
        ( 2, 1, 'B' ),
        ( 3, NULL, 'C' ),
        ( 4, NULL, 'A' ),
        ( 5, 4, 'B' ),
        ( 6, 4, 'C' ),
        ( 7, NULL, 'A' ),
        ( 8, 7, 'B' ),
        ( 9, 8, 'D' ),
        ( 10, NULL, 'D' ),
        ( 11, 10, 'A' ),
        ( 12, 11, 'B' ),
        ( 13, 8, 'D' );

WITH    cte1
          AS ( SELECT   ID ,
                        ParentID ,
                        Type ,
                        ID AS GroupID ,
                        0 AS B ,
                        0 AS CD
               FROM     @t
               WHERE    Type = 'A'
               UNION ALL
               SELECT   t.ID ,
                        t.ParentID ,
                        t.Type ,
                        c.GroupID ,
                        CASE WHEN t.Type = 'B' THEN 1
                             ELSE 0
                        END ,
                        CASE WHEN t.Type IN ( 'C', 'D' ) THEN 1
                             ELSE 0
                        END
               FROM     @t t
                        JOIN cte1 c ON t.ParentID = c.ID
             ),
        cte2
          AS ( SELECT   ID ,
                        ParentID ,
                        Type ,
                        ID AS GroupID ,
                        0 AS B ,
                        0 AS CD
               FROM     @t
               WHERE    Type = 'A'
               UNION ALL
               SELECT   t.ID ,
                        t.ParentID ,
                        t.Type ,
                        c.GroupID ,
                        CASE WHEN t.Type = 'B' THEN 1
                             ELSE 0
                        END ,
                        CASE WHEN t.Type IN ( 'C', 'D' ) THEN 1
                             ELSE 0
                        END
               FROM     @t t
                        JOIN cte2 c ON t.ID = c.ParentID
             ),
        filter
          AS ( SELECT   ID ,
                        Type ,
                        SUM(B) OVER ( PARTITION BY GroupID ) AS B ,
                        SUM(CD) OVER ( PARTITION BY GroupID ) AS CD
               FROM     ( SELECT    *
                          FROM      cte1
                          UNION
                          SELECT    *
                          FROM      cte2
                        ) t
             )
    SELECT  t.*
    FROM    filter f
            JOIN @t t ON t.ID = f.ID
    WHERE   f.Type = 'A'
            AND B > 0
            AND cd > 0

输出:

ID  ParentID    Type
4   NULL        A
7   NULL        A
11  10          A