Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "The indexes PRIMARY and id seem to be equal and one of them could possibly be removed." mean?

Tags:

indexing

mysql

Indexes What does this mean and how can I fix it?

like image 214
Tyler Crompton Avatar asked Mar 31 '11 21:03

Tyler Crompton


1 Answers

You have two separate indexes on the same field of your table (id). One of them is implied by having set id as a PRIMARY KEY, the other you probably created explicitly. Only one of them is needed - having both of them may result in performance drop due to the additional index updates.

Just drop one of them to resolve this issue.

Having a PRIMARY KEY or UNIQUE constraint on a column (or field, if you wish) of a table essentially means that for each row inserted, the value of that column should be unique and therefore it should not already exist in the table. The naive approach would be to read all existing rows before inserting, but that would make the DB very slow once a number of rows has been inserted.

In order to deal with this, most (all?) decent database engines will implicitly create indexes for such fields, so that they can quickly detect if a value already exists in the table, without having to scan all its rows.

As a result, manually creating indexes on fields declared PRIMARY KEY or UNIQUE not only is redudant, but it may also cause performance loss due to the duplication of the work needed to maintain the indexes.

like image 187
thkala Avatar answered Sep 21 '22 06:09

thkala