4

MySQL InnoDB 的 OPTIMIZE TABLE 的 Lock

 3 years ago
source link: https://blog.gslin.org/archives/2021/09/12/10318/mysql-innodb-%e7%9a%84-optimize-table-%e7%9a%84-lock/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

MySQL InnoDB 的 OPTIMIZE TABLE 的 Lock

Backend Twhttps://www.facebook.com/groups/616369245163622/posts/2467225396744655/ 這邊看到:

先大概回答一下假設,DELETE 後的空間是可以被同一個表格重複使用的,所以應該是還好,不過離峰時間跑一下 OPTIMIZE TABLE 也沒什麼關係就是了。

裡面提到的「13.7.2.4 OPTIMIZE TABLE Statement」(MySQL 5.7 文件) 以及「13.7.2.4 OPTIMIZE TABLE Statement」(MySQL 5.6 文件) 都有講到目前比較新的版本都已經是 Online DDL 了:(這邊抓 5.6 的文件,有支援的版本資訊)

Prior to Mysql 5.6.17, OPTIMIZE TABLE does not use online DDL. Consequently, concurrent DML (INSERT, UPDATE, DELETE) is not permitted on a table while OPTIMIZE TABLE is running, and secondary indexes are not created as efficiently.

As of MySQL 5.6.17, OPTIMIZE TABLE uses online DDL for regular and partitioned InnoDB tables, which reduces downtime for concurrent DML operations. The table rebuild triggered by OPTIMIZE TABLE is completed in place. An exclusive table lock is only taken briefly during the prepare phase and the commit phase of the operation. During the prepare phase, metadata is updated and an intermediate table is created. During the commit phase, table metadata changes are committed.

文件上有提到會有一小段 lock 的時間,不過一般來說應該不會造成太大問題。

這邊要講的是早期的經典工具 pt-online-schema-change (pt-osc),這是使用 TRIGGER-based 的方式在跑,他的範例就直接提供了一個不需要 Online DDL 支援的版本:

Change sakila.actor to InnoDB, effectively performing OPTIMIZE TABLE in a non-blocking fashion because it is already an InnoDB table:

pt-online-schema-change --alter "ENGINE=InnoDB" D=sakila,t=actor

這在早期的時候還蠻常被拿出來用的,如果還在維護一些舊系統的話還蠻推薦的...

Related

Amazon Aurora 改善 ALTER TABLE 時增加 column 的速度

Amazon Aurora (MySQL) 提昇了增加 column 操作的速度:「Amazon Aurora Supports Fast DDL Operations」,細節可以在「Amazon Aurora Under the Hood: Fast DDL」這邊看到。 這次加速是限制在 nullable 欄位: We’re addressing this mess, starting with the most common DDL operation we’ve seen: adding a nullable column at the end of a table. 在 MySQL 5.6 (Online DDL Overview) 與…

April 7, 2017

In "AWS"

關於 RDBMS 的 Schema Migration...

在「NoSQL 大腸花」這份投影片裡面的 Page 12 有提到關於 RDBMS 的 Schema Migration: 以目前 open source 的兩個專案,MySQL 與 PostgreSQL 來看,裡面提到的 lock 應該都不是問題... 首先是 MySQL 的部份,真的量大的網站都應該是往 InnoDB 投靠,而 pt-online-schema-change 在這個領域則是處理的很好。 Facebook 的 Mark Callaghan 曾經在 2010 年寫過一篇關於 InnoDB 的 online schema change 的原理:「Online Schema Change for MySQL」,主要是利用 Trigger 的機制,用七個步驟架構出沒有 downtime 的 online scheme change。 就算不考慮…

February 4, 2015

In "Computer"

Eventbrite 的 MySQL 升級計畫

在 2021 年看到 Eventbite 的 MySQL 升級計畫:「MySQL High Availability at Eventbrite」。 看起來是 2019 年年初的時候 MySQL 5.1 出問題,後續決定安排升級,在 2019 年年中把系統升級到 MySQL 5.7 (Percona Server 版本): Our first major hurdle was to get current with our version of MySQL. In July, 2019 we completed the MySQL 5.1 to MySQL 5.7 (v5.7.19-17-log Percona…

January 28, 2021

In "Computer"

a611ee8db44c8d03a20edf0bf5a71d80?s=49&d=identicon&r=gAuthor Gea-Suan LinPosted on September 12, 2021Categories Computer, Database, Murmuring, MySQL, SoftwareTags change, database, db, ddl, innodb, lock, mysql, online, optimize, percona, pt, pt-online-schema-change, pt-osc, rdbms, schema, table, trigger

One thought on “MySQL InnoDB 的 OPTIMIZE TABLE 的 Lock”

  1. b86d26a1622ea99bbd1147cd49824aae?s=42&d=identicon&r=gWill Wang says:

    哈哈! 沒想到我的發問被大大拿到這討論 :p
    剛剛稍早我有再繼續問:

    「是說我再次細看這文件跟 [1] , 照文件說, 如果情況允許的話 (例如沒用 FULLTEXT indexes 或是 engine 有被支援(innoDB有)), rebuild 就不會用 table copy method, 是會用 in place 的方式做(The table rebuild triggered by OPTIMIZE TABLE is completed in place), 也就是只有短的時間會 lock (An exclusive table lock is only taken briefly), 不知道這樣的理解是否正確? 🙂
    thanks!
    [1]
    https://dev.mysql.com/doc/refman/5.7/en/optimize-table.html

    期待有人回答!
    然後, 我先繼續 share 一點觀察, 由於我推測我的 case (mysql 版本 / DB engine / index 使用 / ... etc ) 應該是可以使用到 online DDL / in-place, 剛剛有實際跑了 optimize table my_table 這 command:

    1. command 約跑了 50~70 sec (optimize 後的 size 不到 10 GB)
    2. 確實成功以 in-place 方式在跑, 因為過程中依然可以一直 insert, 表示這期間沒有或幾乎沒有 lock

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment

Name *

Email *

Website

Notify me of follow-up comments by email.

Notify me of new posts by email.

Post navigation


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK