MySQL 表上 Metadata lock 的產生和處理

1. Metadata lock wait 出現的場景mysql

建立、刪除索引sql

修改表結構數據庫

表維護操做(optimize table、repair table 等)併發

刪除表ide

獲取表上表級寫鎖 (lock table tab_name write)fetch


注:支持事務的 InnoDB 引擎表和不支持事務的 MyISAM 引擎表,都會出現 Metadata Lock Wait 等待現象。spa

        一旦出現 Metadata Lock Wait 等待現象,後續全部對該表的訪問都會阻塞在該等待上,致使鏈接堆積,業務受影響。orm



2. Metadata lock wait 的含義索引

爲了在併發環境下維護表元數據的數據一致性,在表上有活動事務(顯式或隱式)的時候,不能夠對元數據進行寫入操做。所以 MySQL引入了metadata lock ,來保護表的元數據信息。事件

所以在對錶進行上述操做時,若是表上有活動事務(未提交或回滾),請求寫入的會話會等待在 Metadata lock wait 



3. 致使 Metadata lock wait 等待的活動事務

當前有對錶的長時間查詢

顯示或者隱式開啓事務後未提交或回滾,好比查詢完成後未提交或者回滾。

表上有失敗的查詢事務

4. 解決方案

a.show processlist 查看會話有長時間未完成的查詢,使用kill 命令終止該查詢。



b.查詢 information_schema.innodb_trx 看到有長時間未完成的事務, 使用 kill 命令終止該查詢。


select concat('kill ',i.trx_mysql_thread_id,';') from information_schema.innodb_trx i,
  (select 
         id, time
     from
         information_schema.processlist
     where
         time = (select 
                 max(time)
             from
                 information_schema.processlist
             where
                 state = 'Waiting for table metadata lock'
                     and substring(info, 1, 5) in ('alter' , 'optim', 'repai', 'lock ', 'drop ', 'creat'))) p
  where timestampdiff(second, i.trx_started, now()) > p.time
  and i.trx_mysql_thread_id  not in (connection_id(),p.id);


c.若是上面兩個檢查沒有發現,或者事務過多,建議使用下面的查詢將相關庫上的會話終止


--  MySQL 5.6
select 
    concat('kill ', a.owner_thread_id, ';')
from
    information_schema.metadata_locks a
        left join
    (select 
        b.owner_thread_id
    from
        information_schema.metadata_locks b, information_schema.metadata_locks c
    where
        b.owner_thread_id = c.owner_thread_id
            and b.lock_status = 'granted'
            and c.lock_status = 'pending') d ON a.owner_thread_id = d.owner_thread_id
where
    a.lock_status = 'granted'
        and d.owner_thread_id is null;
--  MySQL 5.5
select 
    concat('kill ', p1.id, ';')
from
    information_schema.processlist p1,
    (select 
        id, time
    from
        information_schema.processlist
    where
        time = (select 
                max(time)
            from
                information_schema.processlist
            where
                state = 'Waiting for table metadata lock'
                    and substring(info, 1, 5) in ('alter' , 'optim', 'repai', 'lock ', 'drop ', 'creat', 'trunc'))) p2
where
    p1.time >= p2.time
        and p1.command in ('Sleep' , 'Query')
        and p1.id not in (connection_id() , p2.id);

5. 如何避免出現長時間 metadata lock wait 致使表上相關查詢阻塞,影響業務

在業務低峯期執行上述操做,好比建立刪除索引。

在到數據庫鏈接創建後,設置會話變量 autocommit 爲 1 或者 on,好比 set autocommit=1; 或 set autocommit=on; 。

考慮使用事件來終止長時間運行的事務,好比下面的例子中會終止執行時間超過60分鐘的事務。

create event my_long_running_trx_monitor
on schedule every 60 minute
starts '2018-08-08 11:00:00'
on completion preserve enable do
begin
  declare v_sql varchar(500);
  declare no_more_long_running_trx integer default 0; 
  declare c_tid cursor for
    select concat ('kill ',trx_mysql_thread_id,';') 
    from information_schema.innodb_trx 
    where timestampdiff(minute,trx_started,now()) >= 60;
  declare continue handler for not found
    set no_more_long_running_trx=1;
 
  open c_tid;
  repeat
    fetch c_tid into v_sql;
 set @v_sql=v_sql;
 prepare stmt from @v_sql;
 execute stmt;
 deallocate prepare stmt;
  until no_more_long_running_trx end repeat;
  close c_tid;
end;
相關文章
相關標籤/搜索