Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 960
Lestat86 Avatar asked Mar 06 '23 21:03

Lestat86


1 Answers

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.

like image 189
axiac Avatar answered Mar 23 '23 19:03

axiac