mysql architecture and historical notes

The most important feature in MySQL is its storage engine architecture . This architecture separates query processing , other system tasks, and data storage and extraction . Because of this, MySQL can be used in most different scenarios and become one of the most widely used databases.

1. MySQL logical architecture

The figure below is the logical architecture of MySQL. The
image
picture is from BLOG
roughly three layers: the
first layer interacts with the client and provides functions such as connection processing, authorization authentication, and security assurance.
In the second layer, most of the core functions of MySQL are here. Including its various built-in functions as well as functions such as stored procedures, triggers, and views.
The third layer, this layer includes the storage engine, providing the function of storing and extracting data. The server communicates with the storage engine through the API. The storage engine is similar to the file system, both of which are used to store data. Common storage engines for MySQL include InnoDB, Memory, and MyISAM.
When each client connects to the MySQL server, there will be a thread corresponding to the execution of the task.
MySQL will parse the query and optimize it.

2. Concurrency Control

2.1 Read-write lock

The read lock is shared and non-blocking. Multiple clients can read the same resource at the same time without interfering with each other.
Write lock is exclusive, it will block other read locks or write locks. This ensures that in a given time, only one user's write is successful.

2.2 Lock granularity

Ideally, only the modified data is accurately locked. The smaller the amount of locked data, the higher the concurrency. But locking and managing locks also consume resources. MySQL provides a variety of options, and provides different lock strategies or lock granularities for different scenarios.
Table lock The most basic locking strategy, when a user performs write operations such as adding, deleting, modifying, etc., a table must first obtain a lock.
Row-level locking Row-level locking can support concurrency to the greatest extent, and it can only be implemented at the storage engine layer.

3. Affairs

A transaction can be seen as a set of atomic SQL queries or units of work. It has ACID characteristics, as follows.
Atomicity
cannot be divided, either success or failure rolls back.
Consistency (Consistency)
database always transition from a consistent state to another consistent state. The execution order of multiple atomic transactions is different and may not be consistent.
Isolation (Isolation)
When multiple concurrent transactions read, write and modify data, isolation can prevent inconsistencies to varying degrees. The following isolation level talks about this.
Durability (Durabili)
Once the transaction is committed, the changes made will be permanently saved in the database.
My understanding is that atomicity + isolation + durability can achieve the purpose of consistency .

3.1 Isolation level

Lower-level isolation can usually perform higher concurrency and lower system overhead.
Read UnCommitted (uncommitted read) transaction can read uncommitted data, commonly known as dirty read.
Read Committed (committed to read) at the beginning of a transaction, you can only "see" the changes made by the committed transaction. Also called non-repeatable reading.
The Repeatable Read (repeatable read ) change level ensures that the same result is consistent when the same transaction is read multiple times. But there is no way to solve the problem of phantom reading. While one transaction is reading, another transaction is writing. When the previous transaction is read again, a phantom line will be generated. Repeatable read is MySQL's ++ default transaction isolation level++.
Serializable (Serializable) is the highest isolation level. Force transactions to be executed serially to avoid phantom reads. Rarely used, suitable for situations where data consistency is emphasized and there is no concurrency. A phantom read means that when a transaction reads a record in a certain range, another transaction inserts a new record in the range, and when the current transaction reads or modifies the record in the range, a phantom row will be generated.
To summarize, the following table

Transaction isolation level Dirty read Non-repeatable Phantom reading
Uncommitted read Yes Yes Yes
Submit to read no Yes Yes
Repeatable no no Yes
Serializable no no no
3.2 Deadlock

Deadlock refers to a phenomenon in which multiple transactions occupy each other on the same resource and request to lock each other's resources, leading to a vicious circle. The database system implements various deadlock detection and deadlock timeout mechanisms. After a deadlock occurs, only a partial or complete rollback of one of the transactions can break the deadlock.

3.3 Transaction log

The transaction log is similar to the file system log. They are both appended. When writing data, the write operation is first performed in a continuous storage area of ​​the disk, instead of frequently and randomly addressing and moving on the disk. Therefore, the transaction log can improve the efficiency of the transaction. In addition, when the transaction log is written back to the disk, it can ensure that the data is correct.

3.4 Transactions in MySQL
Automatic submission

If you do not explicitly specify a transaction, MySQL defaults to a query for a transaction. Use the variable AUTOCOMMIT to control.

Mixed use of storage engines

Transactions are managed by the storage engine, not the MySQL server. Therefore, the same transaction is unreliable on different storage engines. There will be no problems with submitting, and rollbacks will cause trouble. Because non-transactional table data changes cannot be undone. This will lead to uncertain results.

locking

InnoDB uses a two-phase locking protocol. During the execution of the transaction, it can be locked at any time, but it will only be released when COMMIT or ROOLBACK is executed.

Guess you like

Origin blog.csdn.net/niu91/article/details/114555769