Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Index order matters when your query conditions only apply to <strong>PART</strong> of the index. Consider:</p> <ol> <li><p><code>SELECT * FROM table WHERE first_name="john" AND last_name="doe"</code></p></li> <li><p><code>SELECT * FROM table WHERE first_name="john"</code></p></li> <li><p><code>SELECT * FROM table WHERE last_name="doe"</code></p></li> </ol> <p>If your index is (<code>first_name</code>, <code>last_name</code>) queries 1 and 2 will use it, query #3 won't. If your index is (<code>last_name</code>, <code>first_name</code>) queries 1 and 3 will use it, query #2 won't. Changing the condition order within WHERE clause has no effect in either case.</p> <p>Details are <a href="http://dev.mysql.com/doc/refman/5.0/en/mysql-indexes.html" rel="noreferrer">here</a></p> <p><strong>Update</strong>:<br> In case the above is not clear - MySQL can only use an index if the columns in query conditions form a leftmost prefix of the index. Query #2 above can not use (<code>last_name</code>, <code>first_name</code>) index because it's only based on <code>first_name</code> and <code>first_name</code> is NOT the leftmost prefix of the (<code>last_name</code>, <code>first_name</code>) index.</p> <p>The order of conditions WITHIN the query does not matter; query #1 above will be able to use (<code>last_name</code>, <code>first_name</code>) index just fine because its conditions are <code>first_name</code> and <code>last_name</code> and, taken together, they DO form a leftmost prefix of (<code>last_name</code>, <code>first_name</code>) index.</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