简体   繁体   English

索引列以便在MySQL 5.6或更高版本中更快地进行查询

[英]Indexing columns for faster querying in MySQL 5.6 or higher

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. 我有一个名为属性的表,就像主表,包含所有类型的属性(土地,公寓等等)的所有公共列(10列)然后我有每个属性类型的特定表,因为每种类型有一些具体的专栏。 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? 现在用户可以根据几乎任何这些列或它们的组合过滤他的搜索,那么我应该如何将我的索引策略放在列上(用户可以根据价格,面积,面积和价格,卧室数量和这些组合的位置等等。另外一点是property_description和property_title是文本所以我必须在每个文本上添加全文索引,对吧? 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 我已经阅读了一些说自从mysql 5.6以来优化器中有一些东西使用了多个索引,所以你可以在每一列上放一个索引,但我不知道这是不对的...请建议,因为我不是那么好在处理数据库性能

5.7 has JSON tricks. 5.7有JSON技巧。 MariaDB 10 has Dynamic Columns with similar tricks. MariaDB 10具有类似技巧的动态列。

The main principle: Expose the more useful fields; 主要原则:揭示更多有用的领域; throw the more obscure fields into JSON or Dynamic columns. 将更加模糊的字段放入JSON或动态列中。 Then let MySQL filter on the former, and your app takes care of further filtering on the latter. 然后让MySQL过滤前者,你的应用程序负责进一步过滤后者。

More discussion . 更多讨论

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM