0

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;

+-----------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+
| 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      |         |
+-----------+------------+-----------+--------------+-----------------+-----------+-------------+----------+--------+------+------------+---------+

2 Answers 2

7

It's a single composite index covering 2 columns.

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

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.