Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Otimize a consulta do Oracle


A consulta a seguir produz os mesmos resultados?
SELECT  c.role_id
        , c.subgroup
        , c.subgroup_description
        , COUNT(a.USER_ID) user_count 
FROM    cmp_role c
        LEFT OUTER JOIN (
          SELECT  r.user_id
                  , r.role_id 
          FROM    (
                    SELECT  r.user_id
                            , r.role_id
                            , r.participant_code
                            , MAX(r.status_id) 
                    FROM    user_role r
                            INNER JOIN cmp_role c ON c.role_id = r.role_id
                    WHERE   r.participant_code IS NOT NULL 
                            AND c.group_id = 3 
                    GROUP BY 
                            r.user_id
                            , r.role_id
                            , r.participant_code 
                    HAVING  MAX(r.status_id) IN (
                              SELECT  status_id 
                              FROM    user_role
                              WHERE   (Active = 1 
                                      OR (Active IN ( 0,3 ) 
                                          AND SYSDATE BETWEEN effective_from_date AND effective_to_date)
                                        )
                            ) 
                  ) r
                  INNER JOIN user e ON e.user_id = RTRIM(r.user_id)        
        ) a ON a.role_id = c.role_id         
WHERE   c.group_id = 3
GROUP BY 
        c.role_id
        , c.subgroup
        , c.subgroup_description 
ORDER BY 
        c.subgroup

É mais fácil de ler (para mim). Se produzir os mesmos resultados, você já pode começar descartando r.participant_code e MAX(r.status_id) da seleção interna.