Indexing columns for faster querying in MySQL 5.6 or higher

时间:2016-04-15 15:03:37

标签: mysql indexing entity-attribute-value

I'm building a real estate app. I have a table called properties which is like the main table that has all common columns (10 columns) for all types of properties (lands, apartments, ... etc) and then I have a specific table for each property type since each type has some specific column. here is the property table:

CREATE TABLE `properties` (
  `property_id` int(11) NOT NULL AUTO_INCREMENT,
  `property_type` int(11) DEFAULT NULL,
  `property_title` varchar(255) NOT NULL,
  `property_description` varchar(1000) NOT NULL,
  `country_id` int(11) NOT NULL,
  `city_id` int(11) NOT NULL,
  `city_location_id` int(11) NOT NULL,
  `price` int(11) DEFAULT NULL,
  `area` decimal(7,2) DEFAULT NULL,
  `latitude` decimal(10,8) DEFAULT NULL,
  `longitude` decimal(11,8) DEFAULT NULL,
  `entry_date` datetime NOT NULL,
  `last_modification_date` datetime NOT NULL,
  PRIMARY KEY (`property_id`)
) 

and here is the apartments for example:

CREATE TABLE `apartments` (
  `apartment_id` INT NOT NULL COMMENT '',
  `num_of_bedrooms` INT NULL COMMENT '',
  `num_of_bathrooms` INT NULL COMMENT '',
  `num_of_garages` INT NULL COMMENT '',
  PRIMARY KEY (`apartment_id`)  COMMENT '',
  CONSTRAINT `properties_apartments_fk`
    FOREIGN KEY (`apartment_id`)
    REFERENCES `aqar_world`.`properties` (`property_id`)
    ON DELETE CASCADE
    ON UPDATE NO ACTION);

now the user can filter his search based on almost any of these columns or a combination of them, so how should I put my indexing strategy on the columns (the user could filter based on price, area, area and price, number of bedrooms and location and so on with these so many combinations) .. another point is that the property_description and property_title are texts so I'll have to add a fulltext index on each of them, right? also there is a join between these two tables and also between them and some other table (like agents tables for example).

I've read some say since mysql 5.6 there something in the optimizer that makes use of multiple indexes so you can put an index on each column but I don't know if that is right .. please advice since I'm not that good in taking care of DB performance

1 个答案:

答案 0 :(得分:0)

5.7有JSON技巧。 MariaDB 10的动态列具有类似的技巧。

主要原则:揭示更多有用的领域;将更加模糊的字段放入JSON或动态列中。然后让MySQL过滤前者,你的应用程序负责对后者进行进一步过滤。

More discussion