当前位置:网站首页>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 ”
参考
边栏推荐
- 处理数据集,使用LabelEncoder将所有id转换为从0开始
- Apple releases MacOS 11.6.4 update: mainly security fixes
- 复选框的使用:全选,全不选,选一部分
- Matlab tips (24) RBF, GRNN, PNN neural network
- Kubernetes cluster log and efk architecture log scheme
- [Fuhan 6630 encodes and stores videos, and uses RTSP server and timestamp synchronization to realize VLC viewing videos]
- SQL statement
- I2C 子系统(三):I2C Driver
- [shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling
- Strategy application of Dameng database
猜你喜欢

用docker 连接mysql的过程

Summary of interview project technology stack

What is the way out for children from poor families?

Kubernetes cluster log and efk architecture log scheme

Pytest (6) -fixture (Firmware)

Today, it's time to copy the bottom!
![[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)](/img/e5/c01f760b07b495f5b048ea367e0c21.gif)
[fluent] listview list (map method description of list set | vertical list | horizontal list | code example)

SQL statement

Creation and destruction of function stack frame

Le processus de connexion mysql avec docker
随机推荐
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
How to implement append in tensor
[Fuhan 6630 encodes and stores videos, and uses RTSP server and timestamp synchronization to realize VLC viewing videos]
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
Serious security vulnerabilities reported by moxa mxview network management software
Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
Baidu map - surrounding search
Cancer biopsy instruments and kits - market status and future development trends
Segmentation fault occurs during VFORK execution
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
random shuffle注意
Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
ComponentScan和ComponentScans的区别
JS finds all the parent nodes or child nodes under a node according to the tree structure
Update and return document in mongodb - update and return document in mongodb
Pytest (6) -fixture (Firmware)
Source code analysis | layout file loading process
Kubernetes cluster log and efk architecture log scheme
HW-初始准备