多列索引还需要单列索引吗?

Is a single-column index needed when having multicolumn index?

我掉进了一个设计糟糕的系统。现在我在他们的数据库上做DBA,我有很多情况如下(伪代码):

Table t1{
c1;
c2;
c3;
c4;

key(c1);
key(c2);
key(c1,c2);
key(c1,c2,c3);}

单列索引真的有必要吗,因为我已经有了一个包含这些列的多列索引?

或者另一方面 - 是否需要多行列,因为我已经有了单列列?

c1 上的单列索引是多余的。两列索引与三列索引也是冗余的。

您唯一需要的两个索引是 (c2)(c1, c2, c3)

MySQL 在复合索引上有 pretty good documentation

有关 how MySQL uses indexes 的文档页面的简短摘录:

If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to look up rows. For example, if you have a three-column index on (col1, col2, col3), you have indexed search capabilities on (col1), (col1, col2), and (col1, col2, col3). For more information, see Section 8.3.5, “Multiple-Column Indexes”.

您最好删除 (c1)(c1,c2) 上的索引。它们未被使用,但它们使用存储 space 并消耗处理器功率以在 table 数据更改时保持 up-to-date。