需求:
創(chuàng)新互聯(lián)網(wǎng)站建設(shè)由有經(jīng)驗(yàn)的網(wǎng)站設(shè)計(jì)師、開(kāi)發(fā)人員和項(xiàng)目經(jīng)理組成的專業(yè)建站團(tuán)隊(duì),負(fù)責(zé)網(wǎng)站視覺(jué)設(shè)計(jì)、用戶體驗(yàn)優(yōu)化、交互設(shè)計(jì)和前端開(kāi)發(fā)等方面的工作,以確保網(wǎng)站外觀精美、網(wǎng)站設(shè)計(jì)、網(wǎng)站制作易于使用并且具有良好的響應(yīng)性。
應(yīng)大數(shù)據(jù)部門要求,需要在指定表增加create_time字段并給與當(dāng)前時(shí)間做缺省值。
環(huán)境:
DB:阿里云RDS 的MySQL5.6
OS: centos7
有好幾張大表,過(guò)1億,最大的7個(gè)億,考慮使用pt-online-schema-change工具,夜里執(zhí)行,并根據(jù)工作量分幾次執(zhí)行
選擇方案時(shí),老大意思:要有幾套都列出來(lái),然后比較優(yōu)缺點(diǎn),
1.online DDL
2.pt-online-schema-change工具
3.過(guò)億表,rename成歷史表,然后新建表包含create_time字段,前端要修改程序。
沒(méi)過(guò)億的,就pt-online-schema-change工具修改
4.通過(guò)mycat 對(duì)大表做分庫(kù)分表操作 ,一勞永逸
因rds不能直接ssh上去,所以申請(qǐng)一臺(tái)同網(wǎng)段的ECS服務(wù)器用于執(zhí)行 pt-online-schema-change
命令執(zhí)行機(jī)器:centos7.x mysql5.6client pt工具集
一、工具安裝
yum -y install perl-TermReadKey.x86_64
yum -y install perl-IO-Socket-SSL
yum -y install perl-DBI.x86_64
yum -y install perl-DBD-MySQL.x86_64
yum -y install perl-Digest-MD5
wget -c https://www.percona.com/downloads/percona-toolkit/3.0.12/binary/redhat/7/x86_64/percona-toolkit-3.0.12-1.el7.x86_64.rpm
rpm -ivh percona-toolkit-3.0.12-1.el7.x86_64.rpm
二、監(jiān)控工具:
innotop -h xxx.xxx.xxx.xxx
-u 用名
-p 密碼
輸入:Q 可看當(dāng)前查詢情況
三、限制
1)服務(wù)器空間檢查與評(píng)估
如最大的表50G,索引10G,所以至少需要 “60G臨時(shí)空間” + “binlog空間也算50G”吧 ,也就110G以上
使用OSC會(huì)使增加一倍的空間,包括索引
而且在 Row Based Replication 下,還會(huì)寫(xiě)一份binlog。不要想當(dāng)然使用–set-vars去設(shè)置 sql_log_bin=0,因?yàn)樵谶@個(gè)session級(jí)別,alter語(yǔ)句也要在從庫(kù)上執(zhí)行,除非你對(duì)從庫(kù)另有打算。
2)原表不能有trigger檢查
3)外鍵檢查,最好不要有外鍵
4) 主鍵或唯一索引檢查
絕大部分情況下表上需要有主鍵或唯一索引,因?yàn)楣ぞ咴谶\(yùn)行當(dāng)中為了保證新表也是最新的,需要舊表上創(chuàng)建 DELETE和UPDATE 觸發(fā)器,同步到新表的時(shí)候有主鍵會(huì)更快。個(gè)別情況是,當(dāng)alter操作就是在c1列上建立主鍵時(shí),DELETE觸發(fā)器將基于c1列。
5)mysql服務(wù)器參數(shù)檢查
(1)連接時(shí)間參數(shù):
wait_timeout 值24小時(shí),
innodb_lock_wait_timeout 50秒
(2)在Online DDL過(guò)程中,需要保持這段時(shí)間內(nèi),“增刪改”的最大日志大小。 小了報(bào)錯(cuò),大了產(chǎn)生鎖表時(shí)間長(zhǎng)
innodb_online_alter_log_max_size 134217728 (這是RDS的默認(rèn)值,我沒(méi)有修改)
這個(gè)參數(shù)是mysql 5.6.6引入的,因?yàn)樵趏nline ddl過(guò)程中需要保持delete、update、insert這些數(shù)據(jù),所以需要一個(gè)日志去保持,這個(gè)參數(shù)就是限制這個(gè)日志的最大大小,當(dāng)ddl過(guò)程中需要的這個(gè)日志的大小比這個(gè)限制還大的時(shí)候就會(huì)報(bào)錯(cuò)。
該參數(shù)為“動(dòng)態(tài)”參數(shù)且“全局”的,設(shè)置方法: set global innodb_online_alter_log_max_size=402653184;
官網(wǎng)文檔:
http://dev.mysql.com/doc/refman/5.6/en/innodb-create-index-overview.html
http://dev.mysql.com/doc/refman/5.7/en/innodb-create-index-overview.html
(3)主從復(fù)制參數(shù)
max_binlog_cache_szie
max_allowed_packet
四、產(chǎn)生腳本
#for i in `seq 0 9`
>do
>echo “pt-online-schema-change --no-version-check --charset=utf8 --user=mysqldba --ask-pass --host=127.0.0.1 --alter=\"add COLUMN update_time datetime\" P=3306,D=escore,t=t_user_$i --execute --nocheck-replication-filters” >> batch.sh
>done
#for i in {a..f}
>do
>echo "pt-online-schema-change -no-version-check --charset=utf8 --user=mysqldba --password=xx --host=127.0.0.1 --alter=\"add COLUMN update_time datetime\" P=3306,D=escore,t=t_user_$i --execute --nocheck-replication-filters" >> batch.sh
>done
select concat('pt-online-schema-change --no-version-check --charset=utf8 --user=xx',
' --ask-pass --alter="add COLUMN update_time datetime"',
' h=xx,P=3306,D=', table_schema,
',t=',
table_name,
' --execute')
from information_schema.tables
where table_schema='xx'
說(shuō)明 :
1)密碼
使用參數(shù) --password 會(huì)在shell歷史命令中留下密碼,
這里使用 --ask-pass 提示用戶輸入密碼,但這樣就不能批量跑
可以在 /etc/my.conf 的[client]下配置
user=xxx
password=xxxx
chmod 600 /etc/my.conf 非root用戶不能看
然后就可以不用輸入密碼了
pt-online-schema-change --no-version-check --alter "add column create_time timestamp default current_timestamp comment '入庫(kù)時(shí)間'" --host=192.168.178.131 P=3310,D=testdb,t=t1 --execute
2)
五、執(zhí)行期間異常回退方法
執(zhí)行期間如果有報(bào)錯(cuò),舉例表名時(shí)候card 則
select * from information_schema.triggers where table_name='card'
drop trigger triggername
刪除新建的表(都是以下劃線_開(kāi)頭的new表)
drop table _card_new
調(diào)整參數(shù),重新執(zhí)行
六、可調(diào)整的參數(shù)
表:bw_fund_record 主鍵id
pt-online-schema-change --no-version-check --check-replication-filters --lock-wait-timeout=3 --alter "add (create_time timestamp not null )" -h292.168.178.131 P=3306,u=user1,p=admin,D=testdb,t=e_card --execute
pt-online-schema-change --user=root --password=123456 -h localhost --alter "ADD COLUMN content text" D=db1,t=tb_2--no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --charset=utf8 --execute
pt-online-schema-change --user=root --password="xxxxx" --host=192.168.xx.xx D=M_xx,t=T_xx --alter "ADD Fxxxxx'" --charset=utf8 --no-check-replication-filters --alter-foreign-keys-method=auto --recursion-method=none --print --execute
--critical-load 先不加 (跟負(fù)載有關(guān))
--max-load 先不加 (跟負(fù)載有關(guān))
--max-lag
當(dāng)是主從環(huán)境,不在乎從的延遲,則需要加 --recursion-method=none 參數(shù)。當(dāng)需要盡可能的對(duì)服務(wù)產(chǎn)生小的影響,則需要加上 --max-load參數(shù)。
1)上面的測(cè)試都是把原表刪除了,要是不刪除原表則,則使用 --no-drop-old-table選項(xiàng),這樣會(huì)讓原表(_test_binlog_old)保留。
2)要是在線上環(huán)境上添加字段,但又不想影響到服務(wù),可以用 --max-load選項(xiàng)去執(zhí)行該工具,默認(rèn)是Threads_running=25,即當(dāng)前有這么多線程在運(yùn)行的時(shí)候就暫停數(shù)據(jù)的復(fù)制,等少于該值則繼續(xù)復(fù)制數(shù)據(jù)到新表。
pt-online-schema-change --host=xxxxx -P 3306 --charset=utf8 -u root -p 'xxxxxx;' --alter='add column door_no varchar(200) comment "居住門樓牌"
' --print --execute D=lzmh_wlw_db,t=wlw_room --critical-load="Threads_running=200"
七、最中腳本的樣子
pt-online-schema-change --no-version-check --execute --alter "add column c1 int" h=xxx.mysql.rds.aliyuncs.com,P=3306,u=jacky,p=xxx,D=jacky,t=x 阿里云
pt-online-schema-change --no-version-check --alter "add column create_time timestamp default current_timestamp comment '創(chuàng)建時(shí)間'" P=3306,u=beadwallet,D=beadwalletloan,t=bw_xg_mid_score
pt-online-schema-change --no-version-check --alter "add column create_time timestamp default current_timestamp comment '創(chuàng)建時(shí)間'" P=3306,u=beadwallet,D=beadwalletloan,t=bw_borrower_auth
pt-online-schema-change --no-version-check --lock-wait-timeout=3 --alter "add column create_time timestamp default current_timestamp comment '創(chuàng)建時(shí)間'" P=3306,u=sasbeadwallet,D=sassevenwallet,t=bw_card_city
time ./pt-online-schema-change --no-version-check --alter "modify column create_time timestamp default current_timestamp comment '入庫(kù)時(shí)間'" -hhostname1 P=3306,u=user1,D=db1,t=bw_bigfintech_report --ask-pass --execute
time ./pt-online-schema-change --no-version-check --alter "add column create_time timestamp default current_timestamp comment '入庫(kù)時(shí)間'" -hhostname1 P=3306,u=user1,D=db1,t=bw_xg_emergency_analysis --ask-pass --execute
八、遇到的問(wèn)題:
1)字符集
大量修改后,第二天發(fā)現(xiàn),所有修改過(guò)的字段的注釋都變成亂碼了,還還只是注釋亂碼,數(shù)據(jù)正常。
現(xiàn)在回想起來(lái)都后怕,犯了這樣一個(gè)低級(jí)的錯(cuò)誤,如果pt-online-schema-change 復(fù)制表不是在數(shù)據(jù)塊級(jí)別上,那都整個(gè)庫(kù)都變亂碼了。
mysql> show create table bw_city;
+---------+-------------------------------------------------------------------------------------------+
| Table | Create Table |
+---------+-------------------------------------------------------------------------------------------+
| bw_city | CREATE TABLE `bw_city` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parentCode` varchar(50) DEFAULT NULL COMMENT '??code:0????',
`citycode` varchar(50) NOT NULL COMMENT '??code',
`adcode` varchar(50) CHARACTER SET utf8mb4 NOT NULL COMMENT 'adcode',
`name` varchar(100) NOT NULL COMMENT '??',
`level` varchar(50) NOT NULL COMMENT '??',
`levelNum` tinyint(3) NOT NULL COMMENT '???1???2???3?',
`LAT` varchar(200) NOT NULL COMMENT '??',
`LNG` varchar(200) NOT NULL COMMENT '??',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '?…¥?o“?—?é—′',
PRIMARY KEY (`id`),
KEY `c_i_parentCode` (`parentCode`),
KEY `c_i_cityCode` (`citycode`),
KEY `c_i_adcode` (`adcode`)
) ENGINE=InnoDB AUTO_INCREMENT=3608 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC
因?yàn)槭荝DS,所以我們不能ssh到本地操作,就在另一臺(tái)機(jī)器(-hxxx)遠(yuǎn)程執(zhí)行 ,沒(méi)有加--charset 參數(shù)
指定session級(jí)別為latin1時(shí),新加的字段注釋顯示正常,但原有的字段還時(shí)亂碼
mysql> set names latin1;
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT column_comment FROM `information_schema`.`COLUMNS` where table_name='bw_city';
+----------------+
| column_comment |
+----------------+
| |
| ??code:0???? |
| ??code |
| adcode |
| ?? |
| ?? |
| ???1???2???3? |
| ?? |
| ?? |
| 入庫(kù)時(shí)間 |
+----------------+
10 rows in set (0.00 sec)
解決方法:
(1)--charset=xxx
默認(rèn)字符類型Latin1,例如如果值為utf8,就將輸出的字符設(shè)置為utf8格式,將mysql_enable_utf8傳遞給DBD::mysql,然后連接MySQL后運(yùn)行 SET NAMES UTF8 命令
遠(yuǎn)程執(zhí)行時(shí),如果本地字符集和服務(wù)器上的字符集一致,需要指定
time ./pt-online-schema-change --charset=utf8 --no-version-check --alter "modify column create_time timestamp default current_timestamp comment '入庫(kù)時(shí)間'" -hrm-uhostname1 P=3306,u=user1,D=db1,t=bw_bigfintech_report --ask-pass --execute
(2)在DSN參數(shù)中指定字符集
【DSN】
指定時(shí)注意大小寫(xiě)敏感,“=”左右不能有空格,多個(gè)值之間用逗號(hào)分隔
1. A charset
2. D database
3. F mysql_read_default_file
4. h host
5. p password
6. P port
7. S mysql_socket
8. t table
9. u user
(3)在/etc/my.cnf指定 字符集
[client]
port = 3310
default-character-set = utf8
socket ="/u01/my3310/run/my3310.sock"
user=root
password=xxx
2)存在trigger
pt-online-schema-change -u user -p password -h 10.0.200.195 \
--alter="MODIFY COLUMN f_receiver varchar(128) NOT NULL DEFAULT '' AFTER f_user_id" --dry-run D=db_name,t=table_name
The table `db_name`.`table_name` has triggers. This tool needs to create its own triggers, so the table cannot already have triggers.
3)no-version-check
pt-online-schema-change -uuser -ppassword --alter "add key id_provice(f_provice)" \
D=db_name,t=tb_name -h rdsxxxxxx.mysql.rds.aliyuncs.com
Can't use an undefined value as an ARRAY reference at /usr/bin/pt-online-schema-change line 7335.
這個(gè)錯(cuò)誤在阿里云RDS上執(zhí)行時(shí)出現(xiàn)的,原生5.6的版本上就沒(méi)問(wèn)題
應(yīng)該是pt去驗(yàn)證mysql server版本的時(shí)候從rds拿到的信息不對(duì),導(dǎo)致格式出錯(cuò)
九、服務(wù)器上的參數(shù):
mysql> show grants for 'usera'@'%' ;
+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Grants for usera@% |
+---------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GRANT PROCESS, REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'usera'@'%' IDENTIFIED BY PASSWORD '*xxx' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `db1ra`.* TO 'usera'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `db2seal`.* TO 'usera'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `db3loan`.* TO 'usera'@'%' |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES, EXECUTE, CREATE VIEW, SHOW VIEW, CREATE ROUTINE, ALTER ROUTINE, EVENT, TRIGGER ON `db0`.* TO 'usera'@'%' |
| GRANT SELECT ON `performance_schema`.* TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_transition` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`proc` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_name` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_leap_second` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`event` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`time_zone_transition_type` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`general_log` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`help_topic` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`func` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`help_category` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`help_relation` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`help_keyword` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`time_zone` TO 'usera'@'%' |
| GRANT SELECT ON `mysql`.`slow_log` TO 'usera'@'%' |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------+
20 rows in set (0.01 sec)
mysql> show global variables like 'log%';
+----------------------------------------+-------+
| Variable_name | Value |
+----------------------------------------+-------+
| log_bin | ON |
| log_bin_basename | |
| log_bin_index | |
| log_bin_trust_function_creators | ON |
| log_bin_use_v1_row_events | ON |
| log_error | |
| log_output | TABLE |
| log_queries_not_using_indexes | OFF |
| log_slave_updates | ON |
| log_slow_admin_statements | ON |
| log_slow_slave_statements | OFF |
| log_throttle_queries_not_using_indexes | 0 |
| log_warnings | 2 |
+----------------------------------------+-------+
13 rows in set (0.00 sec)
mysql> show variables like 'long_query_time';
+-----------------+----------+
| Variable_name | Value |
+-----------------+----------+
| long_query_time | 1.000000 |
+-----------------+----------+
1 row in set (0.00 sec)
mysql> show global variables like 'slave%';
+------------------------------+-----------------------+
| Variable_name | Value |
+------------------------------+-----------------------+
| slave_allow_batching | OFF |
| slave_checkpoint_group | 512 |
| slave_checkpoint_period | 300 |
| slave_compressed_protocol | OFF |
| slave_exec_mode | STRICT |
| slave_load_tmpdir | |
| slave_max_allowed_packet | 1073741824 |
| slave_net_timeout | 60 |
| slave_parallel_workers | 8 |
| slave_pending_jobs_size_max | 167772160 |
| slave_pr_mode | TABLE |
| slave_rows_search_algorithms | TABLE_SCAN,INDEX_SCAN |
| slave_skip_errors | OFF |
| slave_sql_verify_checksum | ON |
| slave_transaction_retries | 10 |
| slave_type_conversions | |
+------------------------------+-----------------------+
16 rows in set (0.00 sec)
mysql> show global variables like 'sql%';
+------------------------+----------------------+
| Variable_name | Value |
+------------------------+----------------------+
| sql_auto_is_null | OFF |
| sql_big_selects | ON |
| sql_buffer_result | OFF |
| sql_log_bin | ON |
| sql_log_off | OFF |
| sql_mode | |
| sql_notes | ON |
| sql_quote_show_create | ON |
| sql_safe_updates | OFF |
| sql_select_limit | 18446744073709551615 |
| sql_slave_skip_counter | 0 |
| sql_warnings | OFF |
+------------------------+----------------------+
mysql> show global variables like 'innodb%';
+------------------------------------------+-------------------------+
| Variable_name | Value |
+------------------------------------------+-------------------------+
| innodb_adaptive_flushing | ON |
| innodb_adaptive_flushing_lwm | 10 |
| innodb_adaptive_hash_index | ON |
| innodb_adaptive_hash_index_parts | 8 |
| innodb_adaptive_max_sleep_delay | 150000 |
| innodb_additional_mem_pool_size | 2097152 |
| innodb_api_bk_commit_interval | 5 |
| innodb_api_disable_rowlock | OFF |
| innodb_api_enable_binlog | OFF |
| innodb_api_enable_mdl | OFF |
| innodb_api_trx_level | 0 |
| innodb_autoextend_increment | 64 |
| innodb_autoinc_lock_mode | 1 |
| innodb_buffer_pool_dump_at_shutdown | OFF |
| innodb_buffer_pool_dump_now | OFF |
| innodb_buffer_pool_filename | ib_buffer_pool |
| innodb_buffer_pool_instances | 8 |
| innodb_buffer_pool_load_abort | OFF |
| innodb_buffer_pool_load_at_startup | OFF |
| innodb_buffer_pool_load_now | OFF |
| innodb_buffer_pool_size | 12884901888 |
| innodb_change_buffer_max_size | 25 |
| innodb_change_buffering | all |
| innodb_checksum_algorithm | innodb |
| innodb_checksums | ON |
| innodb_cmp_per_index_enabled | OFF |
| innodb_commit_concurrency | 0 |
| innodb_compression_failure_threshold_pct | 5 |
| innodb_compression_level | 6 |
| innodb_compression_pad_pct_max | 50 |
| innodb_concurrency_tickets | 5000 |
| innodb_data_file_path | ibdata1:200M:autoextend |
| innodb_data_home_dir | |
| innodb_defragment | OFF |
| innodb_defragment_fill_factor | 0.900000 |
| innodb_defragment_fill_factor_n_recs | 20 |
| innodb_defragment_frequency | 40 |
| innodb_defragment_n_pages | 7 |
| innodb_defragment_stats_accuracy | 0 |
| innodb_disable_sort_file_cache | ON |
| innodb_doublewrite | ON |
| innodb_encrypt_algorithm | aes_128_ecb |
| innodb_fast_shutdown | 1 |
| innodb_file_format | Barracuda |
| innodb_file_format_check | ON |
| innodb_file_format_max | Barracuda |
| innodb_file_per_table | ON |
| innodb_flush_log_at_timeout | 1 |
| innodb_flush_log_at_trx_commit | 1 |
| innodb_flush_method | O_DIRECT |
| innodb_flush_neighbors | 1 |
| innodb_flushing_avg_loops | 30 |
| innodb_force_load_corrupted | OFF |
| innodb_force_recovery | 0 |
| innodb_ft_aux_table | |
| innodb_ft_cache_size | 8000000 |
| innodb_ft_enable_diag_print | OFF |
| innodb_ft_enable_stopword | ON |
| innodb_ft_max_token_size | 84 |
| innodb_ft_min_token_size | 3 |
| innodb_ft_num_word_optimize | 2000 |
| innodb_ft_result_cache_limit | 2000000000 |
| innodb_ft_server_stopword_table | |
| innodb_ft_sort_pll_degree | 2 |
| innodb_ft_total_cache_size | 640000000 |
| innodb_ft_user_stopword_table | |
| innodb_io_capacity | 2000 |
| innodb_io_capacity_max | 4000 |
| innodb_large_prefix | OFF |
| innodb_lock_wait_timeout | 50 |
| innodb_locks_unsafe_for_binlog | OFF |
| innodb_log_buffer_size | 8388608 |
| innodb_log_compressed_pages | OFF |
| innodb_log_file_size | 1572864000 |
| innodb_log_files_in_group | 2 |
| innodb_log_group_home_dir | |
| innodb_lru_scan_depth | 1024 |
| innodb_max_dirty_pages_pct | 75 |
| innodb_max_dirty_pages_pct_lwm | 0 |
| innodb_max_purge_lag | 0 |
| innodb_max_purge_lag_delay | 0 |
| innodb_mirrored_log_groups | 1 |
| innodb_monitor_disable | |
| innodb_monitor_enable | |
| innodb_monitor_reset | |
| innodb_monitor_reset_all | |
| innodb_old_blocks_pct | 37 |
| innodb_old_blocks_time | 1000 |
| innodb_online_alter_log_max_size | 134217728 |
| innodb_open_files | 3000 |
| innodb_optimize_fulltext_only | OFF |
| innodb_page_size | 16384 |
| innodb_print_all_deadlocks | OFF |
| innodb_purge_batch_size | 300 |
| innodb_purge_threads | 1 |
| innodb_random_read_ahead | OFF |
| innodb_rds_quick_lru_limit_per_instance | 4096 |
| innodb_rds_trx_own_block_max | 128 |
| innodb_read_ahead_threshold | 56 |
| innodb_read_io_threads | 4 |
| innodb_read_only | OFF |
| innodb_replication_delay | 0 |
| innodb_rollback_on_timeout | OFF |
| innodb_rollback_segments | 128 |
| innodb_sort_buffer_size | 1048576 |
| innodb_spin_wait_delay | 30 |
| innodb_stats_auto_recalc | ON |
| innodb_stats_method | nulls_equal |
| innodb_stats_on_metadata | OFF |
| innodb_stats_persistent | ON |
| innodb_stats_persistent_sample_pages | 20 |
| innodb_stats_sample_pages | 8 |
| innodb_stats_transient_sample_pages | 8 |
| innodb_status_output | OFF |
| innodb_status_output_locks | OFF |
| innodb_strict_mode | OFF |
| innodb_support_xa | ON |
| innodb_sync_array_size | 1 |
| innodb_sync_spin_loops | 100 |
| innodb_table_locks | ON |
| innodb_thread_concurrency | 0 |
| innodb_thread_sleep_delay | 10000 |
| innodb_undo_directory | . |
| innodb_undo_logs | 128 |
| innodb_undo_tablespaces | 0 |
| innodb_use_native_aio | OFF |
| innodb_use_sys_malloc | ON |
| innodb_version | 5.6.16 |
| innodb_write_io_threads | 4 |
+------------------------------------------+-------------------------+
129 rows in set (0.01 sec)
十、執(zhí)行時(shí)間
1.上千萬(wàn)級(jí)表: 40分鐘
2.幾張過(guò)億的表: 1個(gè)多小時(shí)
這樣的速度算很快了
十一、參考:
1)pt-online-schema-change解讀
https://www.cnblogs.com/xiaoyanger/p/6043986.html
2)pt-online-schema-change使用說(shuō)明、限制與比較
https://blog.csdn.net/lijingkuan/article/details/68951089
十二、pt-osc 介紹
1. pt-osc工作過(guò)程
(1)創(chuàng)建一個(gè)和要執(zhí)行 alter 操作的表一樣的新的空表結(jié)構(gòu)(是alter之前的結(jié)構(gòu))
(2)在新表執(zhí)行alter table 語(yǔ)句(速度應(yīng)該很快)
(3)在原表中創(chuàng)建觸發(fā)器3個(gè)觸發(fā)器分別對(duì)應(yīng)insert,update,delete操作
(4)以一定塊大小從原表拷貝數(shù)據(jù)到臨時(shí)表,拷貝過(guò)程中通過(guò)原表上的觸發(fā)器在原表進(jìn)行的寫(xiě)操作都會(huì)更新到新建的臨時(shí)表
(5)Rename 原表到old表中,在把臨時(shí)表Rename為原表
(6)如果有參考該表的外鍵,根據(jù)alter-foreign-keys-method參數(shù)的值,檢測(cè)外鍵相關(guān)的表,做相應(yīng)設(shè)置的處理
(7)默認(rèn)最后將舊原表刪除
2. 常用選項(xiàng)說(shuō)明
只介紹部分常用的選項(xiàng)
–host=xxx –user=xxx –password=xxx
連接實(shí)例信息,縮寫(xiě)-h xxx -u xxx -p xxx,密碼可以使用參數(shù)–ask-pass 手動(dòng)輸入。
–alter
結(jié)構(gòu)變更語(yǔ)句,不需要 ALTER TABLE關(guān)鍵字。與原始ddl一樣可以指定多個(gè)更改,用逗號(hào)分隔。