简体   繁体   English

多列上的多个索引

[英]Multiple indexes on multiple columns

Lets say I have a decent sized MySQL table (that we'll call departments ) with a bunch of columns that cluster together like so: 可以说我有一个大小合适的MySQL表(我们称其为departments ),其中有一堆列聚集在一起,如下所示:

Departments Table: 部门表:

| id | ds_settings | ds_reports | sales_settings | sales_reports | eng_settings | eng_reports | ops_settings | ops_reports | queryable_id | queryable_type |
|----|-------------|------------|----------------|---------------|--------------|-------------|--------------|-------------|--------------|----------------|

So as far as columns are concerned, we have "settings" and we have "reports". 因此,就列而言,我们有“设置”和“报告”。 When this table is queried, it will typically be looking for just the all the settings or reports for a given "queryable" id and type. 查询此表时,通常将只查找给定“可查询” ID和类型的所有设置或报告。

So MOST queries to this table will end up looking something like this: 因此,对该表的MOST查询最终将看起来像这样:

SELECT ds_settings, sales_settings, eng_settings, ops_settings
    FROM departments
    where queryable_id = 1
      AND queryable_type = "User"

Why question is, what's a the correct way to index this table? 为什么会出现问题,索引该表的正确方法是什么? Does it make design sense to include an index that encompasses all of "settings" AND all of "reports", eg: 设计包含一个包含所有“设置”和所有“报告”的索引是否有意义,例如:

UNIQUE KEY `index_on_settings` (`queryable_id`,`queryable_type`,
                   `ds_settings`,`sales_settings`,`eng_settings`)

...or is this misunderstanding how compound indexes are supposed to work? ...或者这是对复合索引应该如何工作的误解?

When considering a key the following elements should be used for the index in order. 当考虑一个键时,以下元素应按顺序用于索引。 Fields used for: 字段用于:

  • joins 加入
  • where (constant fields) 哪里(常数字段)
  • where (range fields) 哪里(范围字段)
  • sorts 排序
  • group by 通过...分组

In this case you are searching by two fields by constant lookup value so keep those as the index. 在这种情况下,您将按常量查找值按两个字段进行搜索,因此请保留它们作为索引。 There is no need to impose a unique constraint. 无需施加唯一约束。

While you can include fields you retrieve in an index it has the downside of increasing the size of the entries in the index and making it slower to search the index. 虽然可以在索引中包含要检索的字段,但它不利于增加索引中条目的大小,并使搜索速度变慢。 If you had one small field there on an exceptionally common query it might be worth it however if your case it would seem premature. 如果您在一个非常普通的查询中只有一个小字段,那么值得,但是,如果您的情况似乎为时过早。

So: 所以:

ALTER TABLE departments ADD KEY index_on_settings (queryable_id, queryable_type)

I'm assuming id is a primary key. 我假设id是主键。

Recommend reading through https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html . 推荐阅读https://dev.mysql.com/doc/refman/8.0/en/mysql-indexes.html There is also a good presentation here on index usage https://github.com/jynus/query-optimization . 关于索引的使用,这里也有一个很好的介绍https://github.com/jynus/query-optimization

Two points in response to your question: 回答您的问题有两点:

  1. The index is to be built on the attributes in the WHERE clause, not on the attributes in the SELECT clause. 索引将建立在WHERE子句中的属性上,而不是建立在SELECT子句中的属性上。
  2. You should build the index on bare minimum attributes needed, because if you include more attributes than needed, then your inserts and updates will have to update the indices too, causing them to slow down. 您应该在所需的最低限度属性上建立索引,因为如果您包含的属性超过了所需的数量,那么插入和更新操作也必须更新索引,从而导致索引变慢。

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

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