如果我在 (a, b) 上有一个复合索引,我知道仅与“a”相关的查询仍将使用复合索引(但与“b”相关的查询则不使用)
我的问题是,如果我有 (a, b) 索引,是否有正当理由在 'a' 上使用单列索引?关于 (a,b) 索引是 a 的完全替代,还是仅仅是“聊胜于无”的索引,我读过的内容似乎含糊不清。
这假设我同时按 a 和 a,b 进行过滤。我有一个包含太多索引的表,这些索引会影响写入性能,我想在删除索引之前仔细检查,我非常确定这些索引没有任何好处。
此外,这个答案是否会根据我使用的是 InnoDb 还是 MyISAM 而改变?涉及的表是MyISAM,但我们的表大部分是InnoDb。
最佳答案
您的 (a,b) 索引也将处理仅涉及“a”的查询,并且不需要单独针对 (a) 的索引。
来自 the documentation :
If the table has a multiple-column index, any leftmost prefix of the index can be used by the optimizer to find 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).
关于mysql - mysql复合索引会让其他一些索引完全多余吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5022947/