"Mysql - Index (select unique index and general index) Change Buffer"

I. Overview

  - If the business has to ensure that the code is not written to duplicate the ID number.

  - From a performance point of consideration , you will be for the ID number, select a unique index or ordinary index it? What is it based on selected?

  - This is from their execution looks.

 

Two: unique index and the general index of inquiry process?

  - E.g

    -  statement executes the query is select id from T where k = 5

 

  - Process query to find in the index tree

    - first through the B + tree from the root beginning , search by layer to the leaf nodes, the positioning data page .

    - internal data pages to locate a record by dichotomy. 

 

  - for the average index for

    - found to meet the conditions of the first recording k = 5, one needs to find the next record.

    - up against the first recording does not satisfy k = 5 condition.

 

  - For a unique index for

    - Due to the uniqueness of the index definition, look to continue a search of the records meet the conditions, it will stop. 

 

Three: compare query performance unique index and the general index?

  -  the difference is minimal performance of

 

  - the reason

    - InnoDB data is by reading and writing data in units of pages.

      - When you need to read a record when the record itself will not read out from the disk, but in the pages (InnoDB, the size of each data page default is 16KB) as a unit, in its entirety read into memory.

    - Well, for the average index is to do more of that time "to find the next record and judgment" of the operation, we only need to find one pointer and one calculation .

    - Of course, if k = 5 the last record is a record that exactly data page, then to remove a record, the next data page must be read, this operation will be slightly more complicated.

      - However, for integer field, a data page can put nearly a thousand key, therefore the probability of occurrence of this situation will be low.

      - So, when we calculated the average performance difference, it can still be considered the operating costs for the current CPU is negligible. 

 

Four: Now that consistent query performance, their performance update it, before discussing the update, we look at the InnoDB optimization for updated Chage Buffer.

 

五:Change Buffer

  - Principle (When you need to update a data time)

    - If the data page directly updated in memory .

 

    - if the data page is not in memory .

      - without affecting data consistency premise, InooDB will operate these updates in change buffer cache, so that you do not need to read into the data page from disk.

      - the next time a query needs to access the data pages, the data page is read into memory and then execute change buffer in operations related to this page.

      - In this way we can guarantee the correctness of the data logic. 

 

  - Change buffer when it will actually update?

    - Although the name is called change buffer, in fact it is persistent data .

    - that is, Change Buffer there is a copy in memory will be written to disk .

    - The operation of change buffer is applied to the original data page, get the latest result of a process called Merge .

    - In addition to access the data page will trigger merge, the system has a background thread periodically merge.

    - a database normally closed (the shutdown) process, also performing merge operations.

 

  - Why Change Buffer?

    - Obviously, if we can update the first record in change buffer, reducing disk read speed of execution of the sentence will be significantly improved.

    - Moreover, the data is read into memory need to occupy buffer pool, so this method can also be enough to avoid occupation of memory, improve memory utilization. 

 

  - Change buffer usage scenarios?

    - Change the action because merge when the real time data were updated, and the main purpose is to change buffer cache record down

      - So before a data page do merge, the more change change buffer records (that is, the more times on this page to be updated), the greater the benefits.

 

    - Thus, for Write Once Read Many small businesses , the probability of a page immediately after finished access to relatively small, then use the best effect change buffer.

      - a common business model on a bill type, logging class system.

 

    - Conversely, suppose a updated model of business is done immediately after writing the query.

      - even if the conditions are satisfied, the first update record in change buffer, but after due immediately to access the data page, it will immediately trigger the merge process.

      - so the number of random access IO will not be reduced, but increased the maintenance costs of change buffer.

      - So, for this business model is, Change Buffer but played side effects. 

 

Six: look at performance issues in a unique index and the general index?

  - unique index

    - All of the update operation must first determine whether the operation violates a unique constraint.

    - For example, to insert (4,400) this record, we must first determine now whether the table already exists k = 4 record, which must want to read data pages into memory to judgment.

    - If you have already read into memory, and that memory will be faster update directly, no need to use the change buffer.

    - Therefore, the update can not use a unique index change buffer, in fact, only the general index can be used.

 

  - General Index

    - Normal use change buffer update.

 

  - in conclusion

    - In fact, these two types of indexes on query capabilities is no difference, the main consideration is the impact on the performance of the update.

    - If all behind updates are immediately associated with a query for the record, then you should turn off change buffer. In other cases, change buffer can improve the performance update.

    - In actual use, you will find, with the general index and change buffer is used for optimization of large volumes of data to update the table is obvious. 

 

Seven: Redo log and Change Buffer

  - ability

    -  Redo log for writing memory, complete the change, provide cash-safe capability.

    -  Change Buffer for record updates, batch update.

 

  - Common Workflow

    - update data

      - in memory, direct memory update;

      - not in memory, just change buffer area of ​​memory, to record "I want to change xxx" this information

    -  The operation of the above-described two of the credited redo log.

    - the transaction is completed.

    - the cost of performing this update statement is very low, that is, two memory write, then write a disk (two operations together to write a disk), but also the order in writing.

 

  -Advantage

    - Redo log major savings random write disk IO consumption (converted into sequential write), and Change Buffer major savings are random read IO consume disk.

Guess you like

Origin www.cnblogs.com/25-lH/p/10967830.html