当前位置:网站首页>【mysql学习笔记30】锁(非教程)
【mysql学习笔记30】锁(非教程)
2022-07-06 07:13:00 【yqs_281876918】
分类
- 全局锁:锁住一个库
- 表级锁:锁住一张表
- 行级锁:锁住一行记录
全局锁
全局锁是对整个数据库进行加锁,锁住以后整个数据库将处于只读状态,其余操作均会阻塞。
应用场景就是一致性数据库备份时需要加全局锁,防止备份过程中数据变动。
使用全局锁
flush tables with read lock;
mysql备份命令
mysqldump -u root -p 1234 db_name>backup.sql
表级锁
分类
- 表锁(又分为read lock和write lock)
- 元数据锁(meta data lock,MDL)
- 意向锁
元数据锁
简介
元数据锁是有mysql系统自行控制的,我们无需显示使用。元数据锁主要是用来维护元数据的数据一致性,在表上有活动事务的时候,不可以对元数据进行写入操作。
理解
元数据可以理解为一张表的结构信息,当一张表上有活动事务的时候,元数据锁会被自动加上,防止操作数据途中表结构被修改导致各种意外情况。
加锁时机
当对一张表进行增删改查的时候,加MDL读锁。
当对一张表结构进行变更操作的时候,加MDL写锁。
加表锁
lock tables 表名 read;
一旦加了读锁,所有连接包括自己都不能写,只能读
lock tables 表名 write;
一旦加了写锁,加锁的连接可以读和写,其它连接既不能读也不能写
解锁
unlock tables;
意向锁
介绍
通过加意向锁,另一个线程在加表锁的时候就不需要遍历所有行来判断是否能加锁,提高了效率。
分类
意向共享锁:由语句select … lock in share mode添加,与表锁共享锁兼容,与排他锁互斥
意向排他锁:由insert、update、delete、select … for update添加,与表锁共享锁以及排他锁都互斥
行级锁
介绍
行级锁,每次操作锁住对应行数据。锁定力度最小,发成锁冲突的概率最低,并发度最高。应用在InnoDB存储引擎中。
InnoDB的数据是基于索引组织的,行锁通过对索引上的索引项加锁来实现,而不是对记录加锁
分类
- 行锁:锁定单个行记录,防止其它事务对此进行update和delete。
- 间隙锁:锁定索引记录间隙(不包含该记录),确保索引记录间隙不变,防止其他事务在这个间隙进行insert,产生欢度。
- 临键锁:是行锁和间隙锁的组合,相当于闭区间的间隙锁
行锁
- 共享锁:允许其他事务读
- 排他锁:禁止其他事务读写

ps:如果增删改的条件没有覆盖索引,那么Innodb会对所有记录加锁,就升级为了表锁
间隙锁/临键锁

边栏推荐
- Yield method of tread
- supervisor 使用文档
- C - Inheritance - hidden method
- What is the biggest problem that fresh e-commerce is difficult to do now
- 【线上问题处理】因代码造成mysql表死锁的问题,如何杀掉对应的进程
- How to find a medical software testing institution? First flight software evaluation is an expert
- 中青看点阅读新闻
- leetcode841. Keys and rooms (medium)
- Refer to how customer push e-commerce does content operation
- L'auteur est mort? Ai utilise l'art pour conquérir l'humanité
猜你喜欢

The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower

Cookie Technology & session Technology & ServletContext object

呆错图床系统源码图片CDN加速与破解防盗链功能

配置树莓派接入网络

【服务器数据恢复】IBM服务器raid5两块硬盘离线数据恢复案例

How to find a medical software testing institution? First flight software evaluation is an expert

mysql如何合并数据

NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections

变量的命名规则十二条

C - Inheritance - polymorphism - virtual function member (lower)
随机推荐
The psychological process from autojs to ice fox intelligent assistance
C language_ Double create, pre insert, post insert, traverse, delete
Raspberry pie serial port login and SSH login methods
微信公众号无限回调授权系统源码 全网首发
Setting and using richview trvstyle template style
Briefly describe the differences between indexes, primary keys, unique indexes, and joint indexes in mysql, and how they affect the performance of the database (in terms of reading and writing)
Uni app practical project
微信脑力比拼答题小程序_支持流量主带最新题库文件
Zhongqing reading news
从autojs到冰狐智能辅助的心里历程
A brief introduction of reverseme in misc in the world of attack and defense
[JDBC] quick start tutorial
C - Inheritance - polymorphism - virtual function member (lower)
可变参数重载时的内存错误
Crawling exercise: Notice of crawling Henan Agricultural University
这个高颜值的开源第三方网易云音乐播放器你值得拥有
chrome查看页面fps
Yield method of tread
Supervisor usage document
Is software testing outsourcing going or not? Three years' real outsourcing experience tells you