Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When does MySQL update the indexes

When exactly are the indexes updated after an update/insert? Is it before the update/insert query returns, is it sometime after after the query returns, or is it when a query using the indexes is executed.

like image 553
snppla Avatar asked Jun 24 '13 18:06

snppla


People also ask

Do indexes automatically update?

The Database Engine automatically modifies indexes whenever insert, update, or delete operations are made to the underlying data.

Does MySQL update automatically?

An auto-updated column is automatically updated to the current timestamp when the value of any other column in the row is changed from its current value. An auto-updated column remains unchanged if all other columns are set to their current values.

Does MySQL automatically index?

MySQL doesn't provide an automatic indexing feature.

How long does it take MySQL to build an index?

The table is partitioned into 40 pieces on column text . Then creating index on the table takes about 1 hours to complete.


2 Answers

Indexes updates are two-fold.

The first part is inserting/updating/deleting the entry into/from the index. The index gets updated as soon as a record is changed, and this process blocks the query until completion.

This allows faster retrieval of records based on an a condition on an indexed column, the most well-understood purpose of an index.

The second part is updating statistics of the index. This allows the optimiser to determine if for a given query it is even worth using the index. Imagine a query like SELECT * FROM users WHERE disabled = 0. Assume most users are in fact active. If the index statistics are up-to-date, the optimiser will realise that most records from the table will be returned by the query, the table will almost entirely need to be scanned. It will likely decide not to use the index and scan the table straight away.

This update does not take place automatically except in very specific situations. These statistics should be manually updated on a regular basis with an ANALYZE TABLE [table_name]

like image 124
RandomSeed Avatar answered Sep 21 '22 01:09

RandomSeed


You may want to refer to the following links which I found helpful for your question:

  1. This question on stackoverflow
  2. This link from mysqlperformanceblog
  3. This post from DBA Stackexchange forum
like image 28
krishnang Avatar answered Sep 21 '22 01:09

krishnang