Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it necessary to have an index on every combination of queryable fields in a SQL table to optimize performance?
    primarykey
    data
    text
    <p>If my <code>User</code> table has several fields that are queryable (say DepartmentId, GroupId, RoleId) will it make any speed difference if I create an index for each combination of those fields?</p> <p>By "queryable", I'm referring to a query screen where the end user can select records based on Department, Group or Role by selecting from a drop-down.</p> <p>At the moment, I have a index on DepartmentId, GroupId and RoleId. That's a single non-unique index per field.</p> <p>If an end user selects "anyone in Group B", the SQL looks like:</p> <pre><code>select * from User where GroupId = 2 </code></pre> <p>Having an index on GroupId should speed that up.</p> <p>But if the end user select "anyone in Group B and in Role C", the SQL would look like this:</p> <pre><code>select * from User where GroupId = 2 and RoleId = 3 </code></pre> <p>Having indexes on GroupId and RoleId individually may not make any difference, right?</p> <p>A better index for that search would be if I had one index spanning <strong>both</strong> GroupId and RoleId.</p> <p>But if that's the case, than that would mean that I would need to have an index for every combination of queryable fields. So I would need all these indexes:</p> <ul> <li>DepartmentId</li> <li>GroupId</li> <li>RoleId</li> <li>DepartmentId and GroupId</li> <li>DepartmentId and RoleId</li> <li>GroupId and RoleId</li> <li>Department Id, GroupId and RoleId</li> </ul> <p>Can anyone shed some light on this? I'm using MySQL if that makes a difference.</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.
 

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