当前位置:网站首页>【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会对所有记录加锁,就升级为了表锁
间隙锁/临键锁
边栏推荐
- Path analysis model
- 18. Multi level page table and fast table
- 这个高颜值的开源第三方网易云音乐播放器你值得拥有
- 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
- Project GFS data download
- JDBC learning notes
- #systemverilog# 可綜合模型的結構總結
- Leetcode59. spiral matrix II (medium)
- What does UDP attack mean? UDP attack prevention measures
- Embed UE4 program into QT interface display
猜你喜欢
UWA pipeline version 2.2.1 update instructions
开源的网易云音乐API项目都是怎么实现的?
leetcode1020. 飞地的数量(中等)
18. Multi level page table and fast table
杰理之如若需要大包发送,需要手机端修改 MTU【篇】
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
杰理之BLE【篇】
Cif10 actual combat (resnet18)
Prefix and array series
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
随机推荐
RichView TRVStyle 模板样式的设置与使用
NFT on fingertips | evaluate ambire on G2, and have the opportunity to obtain limited edition collections
开源的网易云音乐API项目都是怎么实现的?
Babbitt | metauniverse daily must read: the group image of Chinese Internet enterprises pouring into metauniverse: "there are only various survival desires, and there is no ambition for forward-lookin
LeetCode 78:子集
“无聊猿” BAYC 的内忧与外患
C - Inheritance - polymorphism - virtual function member (lower)
kubernetes集群搭建Zabbix监控平台
leetcode1020. Number of enclaves (medium)
CDN acceleration and cracking anti-theft chain function
What is the biggest problem that fresh e-commerce is difficult to do now
#systemverilog# 可綜合模型的結構總結
Kubernetes cluster builds ZABBIX monitoring platform
[JDBC] quick start tutorial
How to configure GUI guide development environment
hydra常用命令
Chrome view page FPS
ROS learning_ Basics
Cif10 actual combat (resnet18)
OpenGL ES 学习初识(1)