Pergunta

Olá, estou tendo problemas com este sql.Basicamente, ele exibe os status que meus amigos postaram e filtra os amigos que bloqueei.

O sql é slooooowwwwwww embora ..Demora 2 segundos para processá-lo.Oque parece ser o problema.(listei as tabelas e alguns exemplos de como fica abaixo do sql) desde já agradeço!O 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 

(Nome da tabela:lista de bloqueio)

    user_id | blocked_id
        1     74
        1     70
        1     94
        1     81

(nome da tabela: amizade)

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

(nome da tabela: usuários)

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

a mesa de parede

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

OBRIGADO!!!

Foi útil?

Solução

NOT IN é usado para valores fixos; em vez disso, use 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 

[EDIT] Use IN para valores fixos (wall_type)...

Outras dicas

sempre use WITH (NOLOCK) em consultas somente leitura!

Além disso, remova todos os parênteses da cláusula JOIN. Você copiou e colou isso do MS Access?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top