开发者

Is it a problem that I somehow have managed to get two indexes with the same name in a MySQL table?

开发者 https://www.devze.com 2023-01-06 16:17 出处:网络
Somehow I managed to get two indexes named user_id, as shown below. Should I drop, rename and rebuild one of them, or is this no problem?

Somehow I managed to get two indexes named user_id, as shown below. Should I drop, rename and rebuild one of them, or is this no problem?

SHOW INDEXES FROM core_item;

+-----------+------------+-----------+--------------+-----------------+-----------+---------开发者_JAVA技巧----+----------+--------+------+------------+---------+
| Table     | Non_unique | Key_name  | Seq_in_index | Column_name     | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+-----------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+
| core_item |          0 | PRIMARY   |            1 | id              | A         |      593642 |     NULL | NULL   |      | BTREE      |         | 
| core_item |          0 | user_id   |            1 | user_id         | A         |       11416 |     NULL | NULL   |      | BTREE      |         | 
| core_item |          0 | user_id   |            2 | product_id      | A         |      593642 |     NULL | NULL   |      | BTREE      |         |
+-----------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+


It's a single composite index covering 2 columns.

I think that output of SHOW CREATE TABLE core_item is easier to understand.


You have just one index, but it is across two fields - it is a composite key on user_id and product_id. It would be created like this:

ALTER TABLE core_item ADD INDEX `user_id` (`user_id`, `product_id`);

It might be worth renaming it to something else to save any future confusion, but only if the rename will not affect any existing queries that specify indexes directly.

0

精彩评论

暂无评论...
验证码 换一张
取 消