High performance MySQL


This series of tutorials has been completely updated. Through the learning of this series of tutorials, you can deeply understand the underlying algorithm, implementation and principle of MySQL, and then through the practice part of the drill, you can gain insight into how to optimize MySQL queries, how to backup and rollback database operations, and how to perform master slave replication and read write separation, so as to create high-performance Highly available MySQL cluster.

You need to upgrade to a subscriber to read all the tutorial content , you can upgrade to a subscriber by clicking the button below (ignored by subscribers):

Upgrade to a subscriber now

The content outline is as follows:

Overall architecture

Index and query optimization

Query tips: for count Aggregated query, some students may be curious to use count(*) still count(id) The query performance is better. For the InnoDB engine, MySQL is dedicated to count(*) Optimized, and count(id) The full table will be scanned and then accumulated row by row, so it is recommended to use count(*) Some people may wonder why InnoDB does not record the number of records in the whole table as MyISAM does, because InnoDB supports transactions, and there is an MVCC mechanism in transactions (described in detail in the following transaction chapter). Each record may have multiple versions at the same time. Therefore, the specific number of rows is uncertain. In addition, for the table fields that often need to be counted, we will design the data table based on the anti normal form design Redundant field To store, such as the number of article views, the number of video views, the number of goods purchased, or through Cache system These methods are all used to improve query performance.

Database Transactions

Note: The following transaction tutorials are limited to the InnoDB engine.

How can MySQL transactions solve the problem of phantom reads at the repeatable read level: We know that InnoDB supports row locks, which can be used to lock a row before updating (modifying/deleting) it. However, for the insert operation, the row inserted in advance does not exist, so it is impossible to add a row lock. Therefore, MySQL introduces the concept of gap lock, That is, the gap between the lines to be inserted is locked (both ends are (- ∞, MIN) and (MAX,+∞)), and an adjacent gap lock and row lock are combined to form a Next key Lock, which is an interval that is opened before and closed after (gap lock+row lock), Next key Lock is the basic unit of MySQL locking, and only objects accessed in the query process can be locked. If the query condition of an SQL locking statement contains an equivalent query of a unique index (including the primary key), the lock will degenerate into a row lock. Therefore, for an insert statement, because Next key Lock is set, Therefore, other transactions can be blocked from reading the corresponding row and gap, thus avoiding the phantom reading problem.

Database high availability

Operation and Maintenance Tips Sharing: Deleting a database does not have to run away: Binlog logs can be used not only to build a highly available database cluster, but also to recover data that has been deleted by mistake. If a data line has been deleted by mistake, it can be used Flashback The tool recovers the corresponding data in combination with the binlog whose log format is ROW; If the database/table is deleted by mistake, the corresponding data can be recovered based on the full backup (the entire database data of the scheduled backup)+incremental backup (binlog); If the entire database instance is deleted by mistake (through a disk file deletion command such as rm), and if a database cluster has been built based on binlog, you don't need to worry about this. You just need to remove this node and synchronize the data of other nodes.

Practical Optimization (Free)

Note: The following practical optimization chapter takes Laravel model class database operation as an example for demonstration

Suggestions for daily database optimization: make SQL query statements simple and easy to optimize through advanced database design, instead of piling up complex SQL statements that are difficult to optimize to obtain data.


give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: There is no previous article

>>Next: MySQL database installation and server startup principle