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 

(表名:阻止列表)

    user_id | blocked_id
        1     74
        1     70
        1     94
        1     81

(表名:友情)

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

(表名:用户)

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

墙桌

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

谢谢!!!

4

2 回答 2

2

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)...

于 2011-02-20T10:25:27.237 回答
0

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

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

于 2011-03-01T09:15:06.477 回答