MySQL 5.7使用pt-online-schema-change對大表加欄位
業務需求:
對一張線上的3000W行的表加欄位:
用普通方式,建議小表使用:
ALTER TABLE `idempotent`
ADD COLUMN `orderid` VARCHAR(20) NULL DEFAULT NULL COMMENT '業務單據號' AFTER `isdelete`;
ALTER TABLE ship_type ADD printer_name VARCHAR(50) COMMENT '印表機名稱';
大表會鎖表。
對大表加欄位,建議用pt-online-schema-change工具,可以不鎖表。
對錶 idempotent加欄位 orderid:
命令:
[root@DB001 ~]#pt-online-schema-change --user=root --password=123456 --host=192.168.1.18 --alter "ADD COLUMN orderid VARCHAR(20) NULL DEFAULT NULL COMMENT '業務單據號'" D=ora,t=idempotent --no-check-replication-filters --execute
Found 1 slaves:
localhost.localdomain -> 192.168.1.19:socket
Will check slave lag on:
localhost.localdomain -> 192.168.1.19:socket
Operation, tries, wait:
analyze_table, 10, 1
copy_rows, 10, 0.25
create_triggers, 10, 1
drop_triggers, 10, 1
swap_tables, 10, 1
update_foreign_keys, 10, 1
Altering `ora`.`idempotent`...
Creating new table...
Created new table ora._idempotent_new OK.
Altering new table...
Altered `ora`.`_idempotent_new` OK.
2019-10-23T19:01:00 Creating triggers...
2019-10-23T19:01:00 Created triggers OK.
2019-10-23T19:01:00 Copying approximately 24968401 rows...
Copying `ora`.`idempotent`: 4% 10:53 remain
Copying `ora`.`idempotent`: 8% 10:40 remain
Copying `ora`.`idempotent`: 11% 11:49 remain
Copying `ora`.`idempotent`: 14% 12:08 remain
Copying `ora`.`idempotent`: 17% 12:09 remain
Copying `ora`.`idempotent`: 19% 12:15 remain
Copying `ora`.`idempotent`: 22% 12:15 remain
Copying `ora`.`idempotent`: 24% 12:11 remain
Copying `ora`.`idempotent`: 27% 12:09 remain
Copying `ora`.`idempotent`: 29% 12:03 remain
Copying `ora`.`idempotent`: 31% 11:45 remain
Copying `ora`.`idempotent`: 34% 11:23 remain
Copying `ora`.`idempotent`: 37% 11:01 remain
Copying `ora`.`idempotent`: 39% 10:39 remain
Copying `ora`.`idempotent`: 42% 10:11 remain
Copying `ora`.`idempotent`: 44% 09:50 remain
Copying `ora`.`idempotent`: 47% 09:35 remain
Copying `ora`.`idempotent`: 49% 09:14 remain
Copying `ora`.`idempotent`: 52% 08:44 remain
Copying `ora`.`idempotent`: 55% 08:10 remain
Copying `ora`.`idempotent`: 59% 07:17 remain
Copying `ora`.`idempotent`: 63% 06:21 remain
Copying `ora`.`idempotent`: 67% 05:32 remain
Copying `ora`.`idempotent`: 71% 04:44 remain
Copying `ora`.`idempotent`: 75% 03:59 remain
Copying `ora`.`idempotent`: 80% 03:11 remain
Copying `ora`.`idempotent`: 84% 02:28 remain
Copying `ora`.`idempotent`: 88% 01:49 remain
Copying `ora`.`idempotent`: 92% 01:08 remain
Copying `ora`.`idempotent`: 96% 00:28 remain
2019-10-23T19:18:24 Copied rows OK.
2019-10-23T19:18:24 Analyzing new table...
2019-10-23T19:18:24 Swapping tables...
2019-10-23T19:18:24 Swapped original and new tables OK.
2019-10-23T19:18:24 Dropping old table...
2019-10-23T19:18:26 Dropped old table `ora`.`_idempotent_old` OK.
2019-10-23T19:18:26 Dropping triggers...
2019-10-23T19:18:26 Dropped triggers OK.
Successfully altered `ora`.`idempotent`.
You have mail in /var/spool/mail/root
整個過程持續10幾分鐘,不鎖表。
欄位新增完成。
PS:
切記在業務低峰執行。
可以指定字符集,防止加好欄位的表的註釋出現亂碼。
修改註釋:
ALTER TABLE idempotent MODIFY COLUMN `sysno` BIGINT(20) NOT NULL AUTO_INCREMENT COMMENT '編號';
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22996654/viewspace-2661185/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.7使用pt-online-schema-change對大表加索引MySql索引
- MySQL8.0大表秒加欄位,是真的嗎?MySql
- MySQL 5.6使用pt-online-schema-change線上修改大表欄位長度MySql
- redis對hash欄位加鎖Redis
- MySQL5.7密碼欄位變更MySql密碼
- mysql表最多欄位數MySql
- mysql建立表的時候對欄位和表新增COMMENTMySql
- MySQL 大欄位問題MySql
- Mysql多欄位大表的幾種優化方法MySql優化
- mysql表操作(alter)/mysql欄位型別MySql型別
- MySQL5.6 5.7使用者臨時表 對比MySql
- pl/sql dev建表加欄位時建的欄位名都帶了“”SQLdev
- MySQL使用pt-online-change-schema實現線上加欄位MySql
- [原創] mysql表欄位多少限定MySql
- mysql 5.7 新特性中線上in-place 修改欄位的大小MySql
- MySQL 更新同一個表不同欄位MySql
- mysql修改表欄位學習筆記MySql筆記
- 5_MySQL 表的欄位約束MySql
- MySQL鎖(二)表鎖:為什麼給小表加欄位會導致整個庫掛掉?MySql
- MySQL 更新一個表裡的欄位等於另一個表某欄位的值MySql
- NRIV表欄位的設定使用
- oracle刪除表欄位和oracle表增加欄位Oracle
- mysql修改表、欄位、庫的字符集MySql
- 更改MySql表和欄位區分大小寫MySql
- oracle大表新增欄位default經驗分享Oracle
- 給mybatis新增自動建表,自動加欄位的功能MyBatis
- MySQL中JSON欄位的使用技巧MySqlJSON
- MySQL 中 JSON 欄位的使用技巧MySqlJSON
- MySQL 分割槽表 partition線上修改分割槽欄位MySql
- mysql 資料庫 表 欄位 編碼修改 方法MySql資料庫
- oracle檢視該使用者的所有表名字、表註釋、欄位名、欄位註釋、是否為空、欄位型別Oracle型別
- SQL新增表欄位SQL
- JDBC讀寫MySQL的大欄位資料薦JDBCMySql
- 使用Docker Mysql 5.7DockerMySql
- MySQL 欄位約束MySql
- mysql中文欄位排序MySql排序
- MSSQL大資料量增加欄位耗時對比SQL大資料
- windows域控裡,屬性和欄位對映表Windows