我的表homes_info
中有9列
locations
表中的外键)(int)在前端,我提供了许多输入字段(过滤器),允许用户以一种可能的组合对一个或多个列进行选择查询,他们还可以按任意列对结果进行排序。
因此查询可能与以下查询一样复杂
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
查询可以像下面这样简单
SELECT * FROM homes_info LEFT JOIN location on locations.location_id = homes_info.location_id WHERE floors > 1 ORDER BY any_column_user_want
用户可以在上述两个极端查询之间进行任何可能的查询
那么我应该如何在以上7列上使用索引,是否应该给每列一个不同的索引(home_id
和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)
但是这会使MySQL为更大的查询合并索引,我不确定那有多糟?
为避免我可以使用复合索引
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)
那么哪种方法更好?在每列上使用不同的索引还是使用复合索引?