Thesql multiple column index Cardinality order

index-tuningMySQLmysql-5.6optimizationquery-performance

How does Multiple-Column Indexes work in MySQL? How is it stored internally?
consider a table with Multiple-Column Indexes on two columns (X,Y).

If X has High-cardinality than Y i.e., X is less duplicated than Y, what should be the order of multiple-column index X,Y or Y,X.

If it is X,Y there is going to be lesser number of rows with same X value. will this improve the performance because B-tree look up of X will be faster?

Best Answer

Not sure if this answers your question, but I would not bother so much about the individual cardinality of X and Y. For index design I would start with the predicates that the index should assist with. If you have queries like:

where X = ?
where Y = ? and X = ?  

go with (X, Y). If you have queries like:

where Y = ?
where Y = ? and X = ?  

go with (Y, X). If your queries are like:

where X = ?
where Y = ?
where Y = ? and X = ?  

go with (Y, X) and (X, Y). If your queries are like:

where X = ?
where Y = ?

go with (X) and (Y)

I assume here that the cardinality for each column is greater than 1. For cardinality in general I consider the full key cardinality (all involved columns together) to be of more importance than the individual cardinality.