为什么MySQL在添加新索引时会删除现有索引?

时间:2016-03-11 15:17:56

标签: mysql

我正在向表中添加组合索引,但该索引会删除已存在的索引(请参阅fk_service_catalog_parameter_component如何消失)。知道为什么吗?

mysql> show indexes from service_catalog_parameter;                                    
+---------------------------+------------+----------------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                     | Non_unique | Key_name                               | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------+------------+----------------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| service_catalog_parameter |          0 | PRIMARY                                |            1 | id           | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_resource  |            1 | resource_id  | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_component |            1 | component_id | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------------------------+------------+----------------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

mysql> ALTER TABLE `service_catalog_parameter` ADD UNIQUE INDEX `resource_parameter_component_unique` (`component_id`, `resource_id`, `parameter_name`);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show indexes from service_catalog_parameter;                                                                                                     
+---------------------------+------------+---------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                     | Non_unique | Key_name                              | Seq_in_index | Column_name    | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------+------------+---------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| service_catalog_parameter |          0 | PRIMARY                               |            1 | id             | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| service_catalog_parameter |          0 | resource_parameter_component_unique   |            1 | component_id   | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          0 | resource_parameter_component_unique   |            2 | resource_id    | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          0 | resource_parameter_component_unique   |            3 | parameter_name | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_resource |            1 | resource_id    | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------------------------+------------+---------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
5 rows in set (0.00 sec)

mysql> 

当我更改索引中列的顺序时,我的脑袋完全爆炸。

mysql> show indexes from service_catalog_parameter;                                
+---------------------------+------------+----------------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                     | Non_unique | Key_name                               | Seq_in_index | Column_name  | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------+------------+----------------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| service_catalog_parameter |          0 | PRIMARY                                |            1 | id           | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_resource  |            1 | resource_id  | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_component |            1 | component_id | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------------------------+------------+----------------------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.01 sec)

mysql> ALTER TABLE `service_catalog_parameter` ADD UNIQUE INDEX `resource_parameter_component_unique` (`parameter_name`, `component_id`, `resource_id`);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show indexes from service_catalog_parameter;                                                                                                     
+---------------------------+------------+----------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table                     | Non_unique | Key_name                               | Seq_in_index | Column_name    | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+---------------------------+------------+----------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| service_catalog_parameter |          0 | PRIMARY                                |            1 | id             | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| service_catalog_parameter |          0 | resource_parameter_component_unique    |            1 | parameter_name | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| service_catalog_parameter |          0 | resource_parameter_component_unique    |            2 | component_id   | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          0 | resource_parameter_component_unique    |            3 | resource_id    | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_resource  |            1 | resource_id    | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
| service_catalog_parameter |          1 | fk_service_catalog_parameter_component |            1 | component_id   | A         |           0 |     NULL | NULL   | YES  | BTREE      |         |               |
+---------------------------+------------+----------------------------------------+--------------+----------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
6 rows in set (0.00 sec)

mysql> 

0 个答案:

没有答案