This article brings you an introduction to what is Mysql Innodb transaction isolation level? It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Mysql has four transaction isolation levels, as follows:
1. Read Uncommitted: It allows reading dirty data that has been changed but not committed by other transactions, and will also be read. Leading to non-repeatable read and phantom read problems.
2. Read Committed: It can avoid reading dirty data, but it will still cause non-repeatable read and phantom read problems.
3. REPEATABLE-READ: Mysql default isolation level will cause phantom reads. However, mysql uses MVCC consistency reading at this level and will not produce phantom reads. .
4. Serializable: The highest isolation level, which will avoid the above problems.
You can use the following method to check the isolation level of the current system
mysql> select @@global.tx_isolation,@@tx_isolation; +-----------------------+-----------------+ | @@global.tx_isolation | @@tx_isolation | +-----------------------+-----------------+ | REPEATABLE-READ | REPEATABLE-READ | +-----------------------+-----------------+ 1 row in set (0.00 sec)
Not yet Committed readREAD-UNCOMMITTED Example of dirty read and non-repeatable read:
#session A mysql> set session transaction isolation level read uncommitted; #设置隔离级别为未提交读 Query OK, 0 rows affected (0.00 sec) mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> select * from inno_tbl where id=2; +----+------+ | id | name | +----+------+ | 2 | John | +----+------+ 1 row in set (0.00 sec)
#session B mysql> select @@tx_isolation; +-----------------+ | @@tx_isolation | +-----------------+ | REPEATABLE-READ | +-----------------+ 1 row in set (0.00 sec) mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update inno_tbl set name='Jack Ma' where id=2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0
#session A mysql> select * from inno_tbl where id=2; +----+---------+ | id | name | +----+---------+ | 2 | Jack Ma | +----+---------+ 1 row in set (0.00 sec)
At this time, session A reads the data modified but not submitted by session B. If session B rolls back at this time, Then the data read by A is invalid, which is "dirty data", because the data read by A for the first time is different from the data read for the second time, which is a "non-repeatable read"; In the same way, if new data is inserted into B, new data rows will also be read in this transaction in A. This is a phantom read.
In the same process, if the isolation level of A is changed to read committed, "dirty reading" will not occur, but "non-repeatable reading" and "phantom reading" will also occur
Default Isolation level REPEATABLE-READUnder:
#session A mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> select * from inno_tbl where id=2; +----+--------------+ | id | name | +----+--------------+ | 2 | John | +----+--------------+ 1 row in set (0.00 sec)
#session B mysql> begin; Query OK, 0 rows affected (0.00 sec) mysql> update inno_tbl set name='Lucy' where id=2; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> commit; Query OK, 0 rows affected (0.03 sec)
#session A mysql> select * from inno_tbl where id=2; +----+--------------+ | id | name | +----+--------------+ | 2 | John | +----+--------------+ 1 row in set (0.00 sec) #注意,此时没有产生“不可重复读”问题,但若是为查询加上共享锁: mysql> select * from inno_tbl1 where id=2 lock in share mode; +----+---------+ | id | name | +----+---------+ | 2 | Lucy | +----+---------+ 1 row in set (0.00 sec)
Explanation:
The transaction in session A reads that the name field in the inno_tbl table with id 2 is John, and If the transaction in session B changes the name with id 2 in inno_tbl to Lucy and submits it, if the transaction in A reads this row of data again later, it will be found that if you directly use the select method to query, the read The data is still the old data, and with the shared lock, the real data will be read.
Why? Because in the innodb engine, mysql's add, delete, modify and query statements can be divided into two types: one is snapshot read, and the other is current read. Only ordinary query statements are snapshot reads, while the remaining additions, deletions, modifications and query statements with lock in share mode shared lock or for update exclusive lock are all current reads; what is read at that time is the latest data, while snapshot reading does not necessarily read the latest data.
It can be deduced from this: When updating or deleting with the condition name=John in session A, the update or deletion will definitely not be successful, as shown below:
mysql> update inno_tbl set name='张三' where name='John'; Query OK, 0 rows affected (0.00 sec) Rows matched: 0 Changed: 0 Warnings: 0 mysql> delete from inno_tbl where name='John'; Query OK, 0 rows affected (0.00 sec)
If you isolate If the level is changed to Read Commited, then the query statement in session A can query the latest content that has been changed and submitted in session B without adding lock in share mode or for update. This situation is called impossibility. Read repeatedly. As I write this, I have a little question. Are non-repeatable reading and phantom reading contradicting each other? Answer: No, non-repeatable reading is mainly for modification, and phantom reading is mainly for modification. For insertions and deletions.
The above is the detailed content of What is Mysql Innodb transaction isolation level?. For more information, please follow other related articles on the PHP Chinese website!
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AMACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.
MySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AMMySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.
MySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AMMySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.
MySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AMMySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.
SQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AMThe relationship between SQL and MySQL is the relationship between standard languages and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.
Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AMInnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.
What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AMKey metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.
What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AMUsingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

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






