0

我尝试在 SQL Server 中使用图形功能。现在我遇到了软删除问题。

我有以下图表

[1] -> [2] -> [9 (deleted)] -> [4]

当我运行以下脚本时

CREATE TABLE MyNode 
(
    [Id] [bigint] NOT NULL,
    [IsDeleted] [bit] NOT NULL,
) AS NODE;

CREATE TABLE MyEdge 
(
    State [int] NOT NULL
) AS EDGE;

INSERT INTO MyNode (Id, IsDeleted)
VALUES (1, 0), (2, 0), (4, 0), (9, 1);

INSERT INTO MyEdge
VALUES
( (SELECT $node_id FROM MyNode WHERE Id = 1), (SELECT $node_id FROM MyNode WHERE Id = 2), 1),
( (SELECT $node_id FROM MyNode WHERE Id = 2), (SELECT $node_id FROM MyNode WHERE Id = 9), 1),
( (SELECT $node_id FROM MyNode WHERE Id = 9), (SELECT $node_id FROM MyNode WHERE Id = 4), 1)
;

SELECT
    src.Id ID_SOURCE
,   LAST_VALUE(trgt.Id) WITHIN GROUP (GRAPH PATH) AS ID_TARGET
,   STRING_AGG(trgt.Id, '->') WITHIN GROUP (GRAPH PATH) AS ID_CHAIN
--, STRING_AGG(compare.State, '->') WITHIN GROUP (GRAPH PATH) AS STATE_CHAIN
--, STRING_AGG(trgt.IsDeleted, '->') WITHIN GROUP (GRAPH PATH) AS DELETED_CHAIN
FROM
    MyNode AS src
,   (   SELECT
            *
        FROM
            MyEdge
        WHERE
            State = 1
    ) FOR PATH AS compare
,   (   SELECT
            *
        FROM
            MyNode
        WHERE
            IsDeleted = 0
    ) FOR PATH AS trgt
WHERE
    MATCH ( SHORTEST_PATH( src(-(compare)->trgt)+ ) )
    AND src.Id = 1;

SELECT
    src.Id AS SOURCE_ID
,   ed.State AS EDGE_STATE
,   trgt.Id AS TARGET_ID
FROM
    MyNode AS src
,   MyEdge AS ed
,   MyNode AS trgt
WHERE
    MATCH( src-(ed)->trgt )
    AND src.Id = 2;


DROP TABLE MyNode;
DROP TABLE MyEdge;

(提示:这只适用于 SQL-Server 2019)

我得到以下结果

ID_SOURCE ID_TARGET ID_CHAIN
1 2 2
1 4 2->4

没有优势2->4,但是2->9->4。但是节点 9 被删除,因此它用于图遍历,但在输出中被抑制。

这是 SQL-Server 中的错误还是我做错了什么?

还是有另一种方法我应该使用 Graph-DB 的软删除?

4

2 回答 2

0

您可以过滤掉来自/到软删除节点的边缘

....
(   
SELECT e.*
FROM MyEdge as e
WHERE
    e.State = 1
    and exists(select * from MyNode as x where x.IsDeleted = 0 and x.$node_id = e.$to_id)
    --and exists(select * from MyNode as x where x.IsDeleted = 0 and x.$node_id = e.$from_id)
) FOR PATH AS compare
....

不知何故,以下查询返回错误(执行几秒钟后):

SELECT
    src.Id ID_SOURCE
,   LAST_VALUE(trgt.Id) WITHIN GROUP (GRAPH PATH) AS ID_TARGET
,   STRING_AGG(trgt.Id, '->') WITHIN GROUP (GRAPH PATH) AS ID_CHAIN
--, STRING_AGG(compare.State, '->') WITHIN GROUP (GRAPH PATH) AS STATE_CHAIN
--, STRING_AGG(trgt.IsDeleted, '->') WITHIN GROUP (GRAPH PATH) AS DELETED_CHAIN
FROM
    MyNode AS src
,   
(   
SELECT e.*
FROM MyEdge as e
WHERE
    e.State = 1
    and exists(select * from MyNode as x where x.IsDeleted = 0 and x.$node_id = e.$to_id)
    --and exists(select * from MyNode as x where x.IsDeleted = 0 and x.$node_id = e.$from_id)
) FOR PATH AS compare

,   (   SELECT
            *
        FROM
            MyNode
        WHERE
            IsDeleted = 0
    ) FOR PATH AS trgt
WHERE
    MATCH ( SHORTEST_PATH( src(-(compare)->trgt)+ ) )

...并且通过向 src 表附加条件/引用来消除错误,这始终是 true :

AND src.Id = src.Id --or src.IsDeleted = src.IsDeleted

肯定有问题 [Microsoft SQL Server 2019 (RTM-CU8-GDR) (KB4583459)]..

于 2021-02-04T12:10:36.060 回答
0

将 isDeleted 标志合并到边缘表中,并在相应的(到/从)节点被标记为软删除时更新该标志,并在边缘表子查询上使用 isDeleted 是首选方法。我们不建议直接比较 $node_id 和 $from_id ,而是通过 MATCH 函数隐式比较。

于 2021-06-03T20:30:39.597 回答