SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
深入解析MySQL之
锁机制应用

  邹德平(半瓶)
  Architect@RT.CN
 www.banping.com
问题
• 只有一件库存商品时,如果两个顾客同时
  购买怎么办?
实现功能的SQL语句
• Select 库存数量 from 商品库存表 where 商
  品ID=1;
• Insert into 订单表;
• Insert into 付款表;
• Update 商品库存表 set 库存数量=库存数量-
  1 where 商品ID=1;
为什么需要锁?
• 对有限资源的保护
• 更新丢失问题
• 隔离和并发的矛盾
事务的隔离级别           脏读    不可重复读   幻读

Read uncommited   Yes   Yes     Yes

Read commited     No    Yes     Yes

Repeatable read   No    No      Yes

Serializable      no    no      no
Repeatable read
MySQL的锁管理机制
MySQL的锁执行流程
元数据锁的参数和监控
• lock_wait_timeout
表级锁的模式
• 读写会互相阻塞

  兼容性          Read lock   Write lock
  Read lock    Yes         No
  Write lock   No          No
表级锁的加锁方式
手动加表级锁
影响表级锁的几个参数
• concurrent_insert
• low-priority-updates
• max_write_lock_count
表级锁的性能监控
Innodb锁的模式
• 通过MVCC提供非锁定读
兼容性   S        IS    X    IX

S     Yes      Yes   No   No

IS    Yes      Yes   No   Yes

X     No       No    No   No

IX    No       Yes   No   Yes
Innodb锁的类型
• Record lock
• Gap lock
• Next-key lock
手动加Innodb锁
• SELECT ... LOCK IN SHARE MODE
• SELECT ... FOR UPDATE
影响Innodb锁的几个参数
•   tx_isolation
•   autocommit
•   innodb_table_locks
•   innodb_lock_wait_timeout
•   innodb_locks_unsafe_for_binlog
Innodb锁的性能监控
About TechClub
• IT技术沙龙
• 兴趣、分享、公益
• http://www.tech-club.org
• https://groups.google.com/group/tech-club-org
• TechClub@weibo
• TechCluber@twitter
• QQ群:79207910
RT寻人,欢迎加入!

Php/Android/IOS 攻城师

Weitere ähnliche Inhalte

Andere mochten auch

MySQL AIO详解
MySQL AIO详解MySQL AIO详解
MySQL AIO详解mysqlops
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLGeorgi Kodinov
 
Mysql features for the enterprise
Mysql features for the enterpriseMysql features for the enterprise
Mysql features for the enterpriseGiuseppe Maxia
 
Understanding MySql locking issues
Understanding MySql locking issuesUnderstanding MySql locking issues
Understanding MySql locking issuesOm Vikram Thapa
 
Metadata locking in MySQL 5.5
Metadata locking in MySQL 5.5Metadata locking in MySQL 5.5
Metadata locking in MySQL 5.5Kostja Osipov
 
Pldc2012 innodb architecture and internals
Pldc2012 innodb architecture and internalsPldc2012 innodb architecture and internals
Pldc2012 innodb architecture and internalsmysqlops
 
Percon XtraDB Cluster in a nutshell
Percon XtraDB Cluster in a nutshellPercon XtraDB Cluster in a nutshell
Percon XtraDB Cluster in a nutshellFrederic Descamps
 
MySQL Scalability Mistakes - OTN
MySQL Scalability Mistakes - OTNMySQL Scalability Mistakes - OTN
MySQL Scalability Mistakes - OTNRonald Bradford
 
Locking and Concurrency Control
Locking and Concurrency ControlLocking and Concurrency Control
Locking and Concurrency ControlMorgan Tocker
 
MySQL Monitoring Mechanisms
MySQL Monitoring MechanismsMySQL Monitoring Mechanisms
MySQL Monitoring MechanismsMark Leith
 
Mysql tech day_paris_ps_and_sys
Mysql tech day_paris_ps_and_sysMysql tech day_paris_ps_and_sys
Mysql tech day_paris_ps_and_sysMark Leith
 
Haute disponibilité my sql avec group réplication
Haute disponibilité my sql avec group réplicationHaute disponibilité my sql avec group réplication
Haute disponibilité my sql avec group réplicationFrederic Descamps
 
MySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationMySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationFrederic Descamps
 
SQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workMarkus Winand
 
My sql performance tuning course
My sql performance tuning courseMy sql performance tuning course
My sql performance tuning courseAlberto Centanni
 
分析mysql acid 设计实现
分析mysql acid 设计实现分析mysql acid 设计实现
分析mysql acid 设计实现rfyiamcool
 
Blazing Performance with Flame Graphs
Blazing Performance with Flame GraphsBlazing Performance with Flame Graphs
Blazing Performance with Flame GraphsBrendan Gregg
 

Andere mochten auch (20)

MySQL AIO详解
MySQL AIO详解MySQL AIO详解
MySQL AIO详解
 
InnoDB Magic
InnoDB MagicInnoDB Magic
InnoDB Magic
 
OUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQLOUGLS 2016: How profiling works in MySQL
OUGLS 2016: How profiling works in MySQL
 
Mysql features for the enterprise
Mysql features for the enterpriseMysql features for the enterprise
Mysql features for the enterprise
 
Understanding MySql locking issues
Understanding MySql locking issuesUnderstanding MySql locking issues
Understanding MySql locking issues
 
Metadata locking in MySQL 5.5
Metadata locking in MySQL 5.5Metadata locking in MySQL 5.5
Metadata locking in MySQL 5.5
 
Pldc2012 innodb architecture and internals
Pldc2012 innodb architecture and internalsPldc2012 innodb architecture and internals
Pldc2012 innodb architecture and internals
 
Percon XtraDB Cluster in a nutshell
Percon XtraDB Cluster in a nutshellPercon XtraDB Cluster in a nutshell
Percon XtraDB Cluster in a nutshell
 
MySQL Scalability Mistakes - OTN
MySQL Scalability Mistakes - OTNMySQL Scalability Mistakes - OTN
MySQL Scalability Mistakes - OTN
 
Perf Tuning Short
Perf Tuning ShortPerf Tuning Short
Perf Tuning Short
 
Locking and Concurrency Control
Locking and Concurrency ControlLocking and Concurrency Control
Locking and Concurrency Control
 
MySQL Monitoring Mechanisms
MySQL Monitoring MechanismsMySQL Monitoring Mechanisms
MySQL Monitoring Mechanisms
 
Mysql tech day_paris_ps_and_sys
Mysql tech day_paris_ps_and_sysMysql tech day_paris_ps_and_sys
Mysql tech day_paris_ps_and_sys
 
Haute disponibilité my sql avec group réplication
Haute disponibilité my sql avec group réplicationHaute disponibilité my sql avec group réplication
Haute disponibilité my sql avec group réplication
 
MySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group ReplicationMySQL InnoDB Cluster - Group Replication
MySQL InnoDB Cluster - Group Replication
 
InnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick FiguresInnoDB Locking Explained with Stick Figures
InnoDB Locking Explained with Stick Figures
 
SQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they workSQL Transactions - What they are good for and how they work
SQL Transactions - What they are good for and how they work
 
My sql performance tuning course
My sql performance tuning courseMy sql performance tuning course
My sql performance tuning course
 
分析mysql acid 设计实现
分析mysql acid 设计实现分析mysql acid 设计实现
分析mysql acid 设计实现
 
Blazing Performance with Flame Graphs
Blazing Performance with Flame GraphsBlazing Performance with Flame Graphs
Blazing Performance with Flame Graphs
 

Ähnlich wie 深入解析MySQL之锁机制应用

Mysql 培训-优化篇
Mysql 培训-优化篇Mysql 培训-优化篇
Mysql 培训-优化篇sunmonth
 
数据架构方面的一些探讨
数据架构方面的一些探讨数据架构方面的一些探讨
数据架构方面的一些探讨Chao Zhu
 
InnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCCInnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCCfrogd
 
Mysql调优
Mysql调优Mysql调优
Mysql调优ken shin
 
Taobao casestudy-yufeng-qcon
Taobao casestudy-yufeng-qconTaobao casestudy-yufeng-qcon
Taobao casestudy-yufeng-qconYiwei Ma
 
Notes of jcip
Notes of jcipNotes of jcip
Notes of jcipDai Jun
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysqlZoom Quiet
 
基于My sql的分布式数据库实践
基于My sql的分布式数据库实践基于My sql的分布式数据库实践
基于My sql的分布式数据库实践锐 张
 
基于MySQL的分布式数据库实践
基于MySQL的分布式数据库实践基于MySQL的分布式数据库实践
基于MySQL的分布式数据库实践jackbillow
 
构建可维护的Javascript 小米网
构建可维护的Javascript 小米网构建可维护的Javascript 小米网
构建可维护的Javascript 小米网yang alex
 
新浪微博Feed服务架构
新浪微博Feed服务架构新浪微博Feed服务架构
新浪微博Feed服务架构XiaoJun Hong
 
N-layer design & development
N-layer design & developmentN-layer design & development
N-layer design & developmentXuefeng Zhang
 
Comment System of 56.com
Comment System of 56.comComment System of 56.com
Comment System of 56.comHo Kim
 
美团前端架构简介
美团前端架构简介美团前端架构简介
美团前端架构简介pan weizeng
 
有道云笔记架构简介
有道云笔记架构简介有道云笔记架构简介
有道云笔记架构简介drewz lin
 
一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统
一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统
一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统yiditushe
 
4 葉金榮-my sql優化 - 20151219
4 葉金榮-my sql優化 - 201512194 葉金榮-my sql優化 - 20151219
4 葉金榮-my sql優化 - 20151219Ivan Tu
 
Google LevelDB Study Discuss
Google LevelDB Study DiscussGoogle LevelDB Study Discuss
Google LevelDB Study Discusseverestsun
 
基于My sql的分布式数据库实践 公开
基于My sql的分布式数据库实践 公开基于My sql的分布式数据库实践 公开
基于My sql的分布式数据库实践 公开YANGL *
 
MySQL技术分享:一步到位实现mysql优化
MySQL技术分享:一步到位实现mysql优化MySQL技术分享:一步到位实现mysql优化
MySQL技术分享:一步到位实现mysql优化Jinrong Ye
 

Ähnlich wie 深入解析MySQL之锁机制应用 (20)

Mysql 培训-优化篇
Mysql 培训-优化篇Mysql 培训-优化篇
Mysql 培训-优化篇
 
数据架构方面的一些探讨
数据架构方面的一些探讨数据架构方面的一些探讨
数据架构方面的一些探讨
 
InnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCCInnoDB Transaction Lock and MVCC
InnoDB Transaction Lock and MVCC
 
Mysql调优
Mysql调优Mysql调优
Mysql调优
 
Taobao casestudy-yufeng-qcon
Taobao casestudy-yufeng-qconTaobao casestudy-yufeng-qcon
Taobao casestudy-yufeng-qcon
 
Notes of jcip
Notes of jcipNotes of jcip
Notes of jcip
 
111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql111030 gztechparty-小路-云时代的mysql
111030 gztechparty-小路-云时代的mysql
 
基于My sql的分布式数据库实践
基于My sql的分布式数据库实践基于My sql的分布式数据库实践
基于My sql的分布式数据库实践
 
基于MySQL的分布式数据库实践
基于MySQL的分布式数据库实践基于MySQL的分布式数据库实践
基于MySQL的分布式数据库实践
 
构建可维护的Javascript 小米网
构建可维护的Javascript 小米网构建可维护的Javascript 小米网
构建可维护的Javascript 小米网
 
新浪微博Feed服务架构
新浪微博Feed服务架构新浪微博Feed服务架构
新浪微博Feed服务架构
 
N-layer design & development
N-layer design & developmentN-layer design & development
N-layer design & development
 
Comment System of 56.com
Comment System of 56.comComment System of 56.com
Comment System of 56.com
 
美团前端架构简介
美团前端架构简介美团前端架构简介
美团前端架构简介
 
有道云笔记架构简介
有道云笔记架构简介有道云笔记架构简介
有道云笔记架构简介
 
一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统
一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统
一天交易达十亿次: 由核心J2Ee(Tm) 模式架构成的J2Ee(Tm)系统
 
4 葉金榮-my sql優化 - 20151219
4 葉金榮-my sql優化 - 201512194 葉金榮-my sql優化 - 20151219
4 葉金榮-my sql優化 - 20151219
 
Google LevelDB Study Discuss
Google LevelDB Study DiscussGoogle LevelDB Study Discuss
Google LevelDB Study Discuss
 
基于My sql的分布式数据库实践 公开
基于My sql的分布式数据库实践 公开基于My sql的分布式数据库实践 公开
基于My sql的分布式数据库实践 公开
 
MySQL技术分享:一步到位实现mysql优化
MySQL技术分享:一步到位实现mysql优化MySQL技术分享:一步到位实现mysql优化
MySQL技术分享:一步到位实现mysql优化
 

深入解析MySQL之锁机制应用