https://zhuanlan.zhihu.com/p/75595737
1 MySQL 的三种锁
1.1 表锁
开销小,加锁快
不会出现死锁
锁定粒度大,发生锁冲突的概率最高,并发度最低
1.2 行锁
开销大,加锁慢
会出现死锁
锁定粒度小,发生锁冲突的概率最低,并发度最高
1.3 页锁
开销和加锁时间介于表锁和行锁之间
会出现死锁
锁定粒度介于表锁和行锁之间,并发度一般
1.4 引擎与锁
MyISAM 和 MEMORY 支持表锁
BDB 支持页锁,也支持表锁
Innodb 既支持行锁,也支持表锁,默认行锁
1.5 查询表锁争用情况
检查 table_locks_waited 和 table_locks_immediate 状态变量分析
table_locks_immediate : 可以立即获取锁的次数
table_locks_waited : 不能立即获取锁,需要等待锁的次数
mysql> show status like '%table%';
+-----------------------------------------+---------+
| Variable_name | Value |
+-----------------------------------------+---------+
| Com_alter_table | 0 |
| Com_alter_tablespace | 0 |
| Com_create_table | 0 |
| Com_drop_table | 0 |
| Com_lock_tables | 0 |
| Com_rename_table | 0 |
| Com_show_create_table | 0 |
| Com_show_open_tables | 0 |
| Com_show_table_status | 0 |
| Com_show_tables | 0 |
| Com_unlock_tables | 0 |
| Created_tmp_disk_tables | 0 |
| Created_tmp_tables | 0 |
| Innodb_undo_tablespaces_total | 2 |
| Innodb_undo_tablespaces_implicit | 2 |
| Innodb_undo_tablespaces_explicit | 0 |
| Innodb_undo_tablespaces_active | 2 |
| Open_table_definitions | 617 |
| Open_tables | 5860 |
| Opened_table_definitions | 0 |
| Opened_tables | 0 |
| Performance_schema_table_handles_lost | 0 |
| Performance_schema_table_instances_lost | 0 |
| Performance_schema_table_lock_stat_lost | 0 |
| Replica_open_temp_tables | 0 |
| Slave_open_temp_tables | 0 |
| Table_locks_immediate | 4099671 |
| Table_locks_waited | 0 |
| Table_open_cache_hits | 0 |
| Table_open_cache_misses | 0 |
| Table_open_cache_overflows | 0 |
+-----------------------------------------+---------+
31 rows in set (3.13 sec)
相关文档
评论