mysql優化 ddl語句

2021-06-22 12:20:03 字數 1661 閱讀 5069

mysql優化-----ddl語句

mysql優化-----ddl語句

在drop table維護mysql資料庫時,在drop操作期間,整個系統會被hang住,這個hang的時間的長短與buffer pool的大小相關。主要原因在於innodb在drop table時,會連續兩次遍歷buf pool lru 鍊錶,遍歷的過程加鎖,因此導致系統hang住。

第一遍用於釋放adaptive hash中的記錄:

0. 獲取當前buf pool的mutex,為遍歷buf pool lru list的準備

1. 從lru鍊錶尾部開始遍歷

2. 對於每乙個屬於drop table的page,判斷page中是否有項進入adaptive hash,若有,則收集當前page

3. 收集到1024個這樣的page後,釋放buf pool mutex,集中呼叫函式buf_lru_drop_page_hash_batch,釋放page在adaptive hash中的項

4. 重新獲取buf pool mutex,繼續遍歷buf pool lru 鍊錶,直至煉表頭

5. 最終,釋放buf pool mutex,退出

第二次遍歷buf pool lru鍊錶,釋放所有drop table對應的page

1. 獲取buf pool mutex

2. 遍歷buffer pool lru鍊錶

3. 若為dirty page,則將dirty page設定為clean page,並從flush list中移除

4. 將page從lru list中移除,並且新增入free list

5. 最後,釋放buf pool mutex

測試:環境:

os:redhat5.2

mysql:5.5.24-log

session1:

mysql> insert into tt2 select @a:=@a+1,name from tt2;

query ok, 4194304 rows affected (1 min 11.52 sec)

records: 4194304  duplicates: 0  warnings: 0

開始drop tt2,同時在session2中insert一條記錄

mysql> drop table tt2;

query ok, 0 rows affected (6.42 sec)

mysql>

session2:

正常insert一條記錄需要0.03s

mysql> insert into t2 values (11,'22');

query ok, 1 row affected (0.03 sec)

當在執行drop tt2的時候,就需要4.17s

mysql> insert into t2 values (11,'22');

query ok, 1 row affected (4.17 sec)

mysql>

總結:1.第一遍遍歷lru鍊錶,會定期釋放buf pool mutex,因此對於系統hang的影響較小;而第二遍會一直持有,對系統hang的影響較大。

2.在維護drop操作時,一定要選擇空閒時間,因為他會阻塞所有ddl和dml操作。

MySQL 基礎語句 DDL

ddl語言 邏輯庫 資料表 檢視 索引 dml語言 新增 修改 刪除 查詢 dcl語言 使用者 許可權 事務 以下是ddl命令 建立 修改 刪除資料庫 使用資料庫 use test01 建立表和字段 create table student id int unsigned primary key,n...

MySQL常用DDL語句

刪除表 如果存在則刪除 drop table if exists t user 建立表 建立表 create table t user id bigint 20 unsigned not null auto increment,user code varchar 20 not null commen...

mysql 日常DDL語句

新增刪除列 alter table table1 add column name varchar 20 comment 姓名 not null default alter table table1 add column name varchar 20 comment 姓名 not null defa...