I have a vehicle table with many rows. Every vehicle is stored with a name, and then additional characterizing attributes. Let's say
name PRIMARY KEY
attr_1
attr_2
attr_3
attr_4
etc...
The user can search via a web form by name and then add filters for each attribute if they wish. I want to add some DB indexes to improve search performance for every use-case.
The user query could result into any combination of columns being added to the WHERE clause. For example:
SELECT * FROM vehicles WHERE name = ?
SELECT * FROM vehicles WHERE name = ? AND attr_1 = ?
SELECT * FROM vehicles WHERE name = ? AND attr_3 = ?
SELECT * FROM vehicles WHERE name = ? AND attr_1 = ? AND attr_2 = ? AND attr_4 = ?
SELECT * FROM vehicles WHERE name = ? AND attr_4 = ?
etc...
How should I best set up the indexes? As individual single column indexes? Or as multi-column indexes?
Do I need to cover all combinations, e.g.
Index for name, attr_1, attr_2, attr_3, attr_4
Index for name, attr_2, attr_3, attr_4
Index for name, attr_3, attr_4
Index for name, attr_4
Index for name, attr_1, attr_3, attr_4
etc...
Or do I analyze for which filter combinations happen most often and only target indexes for those use cases? I don't know if there is some elegant solution for this problem?