SQL邻接列表查询

时间:2017-12-28 11:09:14

标签: sql sql-server-2017

尝试为用户访问的服务建模依赖关系。我创建了一个父/子邻接列表样式表,该表规定'服务'依赖于组件1,组件1依赖于组件2等,以及依赖的类型,例如全部或部分

此图显示布局 - 依赖关系图像

enter image description here

Comp 1& 9的颜色不同,因为如果它们失败则整体服务失败。如果Comp 2-9中的任何一个失败,则服务继续,但只是降低了弹性。

以下是我用来创建表格的内容

CREATE TABLE scratch
( 
    KeyID        int    PRIMARY KEY        NOT NULL, 
    CompDesc       varchar(30), 
    CompID        int    NOT NULL, 
    ReliesOn       int    NOT NULL, 
    RelianceType       varchar(30), 
)

INSERT scratch SELECT 0, 'Service', 0, 1, 'Full'
INSERT scratch SELECT 1, 'Component 1', 1, 2, 'Partial'
INSERT scratch SELECT 2, 'Component 1', 1, 3, 'Partial'
INSERT scratch SELECT 3, 'Component 1', 1, 4, 'Partial'
INSERT scratch SELECT 4, 'Component 4', 4, 5, 'Full'
INSERT scratch SELECT 5, 'Component 5', 5, 6, 'Full'
INSERT scratch SELECT 6, 'Component 6', 6, 7, 'Partial'
INSERT scratch SELECT 7, 'Component 6', 6, 8, 'Partial'
INSERT scratch SELECT 8, 'Component 2', 2, 9, 'Full'
INSERT scratch SELECT 9, 'Component 3', 3, 9, 'Full'
INSERT scratch SELECT 10, 'Component 7', 7, 9, 'Full'
INSERT scratch SELECT 11, 'Component 8', 8, 9, 'Full'

然后我可以运行一个非常粗略的查询来显示用户可以访问该服务的4个不同选项 -

SELECT t1.ReliesOn AS lev1, t2.ReliesOn as lev2, t3.ReliesOn as lev3, t4.ReliesOn as lev4, t5.ReliesOn as lev5, t6.ReliesOn as lev6
FROM Scratch AS t1
LEFT JOIN Scratch AS t2 ON t2.CompID = t1.ReliesOn
LEFT JOIN Scratch AS t3 ON t3.CompID = t2.ReliesOn
LEFT JOIN Scratch AS t4 ON t4.CompID = t3.ReliesOn
LEFT JOIN Scratch AS t5 ON t5.CompID = t4.ReliesOn
LEFT JOIN Scratch AS t6 ON t6.CompID = t5.ReliesOn
WHERE t1.ReliesOn = 1;

(道歉,可能有更好的方法将该查询串在一起)

有了这个结果 -

lev1    lev2    lev3    lev4    lev5    lev6
1       2       9       NULL    NULL    NULL
1       3       9       NULL    NULL    NULL
1       4       5       6       7       9
1       4       5       6       8       9

我要做的是创建一个查询,我可以设置失败的组件,并确定该服务是否仍然可用,例如。

Failed      Result
1           No Service
2,3         Reduced Resiliency
3,8         Reduced Resiliency
2,3,7,8     No Service

这只是一个非常简单的例子,我需要添加更多内容,在大多数情况下,许多服务都依赖于相同的组件。

那么,你如何从失败的comp中走出来并向上/向下/向下依赖,以确定路径是否仍然存在?

希望这是有道理的

由于

2 个答案:

答案 0 :(得分:0)

显然,结果查询并不容易。我向您发布了一种方法,您可以在SQL Fiddle

上进行播放

MS SQL Server 2014架构设置

CREATE TABLE scratch
( 
    KeyID        int    PRIMARY KEY        NOT NULL, 
    CompDesc       varchar(30), 
    CompID        int    NOT NULL, 
    ReliesOn       int     NULL, --null to allow node 9
    RelianceType       varchar(30), 
)

INSERT scratch SELECT 0, 'Service', 0, 1, 'Full'
INSERT scratch SELECT 1, 'Component 1', 1, 2, 'Partial'
INSERT scratch SELECT 2, 'Component 1', 1, 3, 'Partial'
INSERT scratch SELECT 3, 'Component 1', 1, 4, 'Partial'
INSERT scratch SELECT 4, 'Component 4', 4, 5, 'Full'
INSERT scratch SELECT 5, 'Component 5', 5, 6, 'Full'
INSERT scratch SELECT 6, 'Component 6', 6, 7, 'Partial'
INSERT scratch SELECT 7, 'Component 6', 6, 8, 'Partial'
INSERT scratch SELECT 8, 'Component 2', 2, 9, 'Full'
INSERT scratch SELECT 9, 'Component 3', 3, 9, 'Full'
INSERT scratch SELECT 10, 'Component 7', 7, 9, 'Full'
INSERT scratch SELECT 11, 'Component 8', 8, 9, 'Full'
INSERT scratch SELECT 12, 'Component 9', 9, Null, 'Full' --node 9 added

这里查询来计算活动路径:

with 
from_to as ( select 9 as [from], 1 as [to] ),
failed_nodes as ( select 3 as f union select 4 ), --list of failed nodes
cte as (
   select *
          ,CAST(CompID AS VARCHAR(255)) AS Path
   from scratch
   where CompId = (select [from] from from_to ) -easy: ReliesOn is null
   union all
   select s.*
          ,CAST(Path + '.' + CAST(s.CompID AS VARCHAR(255)) AS VARCHAR(255))
   from scratch s
   inner join cte on s.ReliesOn = cte.CompID
   where s.compid not in ( select * from failed_nodes)
)
select * from cte

只需检查 Results 结果中的keyID = 0,了解服务是否仍然可用以及活动路径(简单where):

| KeyID |    CompDesc | CompID | ReliesOn | RelianceType |    Path |
|-------|-------------|--------|----------|--------------|---------|
|    12 | Component 9 |      9 |   (null) |         Full |       9 |
|     8 | Component 2 |      2 |        9 |         Full |     9.2 |
|    10 | Component 7 |      7 |        9 |         Full |     9.7 |
|    11 | Component 8 |      8 |        9 |         Full |     9.8 |
|     7 | Component 6 |      6 |        8 |      Partial |   9.8.6 |
|     5 | Component 5 |      5 |        6 |         Full | 9.8.6.5 |
|     6 | Component 6 |      6 |        7 |      Partial |   9.7.6 |
|     5 | Component 5 |      5 |        6 |         Full | 9.7.6.5 |
|     1 | Component 1 |      1 |        2 |      Partial |   9.2.1 |
|     0 |     Service |      0 |        1 |         Full | 9.2.1.0 < still alive

此外,请根据您的方案调整此解决方案以处理RelianceType个节点。

答案 1 :(得分:0)

我设法编写了一个使用Recursive CTEs执行此操作的查询。

首先,我冒昧地在scratch表中添加了两个记录:

INSERT scratch SELECT 12, 'Component 9', 9, 10, 'Full'
INSERT scratch SELECT 13, 'Users', 10, 10, 'Full'

这意味着:

  • 9依赖10(用户),如图
  • 所示
  • 10依赖于自身(这意味着在这种情况下,10是终端组件 - 必须为服务提供服务的组件)。

在查询中,不可用的组件应在FailedComponents CTE中枚举其ID:

with FailedComponents (id) as (
  select id from (values
    (1), (7), (8)
    ) x(id)
), OnlineComponents (compId, reliesOn) as (
  select compId, reliesOn
  from scratch
  where compId = 0 -- <== entry point here
  union all
  select s.compId, s.reliesOn
  from scratch s
  join OnlineComponents c
    on s.compId = c.reliesOn
  where s.compId not in (select id from FailedComponents)
)
select case
  when exists (select * from OnlineComponents where compId = reliesOn)
    then 'Service is available'
    else 'No Service'
  end as [Status]
;

以下是SQL Fiddle,您可以在其中尝试使用不同的值来查看查询如何处理它们。

请注意,查询不使用Full表中指定的Partial / scratch依赖类型。依赖类型是从上下文中推断出来的。基本上,如果有一种方法可以将图形从0到10仅在可用组件上行走,那么整个服务就可用了。

另外,还有一点警告:递归CTE的深度有限。这称为MAXRECURSION,默认值为100.如果需要,您需要将其更改为更高的值。