Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>The query will be required to run on every page the user loads and we have over 30,000 permissions, and 3,000 roles. I am just trying <strong>very hard</strong> to keep my number of queries to a minimum. We also host our 7 sites on a server in our office, and the server doesn't seem to be capable of handling the amount of traffic we generate (unfortunately I have no control over this)</p> </blockquote> <p>I see that your intentions are good, but let me tell you this: </p> <p>The "number of queries" is not the right way to measure your site's performance. </p> <p>Many times 2 simple queries use less resources than 1 complex. </p> <p>Also there are other ways to speed up your sites: </p> <ul> <li>Assess if you really need all those roles and permissions loaded on each request. Maybe querying for only the role/permission needed is enough.</li> <li>Have proper indexes </li> <li>Utilize caching techniques to reduce the load (site content)</li> </ul> <p>So finally, I tried to come up with query to satisfy what was asked: </p> <pre><code>select * from ( select ur.role_id, p.* from user_roles ur left join permissions_role pr on ur.role_id = pr.role_id left join permissions p on p.id = pr.perm_id where ur.user_id = '$userid' union all select null as role_id, p.* from permissions_user pu join permissions p on p.id = pu.perm_id where pu.user_id = '$userid' ) sub group by ifnull(name,role_id) -- group by to eliminate duplicates </code></pre> <p>But this is not good on performance. You are much better off with 2 queries: 1st to get all permissions for user</p> <pre><code>select p.* from permissions p join permissions_role pr on pr.perm_id = p.id join user_roles ur on ur.role_id = pr.role_id and ur.user_id = '$userid' union select p.* from permissions p join permissions_user pu on pu.perm_id = p.id and pu.user_id = '$userid'; </code></pre> <p>and 2nd to get all roles. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload