当前位置:网站首页>MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
2022-06-30 07:54:00 【飘然渡沧海】
线上无锁添加索引:加索引的语句不加锁
ALTER TABLE tbl_name ADD PRIMARY KEY (column), ALGORITHM=INPLACE, LOCK=NONE;
ALGORITHM=INPLACE
更优秀的解决方案,在当前表加索引,步骤:
1.创建索引(二级索引)数据字典
2.加共享表锁,禁止DML,允许查询
3.读取聚簇索引,构造新的索引项,排序并插
入新索引
4.等待打开当前表的所有只读事务提交
5.创建索引结束
ALGORITHM=COPY
通过临时表创建索引,需要多一倍存储,还有更多的IO,步骤:
1.新建带索引(主键索引)的临时表
2.锁原表,禁止DML,允许查询
3.将原表数据拷贝到临时表
4.禁止读写,进行rename,升级字典锁
5.完成创建索引操作
LOCK=DEFAULT:默认方式,MySQL自行判断使用哪种LOCK模式,尽量不锁表
LOCK=NONE:无锁:允许Online DDL期间进行并发读写操作。如果Online DDL操
作不支持对表的继续写入,则DDL操作失败,对表修改无效
LOCK=SHARED:共享锁:Online DDL操作期间堵塞写入,不影响读取
LOCK=EXCLUSIVE:排它锁:Online DDL操作期间不允许对锁表进行任何操作
然后在阿里云的帮助上找到了一个解决方法
- In-Place?:对应 DDL语句的 Algorithm 选项,通过 inplace 方式执行 DDL。相比表拷贝方式,可以减少空间和 I/O 消耗。
- Copies Table?:对应 DDL语句的 Algorithm 选项,通过 表拷贝 方式执行 DDL。DDL 执行期间会占用更大的磁盘空间和消耗更多的 I/O。
- 允许并发 DML?:对应 DDL语句的 Lock 选项,DDL 执行期间是否支持并发 DML 操作。
- 允许并发查询?:DDL 语句执行期间是否支持并发查询操作(通常都是支持的)。
- MySQL官方文档请参考:Online DDL 概览
- DDL 操作执行时需要修改表的元数据(metadata),有可能会遇到等待表元数据锁的情况(waiting for table metadata lock),该情况的处理方式请参考:RDS MySQL 表上 Metadata lock 的产生和处理
- Inplace 和 Copy Table 是相反的 2 种处理方式;但即使 DDL 支持 Inplace 选项,某些操作在整个执行过程中也会部分涉及到表拷贝,比如上表中的添加列操作。
边栏推荐
- Construction of energy conservation supervision system for campus buildings of ankery University
- String and underlying character types of go data type
- 安科瑞高等学校校园建筑节能监管系统建设
- At the age of 25, I started to work in the Tiankeng industry with buckets. After going through a lot of hardships to become a programmer, my spring finally came
- Inversion Lemma
- December 19, 2021 [reading notes] - bioinformatics and functional genomics (Chapter 5 advanced database search)
- Halcon12+vs2013 C # configuration
- Summary and common applications of direction and angle operators in Halcon
- Deep learning -- using word embedding and word embedding features
- CRM&PM如何帮助企业创造最优销售绩效
猜你喜欢
Deep learning - embedding matrix and learning word embedding andword2vec
342 maps covering exquisite knowledge, one of which is classic and pasted on the wall
冰冰学习笔记:快速排序
深度学习——目标定位
【花雕体验】12 搭建ESP32C3之Arduino开发环境
Digital white paper on total cost management in chain operation industry
深度学习——LSTM
Deep learning -- using word embedding and word embedding features
National technology n32g45x series about timer timing cycle calculation
跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过
随机推荐
JS代码案例
December 13, 2021 [reading notes] | understanding of chain specific database building
Go 数据类型篇之基本数据类型之间的转化
Cadence innovus physical implementation series (I) Lab 1 preliminary innovus
JS code case
2021.11.20 [reading notes] | differential variable splicing events and DTU analysis
Final review -php learning notes 1
F12抓包用于做postman接口测试的全过程解析
2021-10-29 [microbiology] a complete set of 16s/its analysis process based on qiime2 tool (Part I)
right four steps of SEIF SLAM
CRM能为企业带来哪些管理提升
Lodash filter collection using array of values
February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)
Deep learning -- using word embedding and word embedding features
Network, network card and IP configuration
想问问,炒股怎么选择证券公司?网上开户安全么?
Inversion Lemma
Calculate Euler angle according to rotation matrix R yaw, pitch, roll source code
【花雕体验】14 行空板pinpong库测试外接传感器模块(之一)
Multi whale capital: report on China's education intelligent hardware industry in 2022