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

落爺英雄遲暮 提交于 2019-12-11 03:24:20

问题


I've been dropped in a system that was poorly designed. Now I'm doing DBA on their DB, and I have a lot of situation like the following (Pseudo-code):

Table t1{
c1;
c2;
c3;
c4;

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

Are the single column indexes really necessary, since I already have a multicolumn one containing those columns?

Or on the other hand - is the multiline column needed since I already have the single column ones?


回答1:


A short excerpt from the documentation page about 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”.

You better remove the indexes on (c1) and (c1,c2). They are not used but they use storage space and consume processor power to be kept up-to-date when the table data changes.




回答2:


The single column index on c1 is redundant. The two column index is redundant with the three column index as well.

The only two indexes you need are on (c2) and (c1, c2, c3).

MySQL has pretty good documentation on composite indexes.



来源:https://stackoverflow.com/questions/48827695/is-a-single-column-index-needed-when-having-multicolumn-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!