PostgreSQL 递归 CTE 太慢

问题描述

我正在使用此查询生成类似于 Instagram 的关注者建议。但是这个查询太慢了。有时需要 30 秒才能完成。我可以在物化视图中使用这个查询吗?如果是,那么我如何为物化视图重写它?
如果答案是否定的,那么您可以建议任何其他类似的方法

    WITH RECURSIVE suggestions (leader_id,follower_id,depth) AS (
    SELECT leader_id,1 AS depth FROM followers WHERE follower_id = 1000
          UNION 
    SELECT followers.leader_id,followers.follower_id,depth + 1 
    FROM followers 
        JOIN suggestions ON followers.follower_id = suggestions.leader_id 
    WHERE depth < 3
    ) 
    SELECT disTINCT users.id,users.username 
    FROM users 
    JOIN suggestions ON users.id = suggestions.follower_id 
    WHERE depth = 2;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)