mysql online ddl

來源:互聯網
上載者:User

標籤:mysql ddl

目前InnoDB引擎是通過以下步驟來進行DDL的:

1 按照原始表(original_table)的表結構和DDL語句,建立一個不可見的暫存資料表(tmp_table)

2 在原表上加write lock,阻塞所有更新操作(insert、delete、update等)

3 執行insert into tmp_table select * from original_table

4 rename original_table和tmp_table,最後drop original_table

5 釋放 write lock。

我們可以看見在InnoDB執行DDL的時候,原表是只能讀不能寫的。為此 perconal 推出一個工具 pt-online-schema-change ,其特點是修改過程中不會造成讀寫阻塞。

工作原理:

如果表有外鍵,除非使用 –alter-foreign-keys-method 指定特定的值,否則工具不予執行。

1 建立一個和你要執行 alter 操作的表一樣的空表結構。

2 執行表結構修改,然後從原表中的資料到copy到 表結構修改後的表,

3 在原表上建立觸發器將 copy 資料的過程中,在原表的更新操作 更新到新表.

注意:如果表中已經定義了觸發器這個工具就不能工作了。

4 copy 完成以後,用rename table 新表代替原表,預設刪除原表。


pt-online-schema-change -uroot -h172.17.10.13 -p‘test!‘ --alter=‘add key user_email(email)‘ --execute D=ggxk_account,t=ggxk_user

pt-online-schema-change -uroot -h172.17.10.13 -p‘test!‘ --alter=‘drop key user_email ‘ --execute D=ggxk_account,t=ggxk_user

pt-online-schema-change -uroot -h172.17.10.13 -p‘test!‘ --alter=‘add column lhb int ‘ --execute D=ggxk_account,t=ggxk_user

pt-online-schema-change -uroot -h172.17.10.13 -p‘test!‘ --alter=‘drop column lhb ‘ --execute D=ggxk_account,t=ggxk_user


mysql online ddl

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.