I am looking into adding a composite index to a table in a MYSQL database which will likely be several million rows in size. The composite will be comprised of two varchar c
As a rule of thumb, in a multi-column index, you want the columns that have the highest cardinality, or in other words, the highest number of distinct values, to come first in the index.
To be more accurate, you want the column with the fewest possible matches to your search criteria first so you can narrow the result set down as much as possible, but in general, it's the same as the highest cardinality.
So, in your example, you'll want the column that will have millions of distinct values to be in the index before the one with only 6 distinct values.
Assuming you're selecting only one row out of the millions of values, it allows you to eliminate more rows faster.
When considering two columns of similar cardinality, put the smaller one first (INTEGER columns before VARCHAR columns) because MySQL can compare and iterate over them faster.
One caveat is that if you are selecting with ranges (eg. WHERE datecol > NOW()
), then you want the range columns farthest to the right, and your columns with a single constant (eg. WHERE id = 1
) to the left. This is because your index can only be used for searching and ordering up to the point of the first range value.