繁体   English   中英

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

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

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

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

到目前为止,除了进行 2 个单独的查询和手动处理分页之外,我不知道有更好的方法,如果我们将来除了 Posted/Tweeted 之外添加另一种新的边缘类型,它会变得低效且麻烦。

在 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

我的图表数据

使用异构边或节点视图。 请参阅答案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