Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

深入解析MySQL之锁机制应用

4,193 views

Published on

MySQL Lock

Published in: Technology, Business
  • Be the first to comment

深入解析MySQL之锁机制应用

  1. 1. 深入解析MySQL之锁机制应用 邹德平(半瓶) Architect@RT.CN www.banping.com
  2. 2. 问题• 只有一件库存商品时,如果两个顾客同时 购买怎么办?
  3. 3. 实现功能的SQL语句• Select 库存数量 from 商品库存表 where 商 品ID=1;• Insert into 订单表;• Insert into 付款表;• Update 商品库存表 set 库存数量=库存数量- 1 where 商品ID=1;
  4. 4. 为什么需要锁?• 对有限资源的保护• 更新丢失问题• 隔离和并发的矛盾事务的隔离级别 脏读 不可重复读 幻读Read uncommited Yes Yes YesRead commited No Yes YesRepeatable read No No YesSerializable no no no
  5. 5. Repeatable read
  6. 6. MySQL的锁管理机制
  7. 7. MySQL的锁执行流程
  8. 8. 元数据锁的参数和监控• lock_wait_timeout
  9. 9. 表级锁的模式• 读写会互相阻塞 兼容性 Read lock Write lock Read lock Yes No Write lock No No
  10. 10. 表级锁的加锁方式
  11. 11. 手动加表级锁
  12. 12. 影响表级锁的几个参数• concurrent_insert• low-priority-updates• max_write_lock_count
  13. 13. 表级锁的性能监控
  14. 14. Innodb锁的模式• 通过MVCC提供非锁定读兼容性 S IS X IXS Yes Yes No NoIS Yes Yes No YesX No No No NoIX No Yes No Yes
  15. 15. Innodb锁的类型• Record lock• Gap lock• Next-key lock
  16. 16. 手动加Innodb锁• SELECT ... LOCK IN SHARE MODE• SELECT ... FOR UPDATE
  17. 17. 影响Innodb锁的几个参数• tx_isolation• autocommit• innodb_table_locks• innodb_lock_wait_timeout• innodb_locks_unsafe_for_binlog
  18. 18. Innodb锁的性能监控
  19. 19. About TechClub• IT技术沙龙• 兴趣、分享、公益• http://www.tech-club.org• https://groups.google.com/group/tech-club-org• TechClub@weibo• TechCluber@twitter• QQ群:79207910
  20. 20. RT寻人,欢迎加入!Php/Android/IOS 攻城师

×