Mysql数据库管理--数据库行锁分析与测试

1 表无索引 update任何行,不提交简直是灾难

1 在不通过索引条件查询时,Innodb会锁定表中所有记录。

session 1

set autocommit=0;

update ta set user='chenliguo' where user='rp1';

session 2

update ta set user='wangbaosong' where user='root';

对于索引的表,session2 会被session 1 锁住。

mysql> select * from data_lock_waits\G

*************************** 1. row ***************************

ENGINE: INNODB

REQUESTING_ENGINE_LOCK_ID: 282791232:0:43:2:242839520

REQUESTING_ENGINE_TRANSACTION_ID: 434473

REQUESTING_THREAD_ID: 48

REQUESTING_EVENT_ID: 31

REQUESTING_OBJECT_INSTANCE_BEGIN: 242839520

BLOCKING_ENGINE_LOCK_ID: 282792056:0:43:2:242842776

BLOCKING_ENGINE_TRANSACTION_ID: 434472

BLOCKING_THREAD_ID: 49

BLOCKING_EVENT_ID: 14

BLOCKING_OBJECT_INSTANCE_BEGIN: 242842776s

mysql> desc performance_schema.threads;

+---------------------+------------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------------------+------------------+------+-----+---------+-------+

| THREAD_ID | bigint unsigned | NO | PRI | NULL | |

| NAME | varchar(128) | NO | MUL | NULL | |

| TYPE | varchar(10) | NO | | NULL | |

| PROCESSLIST_ID | bigint unsigned | YES | MUL | NULL | |

| PROCESSLIST_USER | varchar(32) | YES | MUL | NULL | |

| PROCESSLIST_HOST | varchar(255) | YES | MUL | NULL | |

| PROCESSLIST_DB | varchar(64) | YES | | NULL | |

| PROCESSLIST_COMMAND | varchar(16) | YES | | NULL | |

| PROCESSLIST_TIME | bigint | YES | | NULL | |

| PROCESSLIST_STATE | varchar(64) | YES | | NULL | |

| PROCESSLIST_INFO | longtext | YES | | NULL | |

| PARENT_THREAD_ID | bigint unsigned | YES | | NULL | |

| ROLE | varchar(64) | YES | | NULL | |

| INSTRUMENTED | enum('YES','NO') | NO | | NULL | |

| HISTORY | enum('YES','NO') | NO | | NULL | |

| CONNECTION_TYPE | varchar(16) | YES | | NULL | |

| THREAD_OS_ID | bigint unsigned | YES | MUL | NULL | |

| RESOURCE_GROUP | varchar(64) | YES | MUL | NULL | |

+---------------------+------------------+------+-----+---------+-------+

18 rows in set (0.00 sec)

mysql> desc performance_schema.processlist;

+---------+-----------------+------+-----+---------+-------+

| Field | Type | Null | Key | Default | Extra |

+---------+-----------------+------+-----+---------+-------+

| ID | bigint unsigned | NO | PRI | NULL | |

| USER | varchar(32) | YES | | NULL | |

| HOST | varchar(255) | YES | | NULL | |

| DB | varchar(64) | YES | | NULL | |

| COMMAND | varchar(16) | YES | | NULL | |

| TIME | bigint | YES | | NULL | |

| STATE | varchar(64) | YES | | NULL | |

| INFO | longtext | YES | | NULL | |

+---------+-----------------+------+-----+---------+-------+

8 rows in set (0.01 sec)

select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from performance_schema.data_lock_waits a,performance_schema.threads c

where a.REQUESTING_THREAD_ID=c.thread_id

union all

select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from performance_schema.data_lock_waits a,performance_schema.threads c

where a.BLOCKING_THREAD_ID=c.thread_id;

mysql> select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from per

formance_schema.data_lock_waits a,performance_schema.threads c

-> where a.REQUESTING_THREAD_ID=c.thread_id

-> union all

-> select c.processlist_user,c.processlist_host,c.processlist_command,c.processlist_state from per

formance_schema.data_lock_waits a,performance_schema.threads c

-> where a.BLOCKING_THREAD_ID=c.thread_id;

+------------------+------------------+---------------------+-------------------+

| processlist_user | processlist_host | processlist_command | processlist_state |

+------------------+------------------+---------------------+-------------------+

| root | localhost | Query | updating |

| root | localhost | Sleep | NULL |

+------------------+------------------+---------------------+-------------------+

2 rows in set (0.00 sec)

mysql>

2 mysql行锁是针对索引加的锁,不是针对所有记录加的锁,索引虽然访问的是不同行的记录,但是如果使用相同的索引键,会出现锁冲突的。

表tab1(id int,name varchar(100)) id列有索引,name列无索引。

则update1

update tab1 set name='1' where id=1 and name='CHENLIGUO';

update tab1 set name='2' where id=1 and name='LIZONGYUAN'

会发生阻塞。

相关推荐
数据猎手小k1 小时前
AndroidLab:一个系统化的Android代理框架,包含操作环境和可复现的基准测试,支持大型语言模型和多模态模型。
android·人工智能·机器学习·语言模型
Ai 编码助手1 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
陈燚_重生之又为程序员2 小时前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle2 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
白云如幻2 小时前
MySQL排序查询
数据库·mysql
萧鼎2 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
^velpro^2 小时前
数据库连接池的创建
java·开发语言·数据库
你的小102 小时前
JavaWeb项目-----博客系统
android
苹果醋32 小时前
Java8->Java19的初步探索
java·运维·spring boot·mysql·nginx
荒川之神2 小时前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle