阻塞朋友sql(超慢sql??)

发布于 2024-10-18 04:06:42 字数 1861 浏览 0 评论 0原文

你好,我在这个 sql 上遇到了问题。基本上它会显示我的朋友发布的状态,并过滤掉我阻止的朋友。

虽然sql是slooooowwwwwww..处理它需要2秒。看来是什么问题呢。 (我在 SQL 下面列出了表格和一些示例)提前致谢! sql...

SELECT from_user_id,content,wall.date, wall.wall_type, users.id, users.displayname 
            FROM wall

            INNER JOIN friendship ON ( wall.from_user_id =friendship.user_a OR wall.from_user_id = friendship.user_b ) 
            INNER JOIN users ON (wall.from_user_id = users.id)WHERE users.id not in (select blocked_id from blocklist where user_id = 1) and (wall.wall_type = 'home' OR wall.wall_type = 'profile' or wall.wall_type = 'topro') AND (
                  (friendship.user_a = 1 and friendship.user_b = wall.from_user_id)
                or  
                    (friendship.user_a = wall.from_user_id and friendship.user_b = 1)    or (wall.user_id_of_wall = 1 or type_id = 1 or from_user_id = 1))GROUP BY wall_id ORDER BY date DESC LIMIT 10 

(表名:blocklist)

    user_id | blocked_id
        1     74
        1     70
        1     94
        1     81

(表名:friendhip)

user_a   user_b status  date
1        93         1   1297323354
1        79         1   1297323409
1        81         1   1297323403
1        82         1   1297323398
1        85         1   1297323389
1        90         1   1297323367
1        89         1   1297323373

(表名:users)

       id   displayname
        1   Kenny  Jack
        8   Wale Robinson
        7   Victor WIlliams
        6   Micheal Harris
        9   Micheal Boston
        10  Yestor Smith

wall 表

wall_id wall_type   user_id_of_wall   type_id   from_user_id    content      viewed   date
   5    profile        8                    8          8         Just chilling! 0   1296858001

谢谢!!!

Hi im having trouble with this sql. Basically it displays statuses that my friends posted, and it filters out the friends that i blocked.

The sql is slooooowwwwwww though.. It takes 2 seconds to process it. What seems to be the problem. (ive listed the tables and some examples of how it looks below the sql) thanks in advance!
The sql....

SELECT from_user_id,content,wall.date, wall.wall_type, users.id, users.displayname 
            FROM wall

            INNER JOIN friendship ON ( wall.from_user_id =friendship.user_a OR wall.from_user_id = friendship.user_b ) 
            INNER JOIN users ON (wall.from_user_id = users.id)WHERE users.id not in (select blocked_id from blocklist where user_id = 1) and (wall.wall_type = 'home' OR wall.wall_type = 'profile' or wall.wall_type = 'topro') AND (
                  (friendship.user_a = 1 and friendship.user_b = wall.from_user_id)
                or  
                    (friendship.user_a = wall.from_user_id and friendship.user_b = 1)    or (wall.user_id_of_wall = 1 or type_id = 1 or from_user_id = 1))GROUP BY wall_id ORDER BY date DESC LIMIT 10 

(table name: blocklist)

    user_id | blocked_id
        1     74
        1     70
        1     94
        1     81

(table name:friendhip)

user_a   user_b status  date
1        93         1   1297323354
1        79         1   1297323409
1        81         1   1297323403
1        82         1   1297323398
1        85         1   1297323389
1        90         1   1297323367
1        89         1   1297323373

(table name:users)

       id   displayname
        1   Kenny  Jack
        8   Wale Robinson
        7   Victor WIlliams
        6   Micheal Harris
        9   Micheal Boston
        10  Yestor Smith

the wall table

wall_id wall_type   user_id_of_wall   type_id   from_user_id    content      viewed   date
   5    profile        8                    8          8         Just chilling! 0   1296858001

THANKS!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

歌入人心 2024-10-25 04:06:42

NOT IN 用于固定值,请改用 NOT EXISTS :

SELECT from_user_id,content,wall.date, wall.wall_type, users.id, users.displayname 
            FROM wall

            INNER JOIN friendship ON ( wall.from_user_id =friendship.user_a OR wall.from_user_id = friendship.user_b ) 
            INNER JOIN users ON (wall.from_user_id = users.id)
WHERE not exists (select * from blocklist where users.id = blocked_id AND user_id = 1) and wall.wall_type IN ('home', 'profile', 'topro') AND (
                  (friendship.user_a = 1 and friendship.user_b = wall.from_user_id)
                or  
                    (friendship.user_a = wall.from_user_id and friendship.user_b = 1)    or (wall.user_id_of_wall = 1 or type_id = 1 or from_user_id = 1))
GROUP BY wall_id
ORDER BY date DESC LIMIT 10 

[编辑] 使用 IN 表示固定值 (wall_type)...

NOT IN is used for fixed values, use NOT EXISTS instead :

SELECT from_user_id,content,wall.date, wall.wall_type, users.id, users.displayname 
            FROM wall

            INNER JOIN friendship ON ( wall.from_user_id =friendship.user_a OR wall.from_user_id = friendship.user_b ) 
            INNER JOIN users ON (wall.from_user_id = users.id)
WHERE not exists (select * from blocklist where users.id = blocked_id AND user_id = 1) and wall.wall_type IN ('home', 'profile', 'topro') AND (
                  (friendship.user_a = 1 and friendship.user_b = wall.from_user_id)
                or  
                    (friendship.user_a = wall.from_user_id and friendship.user_b = 1)    or (wall.user_id_of_wall = 1 or type_id = 1 or from_user_id = 1))
GROUP BY wall_id
ORDER BY date DESC LIMIT 10 

[EDIT] Use IN for fixed values (wall_type)...

拥有 2024-10-25 04:06:42

始终在只读查询上使用WITH (NOLOCK)!

另外,请删除 JOIN 子句中的所有括号,您是否从 MS Access 复制并粘贴了此内容?

always use WITH (NOLOCK) on read-only queries!

also, please remove all your paranthesis on your JOIN clause, did you copy and paste this from MS Access?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文