简体   繁体   English

SQL 服务器图来获取连接到一个节点的多个节点类型

[英]SQL Server graph to fetch multiple node types connected to a node

I'm planning to use SQL Server 2019 graph features for one of my project.我计划在我的一个项目中使用 SQL Server 2019 图形功能。 The data scheme would look something like the picture below.数据方案如下图所示。

Given the user (Id: 2356, name: Mark), I would want to retrieve all the Posts and the Tweets done by the user's follower ordered by when it was posted or when it was tweeted together with a limit/pagination on the overall result.给定用户(ID:2356,名称:Mark),我想检索用户的追随者完成的所有帖子和推文,这些帖子和推文是在发布时间或推文时排序的,以及对整体结果的限制/分页.

As of now, I don't know of a better way other than doing 2 separate queries & manually handling pagination, which makes it inefficient & also cumbersome if we add another new edge type in future in addition to Posted/Tweeted.到目前为止,除了进行 2 个单独的查询和手动处理分页之外,我不知道有更好的方法,如果我们将来除了 Posted/Tweeted 之外添加另一种新的边缘类型,它会变得低效且麻烦。

Are there better ways to address such usecases in SQL Server graph?在 SQL 服务器图中是否有更好的方法来解决此类用例?

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

我的图表数据

Use heterogenous edge or node view.使用异构边或节点视图。 See answer https://stackoverflow.com/a/70055567/3434168 .请参阅答案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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM