1.對一個表執行ddl(新增字段)會不會阻塞表,影響讀寫?
在一次項目升級之前需要執行一個新增字段的腳本(alter table...),表的數據量是260多萬,執行時間是72秒,感覺略長,不知道會不會影響項目對數據庫的操作,故連百度帶問一番,得出一些結果。
結論是:執行alter table...會影響讀寫
2.MySQL官方文檔alter table的解釋
ALTER TABLE
operations that use theCOPY
algorithm wait for other operations that are modifying the table to complete. After alterations are applied to the table copy, data is copied over, the original table is deleted, and the table copy is renamed to the name of the original table. While theALTER TABLE
operation executes, the original table is readable by other sessions(讀,不阻塞) (with the exception noted shortly). Updates and writes to the table started after theALTER TABLE
operation begins are stalled until the new table is ready(更新|寫入阻塞), then are automatically redirected to the new table. The temporary copy of the table is created in the database directory of the original table unless it is aRENAME TO
operation that moves the table to a database that resides in a different directory.The exception referred to earlier is that
ALTER TABLE
blocks reads (not just writes)(讀寫都會阻塞,當數據庫操作.frm文件時) at the point where it is ready to install a new version of the table.frm
file, discard the old file, and clear outdated table structures from the table and table definition caches. At this point, it must acquire an exclusive lock. To do so, it waits for current readers to finish, and blocks new reads and writes.
3.總結
肯定會影響寫入的,讀取會有一段時間的影響,最后決定在升級項目,停服時執行alter table...,這樣不會影響寫入;
還是要看官方文檔,別自己猜。