Suppose I
What indexes should I create and is creating multiple indexes with the same columns in differing order a horrible idea?
(a, b)
(a, c)
(a, b, c)
(a, c, b)
...
(a, b, c, d)
(a, d, c, b)
...
Theoretically, if you totally don't care about update/insert speed and excessive indexes size (disk space), then you will need all possible combinations of columns used in WHERE
clause of the query, and the query planner will decide which one to use. But whether the index will be useful at all depends on the table data.
The order of index columns plays very important role. The columns should be ordered by cardinality. Let's look at the example:
We have a table of people (id, surname, firstname, year_of_birth, sex). What indexes are appropriate here?
Which index should we add - (surname, firstname) or (firstname, surname)? The right answer is (surname, firstname), because if you group records by surname, it will certainly have higher number of records.
Let's say we already have (surname, firstname) index. Should we change it to (surname, firstname, year)? There might be some benefit, but I'm doubtful about that. For any given surname & firstname, there will be probably just a few records with different age. The point is that if we have almost unique combination (like surname + first name), then adding more columns to the index won't help much, if at all.
No index needed. Because there are only two possible values: male/female. So the index will not be efficient.
Apart from that indexes are great and essential, a few things I'd like to note:
A good point from the documentation: Combining Multiple Indexes
In all but the simplest applications, there are various combinations of indexes that might be useful, and the database developer must make trade-offs to decide which indexes to provide. Sometimes multicolumn indexes are best, but sometimes it's better to create separate indexes and rely on the index-combination feature.