SlideShare a Scribd company logo
浅析My sql事务隔离级别与锁 seanlook
浅析My sql事务隔离级别与锁 seanlook
会锁定多少行记录?
CREATE TABLE `t` (
`f_id` int(11) AUTO_INCREMENT NOT NULL,
`f_name` varchar(30) NOT NULL DEFAULT ‘’,
`f_group` tinyint(3) unsigned,
PRIMARY KEY (`f_id`),
UNIQUE KEY `idx_name` (`f_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
浅析My sql事务隔离级别与锁 seanlook
检测表锁和行锁之间的冲突
为的是准备上更细粒度的锁
session A session B
BEGIN WORK BEGIN WORK
select count(*)
update a record
InnoDB Lock
Compatity
S X IS IX
S 兼容 不兼容 兼容 不兼容
X 不兼容 不兼容 不兼容 不兼容
IS 兼容 不兼容 兼容 兼容
IX 不兼容 不兼容 兼容 兼容
两段封锁法
SELECT * FROM t where f_id=1
SELECT * FROM t where f_id=1 FOR update
SELECT * FROM t where f_id>10 LOCK IN share mode
insert into table values (…)
update table set ? where ?
delete from table where ?
浅析My sql事务隔离级别与锁 seanlook
(buffer)
(LSN)
(顺序IO)
• 2 Phase Commit
• innodb_flush_log_at_trx_commit
• sync_binlog
UNDO -> 原子性
REDO -> 永久性
Lock+Undo -> 隔离性
Constraint -> 一致性
浅析My sql事务隔离级别与锁 seanlook
(MVCC)
1 a 0001 0x123456 018
f_id f_name f_age DATA_TRX_ID DATA_ROLL_PTR DELETE_BIT
undo log
100
trx_1
1 aa 0002 0x234567 018
undo log
101
undo log
100
trx_2
读未提交
(Read Uncommitted)
YES YES YES
已提交读
(Read Committed)
NO YES YES
可重复读
(Repeatable Read)
NO NO YES ?
可串行化
(Serializable)
NO NO NO
(default for RDS)
(default for Official)
隔离级别 脏读
(Dirty Read)
不可重复读
(Non-Repeatable Read)
幻读
(Phantom Read)
session A session B
1 START TRANSACTION START TRANSACTION
2 select * from t where f_id>0 and f_id<5;
3 update t set f_name='aa'
where f_id=1;
4 insert into t values(3,'c');
5 select * from t where f_id>0 and f_id<5;
6 COMMIT
7 select * from t where f_id>0 and f_id<5;
8 COMMIT
9 select * from t where f_id>0 and f_id<5;
分别在RC和RR级别下执行
session A session B
1 START TRANSACTION START TRANSACTION
2 select * from t where f_group>10 and
f_gender=1 for update;
3 update t set f_gender=0
where f_id=99;
分别在RC和RR级别下执行
RC: 0 rows affected
RR: blocked
delete from t1 where id = 10;
RR session A session B
1 START TRANSACTION START TRANSACTION
2 update t set f_group=0 where
f_group < 20
3 insert into t values(111,'10K',18);
4 commit
浅析My sql事务隔离级别与锁 seanlook
session A session B session C
1 START TRANSACTION START TRANSACTION START TRANSACTION
2 INSERT INTO t (f_name)
VALUES (‘cc');
3 INSERT INTO t (f_name)
VALUES (‘cc');
4 INSERT INTO t (f_name)
VALUES (‘cc');
5 rollback
ERROR 1213 (40001): Deadlock found when trying
to get lock; try restarting transaction
http://dev.mysql.com/doc/refman/5.6/en/innodb-locking.html
http://tech.meituan.com/innodb-lock.html
http://mysql.taobao.org/monthly/2015/12/01/?spm=5176.100239.blogc
ont4270.9.cwMW19
http://hedengcheng.com/?p=771
浅析My sql事务隔离级别与锁 seanlook

More Related Content

PDF
ชนิดข้อมูลและตัวแปร
PAGES
Unix open
PDF
Burrowing through go! the book
DOCX
7segment scetch
PDF
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
PDF
Webinar 2013 advanced_query_tuning
PDF
MySQL Performance Tuning: Top 10 Tips
PDF
MySQL Server Settings Tuning
ชนิดข้อมูลและตัวแปร
Unix open
Burrowing through go! the book
7segment scetch
MySQL Webinar 2/4 Performance tuning, hardware, optimisation
Webinar 2013 advanced_query_tuning
MySQL Performance Tuning: Top 10 Tips
MySQL Server Settings Tuning

Viewers also liked (15)

ODP
Performance Tuning
PDF
MySQL Performance Tuning für Entwickler
KEY
Perf Tuning Short
PDF
MySQL Performance Tuning Variables
PDF
MySQL Performance Tuning
PDF
Performance Tuning Best Practices
PDF
Join-fu: The Art of SQL Tuning for MySQL
PPTX
MySQL Indexing - Best practices for MySQL 5.6
PDF
MySQL查询优化浅析
PDF
MySQL Manchester TT - Performance Tuning
PPTX
MySQL Performance Tips & Best Practices
PDF
Mysql Explain Explained
PDF
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
PDF
How to Design Indexes, Really
PPTX
Mysql performance tuning
Performance Tuning
MySQL Performance Tuning für Entwickler
Perf Tuning Short
MySQL Performance Tuning Variables
MySQL Performance Tuning
Performance Tuning Best Practices
Join-fu: The Art of SQL Tuning for MySQL
MySQL Indexing - Best practices for MySQL 5.6
MySQL查询优化浅析
MySQL Manchester TT - Performance Tuning
MySQL Performance Tips & Best Practices
Mysql Explain Explained
MySQL Performance Tuning. Part 1: MySQL Configuration (includes MySQL 5.7)
How to Design Indexes, Really
Mysql performance tuning
Ad

Recently uploaded (20)

PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
Introduction to Artificial Intelligence
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
System and Network Administraation Chapter 3
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
top salesforce developer skills in 2025.pdf
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
L1 - Introduction to python Backend.pptx
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
System and Network Administration Chapter 2
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
Odoo POS Development Services by CandidRoot Solutions
Introduction to Artificial Intelligence
Navsoft: AI-Powered Business Solutions & Custom Software Development
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
System and Network Administraation Chapter 3
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
top salesforce developer skills in 2025.pdf
How to Migrate SBCGlobal Email to Yahoo Easily
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
How to Choose the Right IT Partner for Your Business in Malaysia
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
L1 - Introduction to python Backend.pptx
2025 Textile ERP Trends: SAP, Odoo & Oracle
System and Network Administration Chapter 2
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Upgrade and Innovation Strategies for SAP ERP Customers
Ad

浅析My sql事务隔离级别与锁 seanlook