I have 9 columns in my table homes_info
table
- home_id
- location_id (foreign key from
locations
table)(int) - price (int)
- floors(int)
- rooms(int)
- rental income (float)
- payback (float)
- offer type (varchar)
- date_published
On front-end, i provide many input fields (filters) which allow users to do a select query on one or more than one column in every possible combination they can also sort the results by any column.
so the query can be as complicated as the following query
SELECT * FROM homes_info LEFT JOIN location on locations.location_id = homes_info.location_id WHERE locations.location_name = 'herford' && price < 3000 && price > 1000 && floors < 3 && floors > 1 && rooms < 3 && rooms > 1 && rental income < 100 && rental_income > 150 && payback < 900 && payback > 100 && offer_type = 'rent' ORDER BY any_column_user_want
and the query can be as simple as the following
SELECT * FROM homes_info LEFT JOIN location on locations.location_id = homes_info.location_id WHERE floors > 1 ORDER BY any_column_user_want
And a user can do any possible query between above two extreme queries
So how should I use indexes on above 7 columns, should I give a different index to each column (except home_id
and location_id
)?
ALTER TABLE homes_info ADD INDEX(price)
ALTER TABLE homes_info ADD INDEX(floors)
ALTER TABLE homes_info ADD INDEX(rooms)
..................................
ALTER TABLE homes_info ADD INDEX(published_date)
but that will make MySQL do merge indexes for bigger queries, I am not sure how bad that is?
to avoid that I can use composite indexes
ALTER TABLE homes_info ADD INDEX( price, floors, rooms, rental income, payback, offer type, published_date)
ALTER TABLE homes_info ADD INDEX(floors, rooms, rental income, payback, offer type, published_date)
ALTER TABLE homes_info ADD INDEX(floors, rooms, rental income, payback, offer type, published_date)
..................................................
..................................................
ALTER TABLE homes_info ADD INDEX(published_date)
So which method would be better? use a different index on each column or use composite indexes?