如果一個表被定義爲對列具有唯一約束,那麼如果我們希望將其索引爲快速查找的索引,是否還需要在該列上定義單獨的索引?MySql - 如果一列中的索引被定義爲唯一的,它是否是冗餘索引?
例如,如下表索引:
mysql> show index from FRONTIER;
+----------+------------+-------------------------+--------------+--------------+-----------+-------------+----------+--------+------+-------------+---------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment |
+----------+------------+-------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
| FRONTIER | 0 | PRIMARY | 1 | ID | A | 0 | NULL | NULL | | BTREE | |
| FRONTIER | 0 | uniq_cnstr | 1 | HOST_ID | A | 0 | NULL | NULL | | BTREE | |
| FRONTIER | 0 | uniq_cnstr | 2 | HASH_PATHQRY | A | 0 | NULL | NULL | | BTREE | |
| FRONTIER | 1 | I_FRONTIER_HASH_PATHQRY | 1 | HASH_PATHQRY | A | 0 | NULL | NULL | | BTREE | |
+----------+------------+-------------------------+--------------+--------------+-----------+-------------+----------+--------+------+------------+---------+
是索引 'I_FRONTIER_HASH_PATHQRY' 多餘,唯一約束 'uniq_constr'?
(注意,唯一約束跨越2列)
即使唯一索引跨越兩列嗎? – Joel 2010-11-03 12:21:23
@Joel:如果該列是領先的,則不必創建單獨的索引。但是,它仍在拖尾,所以你仍然必須這樣做。 – Quassnoi 2010-11-03 12:22:31
謝謝,所以如果我理解正確,如果在唯一約束中的兩列的順序是顛倒的,那麼在'前導'列'I_FRONTIER_HASH_PATHQRY'上不需要額外的索引? – Joel 2010-11-03 12:24:26