MySQL actual combat common index and the choice of unique index


Preface

This article learned how developers should choose an index


Tip: The following is the content of this article, the following cases are for reference

Choice of ordinary index and unique index

There is a premise here, that is , the business code in the system guarantees that it will not write two identical values ​​to the target field . This premise is very ideal.

So at this time, for the problem of adding a common index to the target table or setting a unique index selection for the target field, how to make a choice to maximize
performance ? Performance can be analyzed from the two operations of query and update

We assume that the new common index field isk

1. Query process

Since ordinary indexes do not make uniqueness constraints , so

  1. For a normal index , when the first k==5field is found, it will continue to search downwards until the first record that does not meet the where condition is found
  2. For a unique index , when the first record that meets the condition is found, the query process is terminated.However
    , the performance gap between the above two operations is very small.

The reason lies in the data read and write mechanism of innoDB

2. InnoDB's data read and write mechanism

Due to great cost to read and write disk I / O operations, so the data is read when it is first data from disk into memory , and a page unit organization .
Now that the data stored in the memory, and that the general index The search method is just a matter of moving the pointer a few more times , and the speed is not much different from the unique index.

3. Update process

Similarly, affected by the innoDB read and write mechanism, when a row of data needs to be updated, if the data is already stored in the memory , it will be updated directly.
But what if the data has not been read into the memory? At this time, another technical point needs to be introduced. change buffer the

Previously said, for the cost of disk read and write is great. Therefore, InnoDB will update operations to change buffer cache in . The next time access the data page, the data page is read (from disk data read this The page is not updated, that is, it saves the data before the update ). Then InnoDB will do a cached operation in the change buffer (this process is called merge) to ensure data consistency

Performance analysis:
The target page that needs to be updated is in memory (assuming the index value to be inserted is 3):

  • For a unique index , find records with indexes 2 and 4, determine whether they meet the uniqueness constraint , and insert records between the two
  • For ordinary index , find the record between index 2/4 and insert data

Obviously, there is almost no performance difference in this case

The target page that needs to be updated is not in memory

  • For a unique index , you need to read the data from the disk into the memory, determine whether it meets the uniqueness constraint, and then insert the data
  • For ordinary indexes , write the update operation to the change buffer , and that's it~

At this time, the performance of the ordinary index is higher, and the unique index will cause a series of problems such as: system blockage, memory utilization is not high

4.change buffer

The change buffer is copied in memory, and it also needs to be stored on disk for persistence, which is essentially persisted data

Usage scenario: The change buffer is limited to ordinary indexes , especially for businesses that read more and write less , which has a greater performance improvement.However
, the change buffer also has its limitations: when the update operation is completed, the data must be read immediately In terms of business, the merge operation will be triggered frequently , which is counterproductive

Change buffer and redo log: Both are used to record database operations. However, redo log is mainly used to save the IO consumption of random write disks , and the change buffer is used to save the IO consumption of random read disks

5. Suggestions for Index Selection

Specific analysis of specific problems, for the ideal situation set at the beginning, the index is guaranteed to be unique, and we can choose to use the ordinary index.
However, if the business code cannot guarantee the uniqueness, then it is the old practical unique index!

Guess you like

Origin blog.csdn.net/qq_45596525/article/details/115015711