There are four transaction isolation levels: 1. Read Uncommitted (read uncommitted), which allows reading of uncommitted data changes, which may cause dirty reads, non-repeatable reads, and phantom reads. 2. Read Committed (Read Committed) allows reading data that has been submitted by concurrent transactions, which can avoid dirty reads, but may cause non-repeatable and phantom reads. 3. Repeatable Read (repeatable read), the results of multiple reads of the same field are consistent. 4. Serializable (serializable).
The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.
1. What is a transaction?
A transaction is a logical set of operations, either all of them are executed or none of them are executed.
The most classic and often mentioned chestnut in affairs is bank transfer. For example, if Xiao Ming wants to transfer 1,000 yuan to Xiao Hong, this transfer will involve two key operations: reducing Xiao Ming's balance by 1,000 yuan and reducing Xiao Hong's balance by 1,000 yuan. If an error suddenly occurs between these two operations, causing Xiao Ming's balance to decrease but Xiao Hong's balance not to increase, this situation is definitely not allowed. The transaction is to ensure that these two key operations either succeed or neither succeeds.
2. Transaction characteristics (ACID)
- **Atomicity:* *The smallest execution unit of a transaction, no division is allowed. The atomicity of transactions ensures that actions are either all executed or none at all.
- **Consistency:**The data remains consistent before and after executing the transaction. For example, in a money transfer business, the total amount of the transferor and the payee should remain unchanged regardless of whether the transaction is successful or not.
- **Isolation:**When accessing the database concurrently, a user's transaction should not be affected by other transactions, and the database is independent between concurrent transactions.
- **Persistence:**After a transaction is committed, its changes to the data in the database are durable and should not be affected even if the database fails.
3. Problems caused by concurrent transactions
In a typical application, multiple transactions run concurrently, often Manipulate the same data to complete their respective tasks (multiple users operating on the same data). Although concurrency is necessary, it may cause the following problems:
- **Dirty read: **When a transaction is accessing data and has modified it, but it is still The transaction was not submitted. At this time, another transaction also accessed the data and then used the data. Because the modification of the data has not been submitted to the database, the data read by the other transaction is "Dirty Data ", this behavior is "Dirty Read", and operations based on "Dirty Data" may cause problems.
-
Lost of modify: means that when a transaction reads a piece of data, another data also accesses the data, then after the first transaction modifies the data , the second transaction also modified this data. In this way, the modification results in the first transaction are lost. This situation is called Modification loss. For example: transaction 1 reads the data
A=20
in the table, transaction 2 also readsA=20
, transaction 1 modifiesA=A-1
, transaction 2 also modifiesA=A-1
, and the final result is19
, but the modification record of transaction 1 is lost. - Unrepeatable read: refers to reading the same data multiple times within a transaction. Before the transaction ends, another transaction also accesses the data and evaluates the data. If the data is modified, the data read twice by the first transaction may be inconsistent. This situation is called non-repeatable read.
- Phantom read (Phantom read): Phantom read is similar to non-repeatable read. Phantom read refers to a transaction that reads several rows of data. The transaction has not ended yet, and then Another transaction inserts some data. In subsequent queries, the first transaction reads more data than originally read, as if an hallucination has occurred, so it is called phantom reading.
The difference between non-repeatable reading and phantom reading:
The focus of non-repeatable reading is modification, while the focus of phantom reading is addition or deletion.
Chestnut 1 (Same conditions, the data you have read will be different when you read it again): Mr. A in transaction 1 has read that his salary is 1,000. The operation has not ended yet, and the transaction Mr. B in 2 modified Mr. A's salary to 2000. When Mr. A read his salary again, it became 2000. This is a non-repeatable read.
Chestnut 2 (same conditions, the number of records read out for the first and second times is different): If there are 4 people in a salary table with a salary greater than 3,000, transaction 1 reads all For people with a salary greater than 3,000, a total of 4 records were queried. This is because transaction 2 queried another record with a salary greater than 3,000. Transaction 1 read again and found 5 records. This is a phantom read.
4. Transaction isolation levels
The SQL standard defines four isolation levels:
- **READ-UNCOMMITTED: **The lowest isolation level, allowing reading of uncommitted data changes, may cause dirty reads, non-repeatable reads, and phantom reads.
- **READ-COMMITTED: **Allows reading of data that has been submitted by concurrent transactions, can avoid dirty reads, but may cause non-repeatable and phantom reads.
- **Repeatable read (REPEATABLE-READ): **The results of multiple reads of the same field are consistent unless the transaction itself is modified, can avoid dirty reads and unresolved reads Repeated reading may cause phantom reading.
- **Serializable (SERIALIZABLE): **The highest isolation level, fully compliant with the ACID isolation level, all transactions are executed in sequence, can avoid dirty reads and non-repeatable reads , phantom reading.
Isolation level | Dirty read | Non-repeatable read | Phantom read |
---|---|---|---|
Read Uncommitted | √ | √ | √ |
× | √ | √ | |
× | × | √ | |
× | × | × |
The above is the detailed content of What are the mysql transaction isolation levels?. For more information, please follow other related articles on the PHP Chinese website!

Stored procedures are precompiled SQL statements in MySQL for improving performance and simplifying complex operations. 1. Improve performance: After the first compilation, subsequent calls do not need to be recompiled. 2. Improve security: Restrict data table access through permission control. 3. Simplify complex operations: combine multiple SQL statements to simplify application layer logic.

The working principle of MySQL query cache is to store the results of SELECT query, and when the same query is executed again, the cached results are directly returned. 1) Query cache improves database reading performance and finds cached results through hash values. 2) Simple configuration, set query_cache_type and query_cache_size in MySQL configuration file. 3) Use the SQL_NO_CACHE keyword to disable the cache of specific queries. 4) In high-frequency update environments, query cache may cause performance bottlenecks and needs to be optimized for use through monitoring and adjustment of parameters.

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

The steps for upgrading MySQL database include: 1. Backup the database, 2. Stop the current MySQL service, 3. Install the new version of MySQL, 4. Start the new version of MySQL service, 5. Recover the database. Compatibility issues are required during the upgrade process, and advanced tools such as PerconaToolkit can be used for testing and optimization.

MySQL backup policies include logical backup, physical backup, incremental backup, replication-based backup, and cloud backup. 1. Logical backup uses mysqldump to export database structure and data, which is suitable for small databases and version migrations. 2. Physical backups are fast and comprehensive by copying data files, but require database consistency. 3. Incremental backup uses binary logging to record changes, which is suitable for large databases. 4. Replication-based backup reduces the impact on the production system by backing up from the server. 5. Cloud backups such as AmazonRDS provide automation solutions, but costs and control need to be considered. When selecting a policy, database size, downtime tolerance, recovery time, and recovery point goals should be considered.

MySQLclusteringenhancesdatabaserobustnessandscalabilitybydistributingdataacrossmultiplenodes.ItusestheNDBenginefordatareplicationandfaulttolerance,ensuringhighavailability.Setupinvolvesconfiguringmanagement,data,andSQLnodes,withcarefulmonitoringandpe

Optimizing database schema design in MySQL can improve performance through the following steps: 1. Index optimization: Create indexes on common query columns, balancing the overhead of query and inserting updates. 2. Table structure optimization: Reduce data redundancy through normalization or anti-normalization and improve access efficiency. 3. Data type selection: Use appropriate data types, such as INT instead of VARCHAR, to reduce storage space. 4. Partitioning and sub-table: For large data volumes, use partitioning and sub-table to disperse data to improve query and maintenance efficiency.

TooptimizeMySQLperformance,followthesesteps:1)Implementproperindexingtospeedupqueries,2)UseEXPLAINtoanalyzeandoptimizequeryperformance,3)Adjustserverconfigurationsettingslikeinnodb_buffer_pool_sizeandmax_connections,4)Usepartitioningforlargetablestoi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

WebStorm Mac version
Useful JavaScript development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1
Powerful PHP integrated development environment
