Is the index re-built completely or is the index updated? If it is updated then what exactly is updated?
Assume InnoDB is being used.
Is the index re-built completely or is the index updated? If it is updated then what exactly is updated?
Assume InnoDB is being used.
All indexes for a table in MySQL are "immediately" updated (not rebuilt) as a row is INSERTed
into that table. Ditto for DELETE
. In some cases, UPDATE
causes index update(s).
By "immediately", I mean that you cannot tell whether it is finished before control is returned to you, or whether there is some form of caching going on.
Most indexes in MySQL are BTrees. In a few cases, there is FULLTEXT, SPATIAL, or HASH.
Adding an entry to a BTree involves drilling down the "tree" (~3 levels for a million-row table) and adding a 'record' in the leaf node. This is fast enough that you cannot tell whether it is done live.
If you have a dozen indexes, then there are a dozen BTrees (or whatever) to update. This suggests you should not have more indexes than you need.
In InnoDB the PRIMARY KEY
is "clustered". That is, the data and the PRIMARY KEY
live together in a single BTree, ordered by the PRIMARY KEY
and containing all the data.
In InnoDB, each 'record' in a secondary index (also structured as a BTree) contains a copy of the PRIMARY KEY
. (This may be what Zafar is alluding to.)
A BTree index is very efficient for
WHERE key BETWEEN 22 AND 44
)In short you can say that in innodb, every index is associated with clustured index (normally known as primary key) so whenever any index value updated then it (changed value) will again associated with clustered index.