首页 > 数据库 >MySQL 锁实践

MySQL 锁实践

时间:2023-05-10 17:07:09浏览次数:37  
标签:name update 实践 t1 session MySQL where id

测试环境为MySQL5.7 

隔离级别:REPEATABLE-READ

自动提交:autocommit OFF

创建测试表:

CREATE TABLE `t1` (

  `id` int(11) DEFAULT NULL,

  `name` varchar(10) DEFAULT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1
insert into t1 (id,name) values (1,"mirror");
insert into t1 (id,name) values (2,"mirror");
insert into t1 (id,name) values (3,"mirror");
insert into t1 (id,name) values (4,"mirror");
insert into t1 (id,name) values (5,"mirror");
insert into t1 (id,name) values (6,"mirror");
insert into t1 (id,name) values (7,"mirror");
insert into t1 (id,name) values (8,"mirror");
insert into t1 (id,name) values (9,"mirror");

一、模拟测试:S锁和X锁

  1. t1表的id和name无索引,session 1 执行select语句,确认session 2是否会行锁

Session 1:

root@localhost : mirro:02: >select * from t1 where id=2;
+------+--------+
| id   | name   |
+------+--------+
|    2 | mirror |
+------+--------+
1 row in set (0.00 sec)

session 2:

root@localhost : mirro:10: >update t1 set name='zhang' where id=2;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

结论:

当直接执行select语句的sql是没有锁的,不影响dml和dql语句的执行


2.t1表的id和name无索引,session 1 执行select语句加share 锁,确认session 2是否会行锁

session 1 :

root@localhost : mirro:22: >select * from t1 where id=2 lock in share mode;
+------+--------+
| id   | name   |
+------+--------+
|    2 | mirror |
+------+--------+
1 row in set (0.00 sec)


Session 2:


root@localhost : mirro:22: >update t1 set name='zhang' where id=2;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

结论:

当执行select语句并增加share锁,会与x锁互斥
  1. session 1 sql增加share锁 ,session 2增加share 锁

Session 1:

root@localhost : mirro:47: >select * from t1 where id=2 lock in share mode;

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

| id   | name   |

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

|    2 | mirror |

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

1 row in set (0.00 sec)

Session 2:

root@localhost : mirro:43: >select * from t1 where id=2 lock in share mode;
+------+--------+
| id   | name   |
+------+--------+
|    2 | mirror |
+------+--------+
1 row in set (0.00 sec)


总结:

1. S锁和S锁是共享的
2. S锁和X锁是互斥的

二、模拟测试:X锁和X锁

  1. t1表的id和name无索引,session 1 执行update语句where id=1确认session 2执行update是否成功

session 1:

root@localhost : mirro:24: >update t1 set name='zhang' where id=1;

Query OK, 1 row affected (0.00 sec)

Rows matched: 1  Changed: 1  Warnings: 0

session 2 :

root@localhost : mirro:24: >update t1 set name='zhang' where id=2;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
root@localhost : mirro:26: >update t1 set name='zhang' where id
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction

结论:

当session 1执行基于where条件id update语句,session 2无法执行执行基于where条件id update语句
说明当执行dml语句的where条件没有索引,则会造成表锁,其它Session都无法执行dml语句,dql不受影响

2.t1表的id和name无索引,session 1 执行update语句where条件为id >3 确认session 2执行update是否成功

session 1:


root@localhost : mirro:25: >update t1 set name='zhang' where id>3;

Query OK, 6 rows affected (0.00 sec)

Rows matched: 6  Changed: 6  Warnings: 0

session 2:

root@localhost : mirro:24: >update t1 set name='zhang' where id=2;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
root@localhost : mirro:26: >update t1 set name='zhang' where id
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
root@localhost : mirro:24: >update t1 set name='zhang' where id=3;
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction
root@localhost : mirro:26: >insert into t1 (id,name) values (15,"www");
ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction


结论:

当session 1执行基于where条件范围更新 update语句,session 2无法执行基于该表的任何dml语句,包含insert语句
说明当执行dml语句的where条件没有索引的范围更新则会造成表锁,其它Session都无法执行dml语句,dql不受影响


总结:

X锁和X锁是互斥的


标签:name,update,实践,t1,session,MySQL,where,id
From: https://blog.51cto.com/u_13963804/6262738

相关文章

  • MySQL的随机排序(random orderby)
    MySQL的随机排序(randomorderby)是指在查询数据库时,将结果集以随机的方式排列。这种排序方式可以用于有趣的应用场景,例如实现随机音乐播放、广告推荐等。要实现MySQL的随机排序,可以使用RAND()函数。RAND()函数可以生成0-1之间的随机数,将它作为排序的依据即可。SELECT*FROM`my......
  • [系统性能优化实践]JVM进阶实战之监控工具(Prometheus)
    1Prometheus监控SpringCloudGateway1.1简述API网关作为应用服务与外部交互的入口,通过对API网关的监控,可以清晰的知道应用整体的请求量,以便根据不同的并发情况进行扩容处理。对API网关的监控也是相当必要的。通过Prometheus监控Gateway与监控普通Springboot项目几乎......
  • 从本地到云端:豆瓣统一的数据存储实践
    豆瓣成立于2005年,是中国最早的社交网站之一。在2009到2019的十年间,豆瓣数据平台经历了几轮变迁,形成了DPark+Mesos+MooseFS的架构。由机房全面上云的过程中,原有这套架构并不能很好的利用云的特性,豆瓣需要做一次全面的重新选型,既要考虑未来十年的发展趋势,也需要找到与......
  • 【完结撒花】MySQL(二十三)主从复制
    MySQL(二十三)主从复制1主从复制概述1.1如何提高数据库并发能力在实际工作中,常将Redis和MySQL配合使用,如果有请求的时候,首先在缓存中查找,如果存在就直接取出,不存在再访问数据库,这样就提升了读取的效率,减少了对后端数据库的访问压力,Redis缓存是高并发架构非常重要的一环......
  • mysql中删除时报错Cannot truncate a table referenced in a foreign key constraint
    在Mysql使用Truncate截断表时,提示Cannottruncateatablereferencedinaforeignkeyconstraint(monitoritem,CONSTRAINTmonitortaskpollutant_monitortask_fk)。这是因为存在外键约束导致的无法删除,我们可以先关闭外键约束,删除后再启动外键约束。1、检查外键约束SELE......
  • 百人研发团队百亿销售规模的技术架构实践分享
    公司背景公司融资10亿,剥离B2B生鲜业务板块为独立公司运营。除部分核心产品经理,运营,采购角色外,诸如研发团队等重新组建,并承接部分历史系统重新打造一整套供应链平台去支撑大规模业务扩张。全国70个左右仓(包含前置仓,中转仓和实体仓),实际达到百亿业务规模(营收),预计达到千亿业务规模......
  • mysql创建systemd管理项
    vim/usr/lib/systemd/system/mysqld.service写入system配置:[Unit]Description=MySQLServerDocumentation=mysqld##mysql参考手册Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.htmlAfter=network.targetAfter=syslog.target[Install]WantedBy=mul......
  • 服务器配置——mysql安装
    1.安装MySQL打开终端获取root权限suroot下载安装mysql官方YumRepository[root@localhost~]#wget-i-chttp://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpmyum安装 [root@localhost~]#yum-yinstallmysql57-community-release-el7-10.noarch.r......
  • mysql 字段逗号分割行转列操作
    一、需求某字段的值为7654,7698,7782,7788期望的效果: 二、实现语句SELECTSUBSTRING_INDEX(SUBSTRING_INDEX('7654,7698,7782,7788',',',help_topic_id+1),',',-1)ASnumFROMmysql.help_topicWHEREhelp_topic_id<LENGTH('7654,7698,7782,7788......
  • MySQL学习
    1、初始MySQLJavaEE:企业级java开发Web前端(页面:展示,数据!)后台(连接点:连接数据库JDBC,连接前端(控制,控制视图跳转,和给前端传递数据)(Spring等框架实现)数据库(存数据,Txt,Excel,word)1.1、为什么学习数据库1.岗位需求2.现在的世界,大数据时代~,得数据库者的天下3.被迫需求:存数据4.数据......