[英]SQL adjacency list query
尝试对用户访问的服务的依赖关系进行建模。 我创建了一个父/子邻接表样式表,该表列出了“服务”依赖于组件1,它依赖于组件2,等等,依此类推,例如完全或部分依赖
该图显示了布局-依赖关系图
比较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中查找并向上/跨越/向下依赖项以找出路径是否仍然存在?
希望这是有道理的
谢谢
显然,结果查询并非易事。 我向您发布了一种方法,您可以在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
只需检查结果结果中的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
节点。
我设法编写了一个使用递归CTE执行此操作的查询。
首先,我随意将另外两个记录添加到scratch
表中:
INSERT scratch SELECT 12, 'Component 9', 9, 10, 'Full'
INSERT scratch SELECT 13, 'Users', 10, 10, 'Full'
这意味着:
在查询中,不可用的组件应在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 ,您可以在其中尝试不同的值以查看查询如何处理它们。
请注意该查询不使用scratch
表中指定的Full
/ Partial
依赖类型。 依赖类型是从上下文中推断出来的。 基本上,如果有一种方法可以将图从0跨到10,仅对可用组件进行遍历,则整个服务都是可用的。
还有一点警告:递归CTE的深度是有限的。 这称为MAXRECURSION
,默认值为100。如果需要,您需要将其更改为更高的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.