当前位置:网站首页>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 ”
参考
边栏推荐
- 分布式事务
- Andwhere multiple or query ORM conditions in yii2
- Force freeing memory in PHP
- 内存泄漏工具VLD安装及使用
- sql server数据库添加 mdf数据库文件,遇到的报错
- Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
- SQL Server Query spécifie la structure de la table
- Pytest (6) -fixture (Firmware)
- Do you really understand relays?
- TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
猜你喜欢

【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)

Today, it's time to copy the bottom!

I2C subsystem (III): I2C driver

SqlServer行转列PIVOT

Random shuffle note

你真的懂继电器吗?
![ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc](/img/cb/145937a27ef08050a370d5a255215a.jpg)
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc

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

"Analysis of 43 cases of MATLAB neural network": Chapter 43 efficient programming skills of neural network -- Discussion Based on the characteristics of the new version of MATLAB r2012b

Can netstat still play like this?
随机推荐
复选框的使用:全选,全不选,选一部分
Error when installing MySQL in Linux: starting mysql The server quit without updating PID file ([FAILED]al/mysql/data/l.pid
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
二维格式数组格式索引下标连续问题导致 返回json 格式问题
C语言中左值和右值的区别
内存泄漏工具VLD安装及使用
Random Shuffle attention
sql server数据库添加 mdf数据库文件,遇到的报错
Add automatic model generation function to hade
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
SqlServer行转列PIVOT
Interview stereotyped version
Introduction to cron expression
[C语言]给账号密码进行MD5加密
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
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)
Unity3d human skin real time rendering real simulated human skin real time rendering "suggestions collection"
From C to capable -- use the pointer as a function parameter to find out whether the string is a palindrome character
Variable declarations following if statements
Your family must be very poor if you fight like this!