EXPLAIN SELECT DISTINCT main.*
FROM Users main
    CROSS JOIN ACL ACL_4
    JOIN Principals Principals_1
        ON Principals_1.id = main.id
    JOIN CachedGroupMembers CachedGroupMembers_2 
        ON CachedGroupMembers_2.MemberId = Principals_1.id
    JOIN Groups Groups_3
        ON Groups_3.id = CachedGroupMembers_2.GroupId
WHERE
    Principals_1.Disabled = '0'
    AND ACL_4.PrincipalType = Groups_3.Type
    AND Principals_1.id != '1'
    AND Principals_1.PrincipalType = 'User'
    AND ACL_4.RightName = 'OwnTicket'
    AND Groups_3.Domain =  'RT::Queue-Role'
    AND (ACL_4.ObjectType = 'RT::Queue' OR ACL_4.ObjectType = 'RT::System')
ORDER BY main.Name ASC;

SELECT ACL_4.PrincipalType, COUNT(id)
FROM ACL ACL_4
WHERE
    ACL_4.RightName = 'OwnTicket'
    AND (ACL_4.ObjectType = 'RT::Queue' OR ACL_4.ObjectType = 'RT::System')
GROUP BY ACL_4.PrincipalType;

EXPLAIN SELECT ACL_4.PrincipalType
FROM ACL ACL_4
WHERE
    ACL_4.RightName = 'OwnTicket'
    AND (ACL_4.ObjectType = 'RT::Queue' OR ACL_4.ObjectType = 'RT::System');

SELECT COUNT(Groups_3.id)
FROM ACL ACL_4, Groups Groups_3
WHERE
    ACL_4.PrincipalType = Groups_3.Type
    AND ACL_4.RightName = 'OwnTicket'
    AND Groups_3.Domain =  'RT::Queue-Role'
    AND (ACL_4.ObjectType = 'RT::Queue' OR ACL_4.ObjectType = 'RT::System');

EXPLAIN SELECT Groups_3.id
FROM ACL ACL_4, Groups Groups_3
WHERE
    ACL_4.PrincipalType = Groups_3.Type
    AND ACL_4.RightName = 'OwnTicket'
    AND Groups_3.Domain =  'RT::Queue-Role'
    AND (ACL_4.ObjectType = 'RT::Queue' OR ACL_4.ObjectType = 'RT::System');

