当前位置:网站首页>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 ”
参考
边栏推荐
- SQL statement
- [C语言]给账号密码进行MD5加密
- L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
- 用docker 连接mysql的过程
- Parameter index out of range (1 > number of parameters, which is 0)
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
- Opengauss database development and debugging tool guide
- The core idea of performance optimization, dry goods sharing
- tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
- Two dimensional format array format index subscript continuity problem leads to return JSON format problem
猜你喜欢

力扣------网格中的最小路径代价

Check log4j problems using stain analysis

Add MDF database file to SQL Server database, and the error is reported

Linear rectification function relu and its variants in deep learning activation function

I2C subsystem (IV): I2C debug

Sous - système I2C (IV): débogage I2C

HTB-Devel

I2C 子系統(四):I2C debug

左连接,内连接

Force deduction ----- the minimum path cost in the grid
随机推荐
函数栈帧的创建与销毁
I2C 子系统(二):I3C spec
Deep Reinforcement Learning for Intelligent Transportation Systems: A Survey 论文阅读笔记
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
Do you really understand relays?
HTB-Devel
I2C subsystem (III): I2C driver
Process the dataset and use labelencoder to convert all IDs to start from 0
Pytest (6) -fixture (Firmware)
What is the way out for children from poor families?
内存泄漏工具VLD安装及使用
I2C 子系统(一):I2C spec
[shutter] banner carousel component (shutter_wiper plug-in | swiper component)
Informatics Olympiad one general question bank 1006 a+b questions
Chart. JS multitooltip tag - chart js multiTooltip labels
Opengauss database development and debugging tool guide
2022-2028 global splicing display industry research and trend analysis report
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
open file in 'w' mode: IOError: [Errno 2] No such file or directory
I2C 子系统(四):I2C debug