【问题标题】:SQL Server 图表以获取连接到节点的多个节点类型
【发布时间】:2021-04-24 23:51:25
【问题描述】:

我计划在我的一个项目中使用 SQL Server 2019 图形功能。数据方案如下图所示。

给定用户(ID:2356,名称:Mark),我想检索用户的追随者完成的所有帖子和推文,这些帖子和推文按发布时间或推文时的限制/分页时间排序总体结果。

到目前为止,除了进行 2 个单独的查询和手动处理分页之外,我不知道有更好的方法,如果我们将来除了已发布/推文之外添加另一种新的边缘类型,这将使​​其效率低下且也很麻烦。

有没有更好的方法来解决 SQL Server 图中的此类用例?

SELECT mainUser.*, followingUser.*, followerPost.*
FROM 
     User mainUser, Follows userFollows, User followingUser, Posted followerPosted, Post followerPost 
WHERE 
     MATCH (mainUser-(userFollows)->followingUser-(followerPosted)->followerPost)
AND
     mainUser.id=2356
ORDER BY
     followerPosted.posted_on desc
SELECT mainUser.*, followingUser.*, followerTweet.*
FROM 
     User mainUser, Follows userFollows, User followingUser, Tweeted tweeted, Tweet followerTweet 
WHERE 
     MATCH (mainUser-(userFollows)->followingUser-(tweeted)->followerTweet)
AND
     mainUser.id=2356
ORDER BY
     tweeted.tweeted_on desc

【问题讨论】:

    标签: sql-server graph-databases sql-server-2019 sql-server-graph sql-graph


    【解决方案1】:

    使用异构边或节点视图。见答案https://stackoverflow.com/a/70055567/3434168

    ---- there may be column colisions due to UNION ALL so fix them as you need
    ---- it doesn't matter which columns you select in your view
    ---- the MATCH algorithm (probably) uses metadata of the VIEW
    CREATE VIEW v_SecondTierEdges AS
      SELECT *, 'Tweeted' AS Type FROM Tweeted
      UNION ALL
      SELECT *, 'Posted' AS Type FROM Posted
    GO
    
    CREATE VIEW v_SecondTierNodes AS
      SELECT tweeted_on AS did_that_on, 'Tweet' AS Type FROM Tweet
      UNION ALL
      SELECT posted_on AS did_that_on, 'Post' AS Type FROM Post
    GO
    
    SELECT
      mainUser.*, followingUser.*, followerTweet_or_Post.*
    FROM 
      User mainUser, Follows userFollows, User followingUser, v_SecondTierEdges tweeted_or_posted, v_SecondTierNodes followerTweet_or_Post 
    WHERE 
      MATCH (mainUser-(userFollows)->followingUser-(tweeted_or_posted)->followerTweet_or_Post)
    AND
      mainUser.id=2356
    ORDER BY
      tweeted_or_posted.did_that_on desc
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 1970-01-01
      • 2011-05-29
      • 1970-01-01
      相关资源
      最近更新 更多