I know there are many Q's about this issue.
I have read many of them.
I know that for performance in general it is better to use the high cardinality fields (most selective) first in the index.
having said that, I want to branch of a comment that was made in: Order of columns in a multi-column index in MySQL and make it an actual Q.
say that I save country id (low cardinality) and person id (high cardinality).
same person id can exist in several countries.
I sometimes need to retrieve all people from a country, and sometimes need to reach a specific person from a specific country.
For the country query I need an index on country or on that starts with country. but what about the second query?
Should I use a single index with country_id,person_id (which is sort of "wrong order of fields" cardinality wise) or add another index person_id,country_id and thus have two indexes - one for each of those queries?
How would that change if:
1) I also needed sometimes to reach all the people with the same person_id (regardless of country) ?
2) instead of country I had a hierarchy of continent,country,region,city,person ?
I don't know how much does it matter but if it does - assume the Q is about MySQL.
UPDATE 1
I am not sure I was clear enough...
Because I said that I will need to ask about all the people in a country ( i.e. ... where country_id=?
) it is obvious that I should have an index on country...
When I need to query on both country and person I can create a query like:
... where country_id=? and person_id=?
and just extend the index to be (country_id,user_id)
and use it for both queries, or I could leave the country index alone (for ... where country_id=?
queries) and add a new (user_id,country_id)
and write my two field queries as ... where person_id=? and country_id=?
which is more efficient.
so option 1: single but less efficient (country_id,user_id)
index querying for ... where country_id=? and person_id=?
or ... where country_id=?
option 2: two indexes (country_id)
index and (person_id,country_id)
index for ... where country_id=?
and ... where person_id=? and country_id=?
Having written this like this I also recognize that the second option also provides an answer to 1st Q variant where a ...where person_id=?
query is also needed...
but still for the basic scenario - would you go with option 1 or 2 ... ?