MySQL 5.6 online ddl 測試

來源:互聯網
上載者:User

在生產環境中,因為業務需求,改動mysql表結構,特別是資料量特別大的時候,幾百萬甚至幾千萬的資料,mysql online ddl操作非常讓mysql dba揪心。5.6版本支援online ddl。

為什麼mysql ddl操作讓dba揪心呢?因為mysql ddl操作內部原理是這樣的:

1create table tmp  like table2  (建立暫存資料表,與原表相似)

2insert into tmp select *  from table2  (原資料拷貝到暫存資料表裡,並且索引等資訊與原表一樣)

3drop table table1  刪除原表

4rename tmp to table1  暫存資料表名改名為原表

在整個過程中,原表table1加共用鎖定(s鎖),這樣對業務影響就很大,作業造成阻塞(曾經同事晚上11點增加欄位,導致訂單業務受到影響)接下來測試一下。在mysql5.6版本裡,進行了最佳化,alter table table1 add  ….操作時,增、刪、改、查都不會鎖表.

首先準備測試資料,盡量千萬以上,測試效果明顯

[root@aeolus1 bin]# sysbench --test=oltp--oltp-table-size=15000000 --mysql-db=test --mysql-user=root prepare

sysbench 0.4.12:  multi-threaded system evaluation benchmark

No DB drivers specified, using mysql

Creating table 'sbtest'...

Creating 15000000 records in table'sbtest'...

其中一個會話執行

mysql> alter table sbtest add testvarchar(10);

另一會話執行

mysql> insert into sbtestvalues(0,1,'abc','abc');

Query OK, 1 row affected (0.44 sec)

mysql> delete  from sbtest where id=2000;

Query OK, 1 row affected (1.21 sec)

mysql> update sbtest set k=3 whereid=20000;

Query OK, 1 row affected (0.59 sec)

Rows matched: 1  Changed: 1 Warnings: 0

順利執行完畢,沒有鎖表。

當有會話訪問時,花費時間比較長,這時進行alter table sbtest add 操作,alter table sbtest操作會等這個會話結束後,再進行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.