Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL indices and order

This is a question that I've had forever.

As far as I know the order of indices matter. So an index like [first_name, last_name] is not the same as [last_name, first_name], right?

If I only define the first index, does it mean that it will only used for

SELECT * FROM table WHERE first_name="john" AND  last_name="doe";  

and not for

SELECT * FROM table WHERE  last_name="doe" AND first_name="john"; 

Since I am using a ORM, I have no idea in which order these columns are going to be called. Does that mean that I have to add indices on all permutations? That is doable if I have a 2 column index, but what happens if my index is on 3 or 4 columns?

like image 895
Julien Genestoux Avatar asked Aug 09 '09 21:08

Julien Genestoux


People also ask

Does MySQL use index in order by?

Yes, MySQL uses your index to sort the information when the order is by the sorted column. Also, if you have indexes in all columns that you have added to the SELECT clause, MySQL will not load the data from the table itself, but from the index (which is faster).

Do indexes affect order by?

Yes, index will help you, when using ORDER BY. Because INDEX is a sorted data structure, so the request will be executed faster.

Does the order of columns in an index matter MySQL?

So the order of columns in a multi-column index definitely matters. One type of query may need a certain column order for the index. If you have several types of queries, you might need several indexes to help them, with columns in different orders.

How does MySQL choose which index to use?

If there is a choice between multiple indexes, MySQL normally uses the index that finds the smallest number of rows (the most selective index). If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows.


2 Answers

Index order matters when your query conditions only apply to PART of the index. Consider:

  1. SELECT * FROM table WHERE first_name="john" AND last_name="doe"

  2. SELECT * FROM table WHERE first_name="john"

  3. SELECT * FROM table WHERE last_name="doe"

If your index is (first_name, last_name) queries 1 and 2 will use it, query #3 won't. If your index is (last_name, first_name) queries 1 and 3 will use it, query #2 won't. Changing the condition order within WHERE clause has no effect in either case.

Details are here

Update:
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 (last_name, first_name) index because it's only based on first_name and first_name is NOT the leftmost prefix of the (last_name, first_name) index.

The order of conditions WITHIN the query does not matter; query #1 above will be able to use (last_name, first_name) index just fine because its conditions are first_name and last_name and, taken together, they DO form a leftmost prefix of (last_name, first_name) index.

like image 106
ChssPly76 Avatar answered Sep 21 '22 06:09

ChssPly76


ChssPly76 is correct that the order of boolean expressions does not have to match the order of columns in the index. Boolean operators are commutative, and the MySQL optimizer is smart enough to know how to match the expression to the index in most cases.

I also want to add that you should learn how to use the EXPLAIN feature of MySQL so you can see for yourself which indexes the optimizer will choose for a given query.

like image 29
Bill Karwin Avatar answered Sep 20 '22 06:09

Bill Karwin