0

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

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

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

是否有更好的方法来解决 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

我的图表数据

4

1 回答 1

0

使用异构边或节点视图。请参阅答案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
于 2021-12-22T13:06:18.527 回答