当前位置:Gxlcms > 数据库问题 > SQL锁机制 - 基础概念+示例分析

SQL锁机制 - 基础概念+示例分析

时间:2021-07-01 10:21:17 帮助过:28人阅读

锁机制: 解决因资源共享而出现的并发控制问题
??锁机制主要包含两个部分【锁类型 - 对数据集合操作的权力】和【锁粒度 - 操作数据集合的大小】

锁机制的基本概念

  1. 示例:买最后一件衣服X
  2. A:
  3. X
  4. 买 : X加锁 ->试衣服...下单..付款..打包 ->X解锁
  5. B:
  6. X 买:发现X已被加锁,等待X解锁, X已售空
  7. 分类:
  8. ??操作类型(锁类型):
  9. a.读锁(共享锁): 对同一个数据(衣服),多个读操作可以同时进行,互不干扰。
  10. b.写锁(排他锁): 如果当前写操作没有完毕(买衣服的一系列操作),则无法进行其他的读操作、写操作
  11. ??操作范围(锁粒度\范围):
  12. a.表锁 :一次性对一张表整体加锁。如MyISAM存储引擎使用表锁,开销小、加锁快、并发能力小;无死锁;但锁的范围大,容易发生锁冲突、并发度低。
  13. b.行锁 :一次性对一条数据加锁。如InnoDB存储引擎使用行锁,开销大,加锁慢、并发能力大;容易出现死锁;锁的范围较小,不易发生锁冲突,并发度高(很小概率 发生高并发问题:脏读、幻读、不可重复度、丢失更新等问题)。
  14. c.页锁

表锁: -- 自增操作 MySQL/SqlServer支持; oracle需要借助于序列来实现自增

【数据准备】

  1. <code>create table tablelock
  2. (
  3. id int primary key auto_increment ,
  4. name varchar(20)
  5. )engine myisam;
  6. insert into tablelock(name) values(‘a1‘);
  7. insert into tablelock(name) values(‘a2‘);
  8. insert into tablelock(name) values(‘a3‘);
  9. insert into tablelock(name) values(‘a4‘);
  10. insert into tablelock(name) values(‘a5‘);
  11. commit;
  12. -- ??增加锁
  13. lock table 表1 read/write ,表2 read/write ,...
  14. mysql> lock table tablelock read;
  15. Query OK, 0 rows affected (0.00 sec)
  16. -- ??查看加锁表
  17. mysql> SHOW OPEN TABLES;
  18. +--------------------+----------------------------------------------+--------+-------------+
  19. | Database | Table | In_use | Name_locked |
  20. +--------------------+----------------------------------------------+--------+-------------+
  21. | mysql | time_zone_transition_type | 0 | 0 |
  22. | performance_schema | events_waits_summary_global_by_event_name | 0 | 0 |
  23. | performance_schema | setup_timers | 0 | 0 |
  24. | performance_schema | events_waits_history_long | 0 | 0 |
  25. | mysql | time_zone_transition | 0 | 0 |
  26. | performance_schema | mutex_instances | 0 | 0 |
  27. | performance_schema | events_waits_summary_by_instance | 0 | 0 |
  28. | mysql | tables_priv | 0 | 0 |
  29. | mysql | procs_priv | 0 | 0 |
  30. | mysql | func | 0 | 0 |
  31. | performance_schema | events_waits_history | 0 | 0 |
  32. | mysql | time_zone_name | 0 | 0 |
  33. | mysql | user | 0 | 0 |
  34. | performance_schema | setup_consumers | 0 | 0 |
  35. | performance_schema | file_instances | 0 | 0 |
  36. | performance_schema | cond_instances | 0 | 0 |
  37. | mysql | plugin | 0 | 0 |
  38. | mysql | db | 0 | 0 |
  39. | mysql | proxies_priv | 0 | 0 |
  40. | mysql | time_zone | 0 | 0 |
  41. | performance_schema | rwlock_instances | 0 | 0 |
  42. | performance_schema | events_waits_current | 0 | 0 |
  43. | mysql | event | 0 | 0 |
  44. | mysql | columns_priv | 0 | 0 |
  45. | performance_schema | performance_timers | 0 | 0 |
  46. | performance_schema | threads | 0 | 0 |
  47. | mysql | host | 0 | 0 |
  48. | performance_schema | events_waits_summary_by_thread_by_event_name | 0 | 0 |
  49. | performance_schema | file_summary_by_event_name | 0 | 0 |
  50. | mysql | time_zone_leap_second | 0 | 0 |
  51. | performance_schema | file_summary_by_instance | 0 | 0 |
  52. | learn_demo | tablelock | 1 | 0 | -- ?
  53. | mysql | servers | 0 | 0 |
  54. | performance_schema | setup_instruments | 0 | 0 |
  55. +--------------------+----------------------------------------------+--------+-------------+
  56. 34 rows in set (0.00 sec)
  57. </code>

会话:session :每一个访问数据的dos命令行、数据库客户端工具 都是一个会话

?? 加读锁??

  1. <code>-- 会话1:
  2. lock table tablelock read;
  3. select * from tablelock; -- 读(查) 可以
  4. delete from tablelock where id = 1; -- 写(增删改) 不可以
  5. select * from emp; -- 读 不可以
  6. delete from emp where id = 1; --- 写(增删改) 不可以
  7. -- ??如果一个会话对一个表施加了读锁,则该会话只能对该表进行读的操作不能进行写操作而且也不能对其他的表进行读或写。
  8. -- 会话2:
  9. select * from tablelock; -- 读(查) 可以
  10. delete from tablelock where id = 1; -- 写,??会等待会话1将锁释放
  11. select * from emp; -- 读(查) 可以
  12. delete from emp where id = 1; -- 写 可以
  13. -- ??如果某一个会话对A表加了读锁则其他的会话可以对A表进行读的操作但写需要等待A表上的锁释放,其他会话对其他表的读写没有影响。
  14. </code>

技术图片

释放锁: UNLOCK TABLES

?? 加写锁??

【数据准备】

  1. <code>mysql> LOCK TABLE tablelock WRITE; --
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql> SHOW OPEN TABLES;
  4. +--------------------+----------------------------------------------+--------+-------------+
  5. | Database | Table | In_use | Name_locked |
  6. +--------------------+----------------------------------------------+--------+-------------+
  7. | mysql | time_zone_transition_type | 0 | 0 |
  8. | performance_schema | events_waits_summary_global_by_event_name | 0 | 0 |
  9. | performance_schema | setup_timers | 0 | 0 |
  10. | performance_schema | events_waits_history_long | 0 | 0 |
  11. | mysql | time_zone_transition | 0 | 0 |
  12. | performance_schema | mutex_instances | 0 | 0 |
  13. | performance_schema | events_waits_summary_by_instance | 0 | 0 |
  14. | mysql | tables_priv | 0 | 0 |
  15. | mysql | procs_priv | 0 | 0 |
  16. | mysql | func | 0 | 0 |
  17. | performance_schema | events_waits_history | 0 | 0 |
  18. | mysql | time_zone_name | 0 | 0 |
  19. | mysql | user | 0 | 0 |
  20. | performance_schema | setup_consumers | 0 | 0 |
  21. | performance_schema | file_instances | 0 | 0 |
  22. | performance_schema | cond_instances | 0 | 0 |
  23. | mysql | plugin | 0 | 0 |
  24. | mysql | db | 0 | 0 |
  25. | mysql | proxies_priv | 0 | 0 |
  26. | mysql | time_zone | 0 | 0 |
  27. | performance_schema | rwlock_instances | 0 | 0 |
  28. | performance_schema | events_waits_current | 0 | 0 |
  29. | mysql | event | 0 | 0 |
  30. | mysql | columns_priv | 0 | 0 |
  31. | performance_schema | performance_timers | 0 | 0 |
  32. | performance_schema | threads | 0 | 0 |
  33. | mysql | host | 0 | 0 |
  34. | performance_schema | events_waits_summary_by_thread_by_event_name | 0 | 0 |
  35. | performance_schema | file_summary_by_event_name | 0 | 0 |
  36. | mysql | time_zone_leap_second | 0 | 0 |
  37. | performance_schema | file_summary_by_instance | 0 | 0 |
  38. | learn_demo | tablelock | 1 | 0 |
  39. | mysql | servers | 0 | 0 |
  40. | performance_schema | setup_instruments | 0 | 0 |
  41. +--------------------+----------------------------------------------+--------+-------------+
  42. 34 rows in set (0.00 sec)
  43. mysql> SELECT *
  44. -> FROM tablelock;
  45. +----+------+
  46. | id | name |
  47. +----+------+
  48. | 1 | a1 |
  49. | 2 | a2 |
  50. | 3 | a3 |
  51. | 4 | a4 |
  52. | 5 | a5 |
  53. | 6 | wyt |
  54. +----+------+
  55. 6 rows in set (0.00 sec)
  56. mysql> UPDATE tablelock
  57. -> SET name = "wzz"
  58. -> WHERE id = 5;
  59. Query OK, 1 row affected (0.03 sec)
  60. Rows matched: 1 Changed: 1 Warnings: 0
  61. mysql> SELECT *
  62. -> FROM tablelock;
  63. +----+------+
  64. | id | name |
  65. +----+------+
  66. | 1 | a1 |
  67. | 2 | a2 |
  68. | 3 | a3 |
  69. | 4 | a4 |
  70. | 5 | wzz |
  71. | 6 | wyt |
  72. +----+------+
  73. 6 rows in set (0.00 sec)
  74. mysql> unlock tables;
  75. Query OK, 0 rows affected (0.00 sec)
  76. -- 如果一个会话对A表加了写锁,则该会话可以对A表进行读写(增删改查), 其他会话对A表进行读写的前提是A表上的锁释放
  77. </code>

技术图片

??MySQL表级锁的锁模式
MyISAM在执行查询语句(SELECT)前,会自动给涉及的所有表加读锁,
在执行更新操作(DML)前,会自动给涉及的表加写锁。
所以对MyISAM表进行操作,会有以下情况:
a、对MyISAM表的读操作(加读锁),不会阻塞其他进程(会话)对同一表的读请求,
但会阻塞对同一表的写请求。只有当读锁释放后,才会执行其它进程的写操作。
b、对MyISAM表的写操作(加写锁),会阻塞其他进程(会话)对同一表的读和写操作,
只有当写锁释放后,才会执行其它进程的读写操作。
对表级锁的分析
  1. 分析表锁定:
  2. 查看哪些表加了锁: show open tables ; 1代表被加了锁
  3. 分析表锁定的严重程度: show status like ‘table%‘ ;
  4. mysql> SHOW STATUS LIKE "table%";
  5. +-----------------------+-------+
  6. | Variable_name | Value |
  7. +-----------------------+-------+
  8. | Table_locks_immediate | 49 |
  9. | Table_locks_waited | 0 |
  10. +-----------------------+-------+
  11. 2 rows in set (0.00 sec)
  12. Table_locks_immediate :即可能获取到的锁数
  13. Table_locks_waited:需要等待的表锁数(如果该值越大,说明存在越大的锁竞争)
  14. 一般建议:
  15. Table_locks_immediate/Table_locks_waited > 5000, 建议采用InnoDB引擎,否则MyISAM引擎

行锁 - InnoDB的锁粒度

【数据准备】

  1. <code>mysql> create table linelock(
  2. -> id int(5) primary key auto_increment,
  3. -> name varchar(20)
  4. -> )engine=innodb ;
  5. Query OK, 0 rows affected (0.04 sec)
  6. mysql> insert into linelock(name) values(‘1‘) ;
  7. Query OK, 1 row affected (0.02 sec)
  8. mysql> insert into linelock(name) values(‘2‘) ;
  9. Query OK, 1 row affected (0.00 sec)
  10. mysql> insert into linelock(name) values(‘3‘) ;
  11. Query OK, 1 row affected (0.00 sec)
  12. mysql> insert into linelock(name) values(‘4‘) ;
  13. Query OK, 1 row affected (0.00 sec)
  14. mysql> insert into linelock(name) values(‘5‘) ;
  15. Query OK, 1 row affected (0.00 sec)
  16. mysql> SELECT *
  17. -> FROM linelock;
  18. +----+------+
  19. | id | name |
  20. +----+------+
  21. | 1 | 1 |
  22. | 2 | 2 |
  23. | 3 | 3 |
  24. | 4 | 4 |
  25. | 5 | 5 |
  26. +----+------+
  27. 5 rows in set (0.00 sec)
  28. </code>

为了研究行级锁,暂时将自动的commit关闭;SET AUTOCOMMIT = 0; 研究之后需要commit

  1. <code>-- 【行锁 - 操作同一条记录 eg: id = 6】
  2. -- 会话1: 写操作
  3. mysql> INSERT INTO linelock VALUES(6, ‘a6‘);;
  4. Query OK, 1 row affected (0.00 sec)
  5. mysql> commit; -- 如果一直不commit即结束此事务则造成其他会话无法对该行数据进行读写
  6. -- ★ ??表锁 是通过unlock tables,也可以通过事务解锁 ; 行锁 是通过事务解锁??
  7. -- 会话2: 写操作同样的数据
  8. mysql> update linelock set name=‘ax‘ where id = 6;
  9. -- 总结: 如果会话x对某条数据a进行 DML操作(研究时:关闭了自动commit的情况下),则其他会话必须等待会话x结束事务(commit/rollback)后 才能对数据a进行操作。
  10. -- 【行锁 - 操作不同记录 eg: name = ax 】
  11. 会话1: 写操作
  12. insert into linelock values(8,‘a8‘) ;
  13. 会话2: 写操作, 不同的数据
  14. update linelock set name=‘ax‘ where id = 5;
  15. 行锁,一次锁一行数据;因此 如果操作的是不同数据,则不干扰。
  16. </code>

?? 如果没有索引,则行锁会转为表锁

  1. <code> 【按理来说,两个会话操作不同的记录相互之间不会发送阻塞的现象但下面的情况发生了阻塞】
  2. 会话0: 写操作
  3. update linelock set name = ‘ai‘ where name = ‘3‘ ;
  4. 会话1: 写操作, 不同的数据
  5. update linelock set name = ‘aiX‘ where name = ‘4‘ ;
  6. commit
  7. -- ?????? 原因:如果索引类 发生了类型转换,则索引失效。 因此 此次操作,会从行锁转为表锁。
  8. </code>

行锁的一种特殊情况: 间隙锁

  1. <code>
  2. b.行锁的一种特殊情况:间隙锁:值在范围内,但却不存在
  3. --此时linelock表中 没有id=7的数据
  4. update linelock set name =‘x‘ where id >1 and id<9 ; --即在此where范围中,没有id=7的数据,则id=7的数据成为间隙。
  5. 间隙:Mysql会自动给 间隙 加索 ->间隙锁。即 本题 会自动给id=7的数据加 间隙锁(行锁)。
  6. 行锁:如果有where,则实际加索的范围 就是where后面的范围(不是实际的值)
  7. 如何仅仅是查询数据,能否加锁? 可以 for update
  8. 研究学习时,将自动提交关闭:
  9. set autocommit =0 ;
  10. start transaction ;
  11. begin ;
  12. select * from linelock where id =2 for update ;
  13. 通过for update对query语句进行加锁。
  14. </code>
??MySQL行级锁的锁模式
InnoDB默认采用行锁;
缺点: 比表锁性能损耗大。
优点:并发能力强,效率高。
因此建议,高并发用InnoDB,否则用MyISAM。

行锁分析:

  1. <code>show status like ‘%innodb_row_lock%‘ ;
  2. Innodb_row_lock_current_waits :当前正在等待锁的数量
  3. Innodb_row_lock_time:等待总时长。从系统启到现在 一共等待的时间
  4. Innodb_row_lock_time_avg :平均等待时长。从系统启到现在平均等待的时间
  5. Innodb_row_lock_time_max :最大等待时长。从系统启到现在最大一次等待的时间
  6. Innodb_row_lock_waits :等待次数。从系统启到现在一共等待的次数
  7. </code>

SQL锁机制 - 基础概念+示例分析

标签:src   进程   次数   ons   关闭   数据集   集合   相互   其它   

人气教程排行