当前位置:网站首页>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 ”
参考
边栏推荐
- [C语言]给账号密码进行MD5加密
- vfork执行时出现Segmentation fault
- [fluent] JSON model conversion (JSON serialization tool | JSON manual serialization | writing dart model classes according to JSON | online automatic conversion of dart classes according to JSON)
- Add automatic model generation function to hade
- 【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
- Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
- 2022-2028 global splicing display industry research and trend analysis report
- Interview stereotyped version
- [error record] the parameter 'can't have a value of' null 'because of its type, but the im
- JMeter performance test JDBC request (query database to obtain database data) use "suggestions collection"
猜你喜欢

Kubernetes family container housekeeper pod online Q & A?

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

迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常

HTB-Devel

Force deduction ----- the minimum path cost in the grid
![[principles of multithreading and high concurrency: 1_cpu multi-level cache model]](/img/c7/6b5ab4ff7379bfccff7cdbb358ff8f.jpg)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]

Joking about Domain Driven Design (III) -- Dilemma

Segmentation fault occurs during VFORK execution

I2C 子系统(三):I2C Driver

Xiaodi notes
随机推荐
HW initial preparation
Linear rectification function relu and its variants in deep learning activation function
Practice of traffic recording and playback in vivo
当lambda没有输入时,是何含义?
Build a private cloud disk cloudrev
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
I2C 子系统(一):I2C spec
用docker 连接mysql的过程
The solution of "the required function is not supported" in win10 remote desktop connection is to modify the Registry [easy to understand]
Kubernetes cluster log and efk architecture log scheme
HW-初始准备
Getting started | jetpack hilt dependency injection framework
docker安装redis
The core idea of performance optimization, dry goods sharing
Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
Three. JS local environment setup
Kubernetes family container housekeeper pod online Q & A?
Why choose a frame? What frame to choose
用docker 連接mysql的過程
I2C subsystem (I): I2C spec