在 MYSQL 中,如果有重复的索引,其中除 key_name 之外的所有内容都相同,这意味着什么?

In MYSQL, what does it mean when there are duplicate indices where everything but key_name is the same?

describe etc_category_metadata;

+---------------------+---------------+------+-----+---------+----------------+
| Field               | Type          | Null | Key | Default | Extra          |
+---------------------+---------------+------+-----+---------+----------------+
| id                  | int(11)       | NO   | PRI | NULL    | auto_increment |
| user_id             | bigint(20)    | NO   |     | NULL    |                |
| time_updated        | int(11)       | YES  |     | NULL    |                |
| category_type       | int(11)       | YES  | MUL | NULL    |                |
| status_keywords     | mediumblob    | YES  |     | NULL    |                |
| page_keywords       | mediumblob    | YES  |     | NULL    |                |
| profession_keywords | mediumblob    | YES  |     | NULL    |                |
| adgroup_ids         | mediumblob    | YES  |     | NULL    |                |
| prod                | tinyint(1)    | YES  |     | 0       |                |
| version             | int(11)       | YES  |     | 1       |                |
| status              | int(11)       | YES  |     | 0       |                |
| dep_category_ids    | mediumblob    | YES  |     | NULL    |                |
| custom_param        | mediumblob    | YES  |     | NULL    |                |
| queue_priority      | int(11)       | YES  |     | 1       |                |
| auto_requeue_num    | int(11)       | YES  |     | 0       |                |
| cloned_version      | int(11)       | YES  |     | 0       |                |
| custom_query        | varchar(1000) | YES  |     | NULL    |                |
| description         | varchar(1000) | YES  |     | NULL    |                |
| error_message       | mediumblob    | YES  |     | NULL    |                |
| time_last_completed | int(11)       | YES  |     | NULL    |                |
+---------------------+---------------+------+-----+---------+----------------+
21 rows in set (0.40 sec)

show index from etc_category_metadata;
+-----------------------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                 | Non_unique | Key_name        | Seq_in_index | Column_name   | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-----------------------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| etc_category_metadata |          0 | PRIMARY         |            1 | id            | A         |       12613 |     NULL | NULL   |      | BTREE      |         |               |
| etc_category_metadata |          0 | category_type   |            1 | category_type | A         |       12613 |     NULL | NULL   | YES  | BTREE      |         |               |
| etc_category_metadata |          0 | category_type   |            2 | version       | A         |       12613 |     NULL | NULL   | YES  | BTREE      |         |               |
| etc_category_metadata |          0 | category_type_2 |            1 | category_type | A         |       12613 |     NULL | NULL   | YES  | BTREE      |         |               |
| etc_category_metadata |          0 | category_type_2 |            2 | version       | A         |       12613 |     NULL | NULL   | YES  | BTREE      |         |               |
| etc_category_metadata |          0 | category_type_3 |            1 | category_type | A         |       12613 |     NULL | NULL   | YES  | BTREE      |         |               |
| etc_category_metadata |          0 | category_type_3 |            2 | version       | A         |       12613 |     NULL | NULL   | YES  | BTREE      |         |               |
+-----------------------+------------+-----------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
7 rows in set (0.07 sec)

正在尝试确定删除密钥 category_type_2category_type_3 是否安全。看起来它们是与 category_type 完全相同的索引。这是我不知道很久以前是谁创造的遗产 table。有人最终会创建这三个看似重复的密钥是否有正当理由?

是的,您在同一列上有重复的索引,所以 mysql 如果您没有给索引 name.IMO,只需添加一个数字即可,mysql 甚至不允许重复的 [=16] =] 可以安全删除它们

DROP INDEX category_type_2  ON etc_category_metadata

并为其他人做同样的事情

没有充分的理由在相同的列上以相同的顺序建立多个索引。在当前 MySQL 数据库上发出这样的创建索引语句仍然会成功(出于向后兼容性原因),但会发出警告:

Duplicate index 'index_name' defined on the table 'db.table_name'. This is deprecated and will be disallowed in a future release.

如果您有此类预先存在的重复索引,则没有理由不删除它们。

有两个相同的索引是对磁盘的浪费space并且会减慢INSERTs(一点点)。没有任何好处。

您无法真正看出它们是否与 DESCRIBE TABLE 重复。相反,做 SHOW CREATE TABLE。注意 UNIQUE/不,prefixed/not,常规/FULLTEXT,手动创建/由 FOREIGN KEY 创建,等等

一旦您确定它们相同(名称除外),请放弃一个。在其他情况下,也可能会删除索引。假设您有这 3 个索引:

INDEX(a,b)  -- keep this
INDEX(a)    -- unnecessary
INDEX(b)    -- keep

或者这对:

UNIQUE(a)   -- keep; same as INDEX(a), plus a uniqueness check
INDEX(a)    -- drop

更微妙,考虑这对:

INDEX(a,b)  -- keep; provides composite index
UNIQUE(a)   -- keep; provides uniqueness check

(还有更多组合。)