WHERE a=1 AND y='m'
is best handled by INDEX(a,y)
in either order. The optimal set of indexes is several pairs like that. However, I do not recommend having more than a few indexes. Try to limit it to queries that users actually make.
INDEX(a,b,c,d)
:
WHERE a=1 AND b=22 -- Index is useful
WHERE a=1 AND d=44 -- Index is less useful
Only the "left column(s)" of an index are used. Hence the second case, uses a
, but stops because b
is not in the WHERE
.
You might be tempted to also have (active, year, model)
. That combination works well for active AND year
, active AND year AND model
, but not active AND model
(but no year
).
More on creating indexes: http://mysql.rjweb.org/doc.php/index_cookbook_mysql
Since model
implies a make
, there is little use to put both of those in the same composite index.
year
is not very selective, and users might want a range of years. These make it difficult to get an effective index on year
.
How many rows will you have? If it is millions, we need to work harder to avoid performance problems. I'm leaning toward this, but only because the lookup would be more compact.