简体   繁体   中英

How do I make this complex query run faster?

This query is taking between 20-40 seconds to run. I need to speed it up greatly if possible.

SELECT DISTINCT a.category, a.key 
FROM system_permissions AS a, system_permission_to_role AS b, 
system_user_to_role AS c, system_users AS d 
WHERE 
( 
    ( 
        a.system_permission_id=b.system_permission_id 
        AND (b.system_role_id=c.system_role_id || c.system_role_id = 0) 
        AND a.system_permission_id NOT IN ( 
            SELECT system_permission_id FROM system_permission_exclusions AS f 
            WHERE d.system_user_id=f.system_user_id 
        ) 
        AND c.system_user_id=d.system_user_id 
    ) 
    OR a.system_permission_id IN ( 
        SELECT system_permission_id 
        FROM system_permission_inclusions AS g 
        WHERE d.system_user_id=g.system_user_id 
    ) 
) 
    AND d.ldap_objectguid = '?'; 

The reason behind doing it this way is that I am creating exclusion and inclusion tables for permissions that fall outside of the standard defined roles, so first I need to exclude ones that are part of the role but exist in the exclusion table, then I need to add ones that are NOT part of their role, but exist in the inclusion table.

I am open to the idea of redesigning the tables also.

Does this work?

SELECT DISTINCT P.category, P.key 
FROM system_users U
LEFT OUTER JOIN system_permission_inclusions PI ON PI.system_user_id = U.system_user_id
INNER JOIN system_user_to_role UR ON UR.system_user_id = U.system_user_id
INNER JOIN system_permission_to_role PR ON PR.system_role_id = UR.system_role_id
INNER JOIN system_permissions P ON P.system_permission_id = PR.system_permission_id OR P.system_permission_id = PI.system_permission_id
WHERE U.ldap_objectguid = '?'
  AND P.system_permission_id NOT IN (SELECT system_permission_id FROM system_permission_exclusions WHERE system_user_id = U.system_user_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM