当前位置:网站首页>MySql实战45讲【全局锁和表锁】
MySql实战45讲【全局锁和表锁】
2022-07-03 02:59:00 【小鱼儿2020】
06 | 全局锁和表锁 :给表加个字段怎么有这么多阻碍?
数据库锁设计的初衷是处理并发问题。作为多用户共享的资源,当出现并发访问的时候,数据库需要合理地控制资源的访问规则。而锁就是用来实现这些访问规则的重要数据结构.
根据加锁的范围,MySQL 里面的锁大致可以分成全局锁、表级锁和行锁三类
全局锁
全局锁就是对整个数据库实例加锁
FTWRL
Flush tables with read lock
当你需要让整个库处于只读状态的时候,可以使用这个命令,之后其他线程的以下语句会被阻塞:数据更新语句(数据的增删改)、数据定义语句(包括建表、修改表结构等)和更新类事务的提交语句.
表级锁
MySQL 里面表级别的锁有两种:一种是表锁,一种是元数据锁(meta data lock,MDL)
表锁的语法是
lock tables 表名 read/write
可以用 unlock tables 主动释放锁,也可以在客户端断开的时候自动释放。
需要注意,lock tables 语法除了会限制别的线程的读写外,
也限定了本线程接下来的操作对象
举个例子, 如果在某个线程 A 中执行 lock tables t1 read, t2 write; 这个语句,则其他线程
写 t1、读写 t2 的语句都会被阻塞。同时,线程 A 在执行 unlock tables 之前,也只能执
行读 t1、读写 t2 的操作。连写 t1 都不允许,自然也不能访问其他表
表级的锁是 MDL(metadata lock)
MDL 不需要显式使用,在访问一个表的时候会被自动加上。MDL 的作用是,保证读写的正确性。
在 MySQL 5.5 版本中引入了 MDL,
当对一个表做增删改查操作的时候,加 MDL读锁;
当要对表做结构变更操作的时候,加 MDL 写锁
读锁之间不互斥,因此你可以有多个线程同时对一张表增删改查,读写锁之间、写锁之间是互斥的,用来保证变更表结构操作的安全性。因此,如果有两个线程要同时给一个表加字段,其中一个要等另一个执行完才能开始执行
给一个表加字段,或者修改字段,或者加索引,需要扫描全表的数据

事务中的 MDL 锁,在语句执行开始时申请,但是语句结束后并不会马上释放,而会等到整个事务提交后再释放
在 MySQL 的information_schema 库的 innodb_trx 表中,你可以查到当前执行中的事务。如果你要
做 DDL 变更的表刚好有长事务在执行,要考虑先暂停 DDL,或者 kill 掉这个长事务。
在 alter table语句里面设定等待时间,如果在这个指定的等待时间里面能够拿到 MDL 写锁最好,拿不
到也不要阻塞后面的业务语句,先放弃。之后开发人员或者 DBA 再通过重试命令重复这
个过程。
ALTER TABLE tbl_name NOWAIT add column ...
ALTER TABLE tbl_name WAIT N add column ...
Online DDL的过程是这样的:
- 拿MDL写锁
- 降级成MDL读锁
- 真正做DDL
- 升级成MDL写锁
- 释放MDL锁
1、2、4、5如果没有锁冲突,执行时间非常短。第3步占用了DDL绝大部分时间,这期间这个表
可以正常读写数据,是因此称为“online ”
参考
边栏推荐
- Cancer biopsy instruments and kits - market status and future development trends
- Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers
- Counter统计数量后,如何返回有序的key
- ComponentScan和ComponentScans的区别
- Change cell color in Excel using C - cell color changing in Excel using C
- [principles of multithreading and high concurrency: 1_cpu multi-level cache model]
- Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
- 模糊查詢時報錯Parameter index out of range (1 > number of parameters, which is 0)
- I2C 子系统(四):I2C debug
- [C language] MD5 encryption for account password
猜你喜欢

From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character

Creation and destruction of function stack frame

SqlServer行转列PIVOT

Segmentation fault occurs during VFORK execution

Check log4j problems using stain analysis

The Linux server needs to install the agent software EPS (agent) database

Yiwen takes you to know ZigBee

Random shuffle note

Super easy to use logzero

Three.js本地环境搭建
随机推荐
二维格式数组格式索引下标连续问题导致 返回json 格式问题
Add automatic model generation function to hade
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
Deep reinforcement learning for intelligent transportation systems: a survey paper reading notes
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
Update and return document in mongodb - update and return document in mongodb
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
Check log4j problems using stain analysis
C language beginner level - pointer explanation - paoding jieniu chapter
[C语言]给账号密码进行MD5加密
Kubernetes cluster log and efk architecture log scheme
Linear rectification function relu and its variants in deep learning activation function
迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
How to return ordered keys after counter counts the quantity
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)
【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
SQL statement
左值右指解释的比较好的
tensor中的append应该如何实现