[英]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.