当前位置:网站首页>【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会对所有记录加锁,就升级为了表锁
间隙锁/临键锁

边栏推荐
- OpenJudge NOI 2.1 1661:Bomb Game
- 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)
- 中青看点阅读新闻
- Seriously recommend several machine learning official account
- You deserve this high-value open-source third-party Netease cloud music player
- On the world of NDK (2)
- Uni app third party package configuration network request
- How to find a medical software testing institution? First flight software evaluation is an expert
- Project GFS data download
- 开源的网易云音乐API项目都是怎么实现的?
猜你喜欢

树莓派3B更新vim

SSM learning

JDBC学习笔记

leetcode704. Binary search (find an element, simple, different writing)

JDBC learning notes

Kubernetes cluster builds ZABBIX monitoring platform

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

Leetcode59. spiral matrix II (medium)
![[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5](/img/c3/7a147151b7338cf38ffbea24e8bafd.jpg)
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5

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
随机推荐
The difference between get and post request types
Uncaught TypeError: Cannot red propertites of undefined(reading ‘beforeEach‘)解决方案
Zhongqing reading news
Markdown 中设置图片图注
[server data recovery] case of offline data recovery of two hard disks of IBM server RAID5
Project GFS data download
Detailed explanation | detailed explanation of internal mechanism of industrial robot
Twelve rules for naming variables
Misc of BUU (update from time to time)
Cookie Technology & session Technology & ServletContext object
Raspberry pie serial port login and SSH login methods
Missing monitoring: ZABBIX monitors the status of Eureka instance
hydra常用命令
leetcode1020. 飞地的数量(中等)
MVVM of WPF
Arduino tutorial - Simon games
kubernetes集群搭建Zabbix监控平台
Win10 64 bit Mitsubishi PLC software appears oleaut32 DLL access denied
#systemverilog# 可綜合模型的結構總結
leetcode841. Keys and rooms (medium)