From my own research (but I'm not a DBA expert), I learned that there are two things to consider when deciding on a composite key index.
First, with regard to column power, an index is usually better suited for finding high power columns. Therefore, I would be inclined to put the column with the highest power first in the index. For reference, there is an article called MySQL Query Optimization , which states:
Indexes work best for high power columns compared to the number of rows in the table (i.e. columns with many unique values ββand multiple duplicates).
In your case, the _id columns will better fit this definition, so they will be the best candidate for a key prefix.
Another thing to consider is the reuse of these indexes. Most (if not all) database systems allow you to use the composite key prefix for reuse. For example, the composite key on (owner_id, owner_type) can also be used by requests on owner_id , but not on owner_type .
So, from what you explained in your question, you might be better off with two indexes: the composite key index at (owner_id, owner_type) and the other at (owner_type) .
Finally, it really comes down to your datasets and queries. Try several scenarios, tests using different composite keys to find out what is the most optimal solution. Also, do not forget that indexes are penalized for writing to your tables.
Refresh . There is also another fairly popular question about a complex key index:
When should I use a composite index?
Nicolas buduroi
source share