2020-01-20: in mysql, there are 300 million data in a table, which is not divided into tables. The requirement is to add a column of data to this large table. The database can't be stopped, and there are addition, deletion and modification operations. How

2020-01-20 mysql million data table


2020-01-20:mysql in , One watch has 3 100 million data , Unclassified table , The requirement is to add a column of data to this large table . The database can't stop , And there are addition, deletion and modification operations . How to operate ?
Fogo's answer 2020-01-20:

Momo answers :
use pt_online_schema_change This tool can operate , The principle is to copy the table structure first and synchronize the data .

CSDN answer :
The old version before MySQL How to add a column :

ALTER TABLE Your watch ADD COLUMN New column char(128);

It will cause a lock on the table , The simple process is as follows :

1. Build a new one and Table1 Completely isomorphic Table2.
2. Counter table Table1 Add write lock .
3. In the table Table2 On the implementation ALTER TABLE Your watch ADD COLUMN New column char(128).
4. take Table1 Copy to Table2.
5. take Table2 Rename it to Table1 And remove Table1, Release all related locks .

If the amount of data is particularly large , It takes a long time to lock the watch , During this period, all table updates block , Online business can't work properly .

in the light of MySQL 5.6( It doesn't contain ) Previous version , The update of one table is repeated on another table through trigger , And data synchronization , When data synchronization is complete , In business, change the table name to a new table and publish . Business will not be suspended . Trigger settings are similar to :

create trigger person_trigger_update AFTER UPDATE on Original table for each row
begin set @x = “trigger UPDATE”;
Replace into New table SELECT * from Original table where New table .id = Original table .id;
END IF;
end;

MySQL 5.6( contain ) Later versions introduced online DDL The function of :

Alter table Your watch , ALGORITHM [=] {DEFAULT|INSTANT|INPLACE|COPY}, LOCK [=] { DEFAULT| NONE| SHARED| EXCLUSIVE }

The parameters are :

ALGORITHM:
DEFAULT: Default mode , stay MySQL 8.0 in , If the specified ALGORITHM, So it's going to be a priority INSTANT Algorithm , If not, use it again INPLACE Algorithm , If not INPLACE The algorithm uses COPY By
INSTANT:8.0 New algorithm added in , Adding columns is an immediate return . But it can't be a virtual column . It's a simple principle , For a new column , All the original data in the table does not change immediately , Just record the column and default value in the table dictionary , For the default Dynamic Line format ( In fact, that is Compressed Variants ), If this column is updated, the original data is marked as deleted and the updated record is appended at the end . That's to say, there's no space left in advance , After updating, the row record space may change frequently . But for most businesses , It's all the latest time records that will be modified , So it's not a big problem .
INPLACE: Modify directly on the original table , Will not copy temporary tables , You can change it one by one , It won't produce a lot of undolog as well as redolog, It won't take much buffer. You can avoid rebuilding the table IO and CPU Consume , Good performance and concurrency during guarantee period .
COPY: Copy to a temporary new table to modify . Because of the record copy , A lot of undolog as well as redolog, And take up a lot of buffer, Impact on business performance .
LOCK:
DEFAULT: and ALGORITHM Of DEFAULT similar .
NONE: unlocked , Allow concurrent reading and updating of tables .
SHARED: Shared lock , Read allowed, update not allowed .
EXCLUSIVE: Reading and updating are not allowed .

Can pass :

ALTER TABLE Your watch ADD COLUMN New column char(128), ALGORITHM=INSTANT, LOCK=NONE;

A similar statement , Add fields online . It's better to be clear ALGORITHM as well as LOCK, In this way DDL You can know exactly how much impact it will have on the online business when you go online .

It can be seen that , In the beginning, you need to metadata lock,metadata lock Is in 5.5 It was introduced into mysql, There was a similar mechanism to protect metadata before , It's just not explicitly stated metadata lock It's just a concept . however 5.5 The previous version ( such as 5.1) And 5.5 There is a significant difference in metadata protection between later versions ,5.1 The protection of metadata is statement level ,5.5 about metadata The protection of is transaction level . The so-called statement level , That is, after the statement is executed , Whether the transaction is committed or rolled back , Its table structure can be updated by other sessions ; The transaction level is released at the end of the transaction metadata lock.

introduce metadata lock after , Mainly solved 2 A question , One is transaction isolation , For example, at the level of repeatable isolation , conversation A stay 2 Query period , conversation B The table structure has been modified , The results of the two queries will be inconsistent , Can't meet the requirement of repeatable reading ; Another problem is data replication , For example, conversation A During execution of multiple update statements , Another conversation B Make table structure changes and submit first , It will lead to slave When redoing , Do it again first alter, Do it again update There will be replication errors .

If there are many transactions currently executing , And there are transactions that contain big queries , for example :

START TRANSACTION;
select count(*) from Your watch
....

In this way, similar transactions will be executed for a long time , It will also block .

therefore , In principle, :

1. Avoid big business .
2. Change the table structure at the low peak of business .


One day a day - MySQL Add a column to a large table
Comment on

版权声明
本文为[A daily question for the architect of Fuda]所创,转载请带上原文链接,感谢
https://javamana.com/2021/01/20210120224458540I.html

  1. 【计算机网络 12(1),尚学堂马士兵Java视频教程
  2. 【程序猿历程,史上最全的Java面试题集锦在这里
  3. 【程序猿历程(1),Javaweb视频教程百度云
  4. Notes on MySQL 45 lectures (1-7)
  5. [computer network 12 (1), Shang Xuetang Ma soldier java video tutorial
  6. The most complete collection of Java interview questions in history is here
  7. [process of program ape (1), JavaWeb video tutorial, baidu cloud
  8. Notes on MySQL 45 lectures (1-7)
  9. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  10. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  11. 精进 Spring Boot 03:Spring Boot 的配置文件和配置管理,以及用三种方式读取配置文件
  12. Refined spring boot 03: spring boot configuration files and configuration management, and reading configuration files in three ways
  13. 【递归,Java传智播客笔记
  14. [recursion, Java intelligence podcast notes
  15. [adhere to painting for 386 days] the beginning of spring of 24 solar terms
  16. K8S系列第八篇(Service、EndPoints以及高可用kubeadm部署)
  17. K8s Series Part 8 (service, endpoints and high availability kubeadm deployment)
  18. 【重识 HTML (3),350道Java面试真题分享
  19. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  20. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  21. [re recognize HTML (3) and share 350 real Java interview questions
  22. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  23. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  24. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  25. RPC 1: how to develop RPC framework from scratch
  26. 造轮子系列之RPC 1:如何从零开始开发RPC框架
  27. RPC 1: how to develop RPC framework from scratch
  28. 一次性捋清楚吧,对乱糟糟的,Spring事务扩展机制
  29. 一文彻底弄懂如何选择抽象类还是接口,连续四年百度Java岗必问面试题
  30. Redis常用命令
  31. 一双拖鞋引发的血案,狂神说Java系列笔记
  32. 一、mysql基础安装
  33. 一位程序员的独白:尽管我一生坎坷,Java框架面试基础
  34. Clear it all at once. For the messy, spring transaction extension mechanism
  35. A thorough understanding of how to choose abstract classes or interfaces, baidu Java post must ask interview questions for four consecutive years
  36. Redis common commands
  37. A pair of slippers triggered the murder, crazy God said java series notes
  38. 1、 MySQL basic installation
  39. Monologue of a programmer: despite my ups and downs in my life, Java framework is the foundation of interview
  40. 【大厂面试】三面三问Spring循环依赖,请一定要把这篇看完(建议收藏)
  41. 一线互联网企业中,springboot入门项目
  42. 一篇文带你入门SSM框架Spring开发,帮你快速拿Offer
  43. 【面试资料】Java全集、微服务、大数据、数据结构与算法、机器学习知识最全总结,283页pdf
  44. 【leetcode刷题】24.数组中重复的数字——Java版
  45. 【leetcode刷题】23.对称二叉树——Java版
  46. 【leetcode刷题】22.二叉树的中序遍历——Java版
  47. 【leetcode刷题】21.三数之和——Java版
  48. 【leetcode刷题】20.最长回文子串——Java版
  49. 【leetcode刷题】19.回文链表——Java版
  50. 【leetcode刷题】18.反转链表——Java版
  51. 【leetcode刷题】17.相交链表——Java&python版
  52. 【leetcode刷题】16.环形链表——Java版
  53. 【leetcode刷题】15.汉明距离——Java版
  54. 【leetcode刷题】14.找到所有数组中消失的数字——Java版
  55. 【leetcode刷题】13.比特位计数——Java版
  56. oracle控制用户权限命令
  57. 三年Java开发,继阿里,鲁班二期Java架构师
  58. Oracle必须要启动的服务
  59. 万字长文!深入剖析HashMap,Java基础笔试题大全带答案
  60. 一问Kafka就心慌?我却凭着这份,图灵学院vip课程百度云