溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么線上變更數(shù)據(jù)庫表結(jié)構(gòu)

發(fā)布時(shí)間:2021-11-16 16:42:34 來源:億速云 閱讀:262 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容介紹了“怎么線上變更數(shù)據(jù)庫表結(jié)構(gòu)”的有關(guān)知識,在實(shí)際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

常見線上變更表結(jié)構(gòu)的方法有兩種,第一種是online ddl ,alter, 第二種是用類似工具percona。本文主要用實(shí)例描述下第二種方式的基本原理。

原理 

 percona  等工具的原理比較簡單。1,copy 一個(gè)新表;2,alter 表結(jié)構(gòu); 3,添加 update, delete, insert 三個(gè)觸發(fā)器;4,insert igonore 原表的數(shù)據(jù);5,刪掉觸發(fā)器。這里不用加鎖就是因?yàn)橛杏|發(fā)器在同步新增的改動(dòng),所有,一旦以前表有觸發(fā)器,就不適合這種方式。

操作實(shí)例

1, 新建一個(gè)表

create table person(id int ,name varchar(8), PRIMARY KEY (id)) ;

2, 插入1條記錄

insert person (id, name) values (1, "zhangsan");

3,  變更表結(jié)構(gòu)

pt-online-schema-change --alter 'add column sex varchar(100)' p='123456',u=root,D=test,t=person --no-check-replication-filters --execute

4, 日志解析:

No slaves found.  See --recursion-method if host didi has slaves.
Not checking slave lag because no slaves were found and --check-slave-lag was not specified.
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 `test`.`person`...
Creating new table...
Created new table test._person_new OK.
Altering new table...
Altered `test`.`_person_new` OK.
2019-08-29T20:05:34 Creating triggers...
2019-08-29T20:05:34 Created triggers OK.
2019-08-29T20:05:34 Copying approximately 1 rows...
2019-08-29T20:05:34 Copied rows OK.
2019-08-29T20:05:34 Analyzing new table...
2019-08-29T20:05:34 Swapping tables...
2019-08-29T20:05:34 Swapped original and new tables OK.
2019-08-29T20:05:34 Dropping old table...
2019-08-29T20:05:34 Dropped old table `test`.`_person_old` OK.
2019-08-29T20:05:34 Dropping triggers...
2019-08-29T20:05:34 Dropped triggers OK.
Successfully altered `test`.`person`.

這里沒有insert ignore 是因?yàn)椴僮鬟^程中,沒有增刪改的記錄,并沒有觸發(fā)。這里還可以做下壓測,看記錄插入速度如何。

“怎么線上變更數(shù)據(jù)庫表結(jié)構(gòu)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI