MySql檢測阻塞,鎖等待sql


------------

1分鍾內產生binlog大小計算


select @a1:=VARIABLE_VALUE as a1
from information_schema.GLOBAL_STATUS
where VARIABLE_NAME='innodb_os_log_written'
union all
select sleep(60)
union all
select @a2:=VARIABLE_VALUE as a2
from information_schema.GLOBAL_STATUS
where VARIABLE_NAME='innodb_os_log_written';

select round((@a2-@a1)/1024/1024/@@innodb_log_files_in_group) as MB;

---------------------



SELECT      
        p2.`HOST` Blockedhost,  
p2.`USER` BlockedUser,  
r.trx_id BlockedTrxId,      
        r.trx_mysql_thread_id BlockedThreadId,      
        TIMESTAMPDIFF(      
            SECOND,      
            r.trx_wait_started,      
            CURRENT_TIMESTAMP      
        ) WaitTime,      
        r.trx_query BlockedQuery,      
        l.lock_table BlockedTable,    
        m.`lock_mode` BlockedLockMode,  
        m.`lock_type` BlockedLockType,  
        m.`lock_index` BlockedLockIndex,  
        m.`lock_space` BlockedLockSpace,  
        m.lock_page BlockedLockPage,  
        m.lock_rec BlockedLockRec,  
        m.lock_data BlockedLockData,   
        p.`HOST` blocking_host,   
        p.`USER` blocking_user,  
        b.trx_id BlockingTrxid,      
        b.trx_mysql_thread_id BlockingThreadId,  
        b.trx_query BlockingQuery,  
        l.`lock_mode` BlockingLockMode,  
        l.`lock_type` BlockingLockType,  
        l.`lock_index` BlockingLockIndex,  
        l.`lock_space` BlockingLockSpace,  
        l.lock_page BlockingLockPage,  
        l.lock_rec BlockingLockRec,  
        l.lock_data BlockingLockData,           
       IF (p.COMMAND = 'Sleep', CONCAT(p.TIME,' seconds'), 0) idel_in_trx               
    FROM      
        information_schema.INNODB_LOCK_WAITS w      
    INNER JOIN information_schema.INNODB_TRX b ON b.trx_id = w.blocking_trx_id      
    INNER JOIN information_schema.INNODB_TRX r ON r.trx_id = w.requesting_trx_id      
    INNER JOIN information_schema.INNODB_LOCKS l ON w.blocking_lock_id = l.lock_id  AND l.`lock_trx_id`=b.`trx_id`  
      INNER JOIN information_schema.INNODB_LOCKS m ON m.`lock_id`=w.`requested_lock_id` AND m.`lock_trx_id`=r.`trx_id`  
    INNER JOIN information_schema. PROCESSLIST p ON p.ID = b.trx_mysql_thread_id     
 INNER JOIN information_schema. PROCESSLIST p2 ON p2.ID = r.trx_mysql_thread_id   
    ORDER BY      
        WaitTime DESC;

--------------



SELECT      
        p2.`HOST` 被阻塞方host,  
p2.`USER` 被阻塞方用戶,  
r.trx_id 被阻塞方事務id,      
        r.trx_mysql_thread_id 被阻塞方線程號,      
        TIMESTAMPDIFF(      
            SECOND,      
            r.trx_wait_started,      
            CURRENT_TIMESTAMP      
        ) 等待時間,      
        r.trx_query 被阻塞的查詢,      
        l.lock_table 阻塞方鎖住的表,    
        m.`lock_mode` 被阻塞方的鎖模式,  
        m.`lock_type`  "被阻塞方的鎖類型(表鎖還是行鎖)",  
        m.`lock_index` 被阻塞方鎖住的索引,  
        m.`lock_space` 被阻塞方鎖對象的space_id,  
        m.lock_page 被阻塞方事務鎖定頁的數量,  
        m.lock_rec 被阻塞方事務鎖定行的數量,  
        m.lock_data  被阻塞方事務鎖定記錄的主鍵值,    
        p.`HOST` 阻塞方主機,  
        p.`USER` 阻塞方用戶,  
        b.trx_id 阻塞方事務id,      
        b.trx_mysql_thread_id 阻塞方線程號,   
        b.trx_query 阻塞方查詢,   
        l.`lock_mode` 阻塞方的鎖模式,  
        l.`lock_type` "阻塞方的鎖類型(表鎖還是行鎖)",  
        l.`lock_index` 阻塞方鎖住的索引,  
        l.`lock_space` 阻塞方鎖對象的space_id,  
        l.lock_page 阻塞方事務鎖定頁的數量,  
        l.lock_rec 阻塞方事務鎖定行的數量,  
        l.lock_data 阻塞方事務鎖定記錄的主鍵值,           
      IF (p.COMMAND = 'Sleep', CONCAT(p.TIME,' 秒'), 0) 阻塞方事務空閑的時間             
    FROM      
        information_schema.INNODB_LOCK_WAITS w      
    INNER JOIN information_schema.INNODB_TRX b ON b.trx_id = w.blocking_trx_id      
    INNER JOIN information_schema.INNODB_TRX r ON r.trx_id = w.requesting_trx_id      
    INNER JOIN information_schema.INNODB_LOCKS l ON w.blocking_lock_id = l.lock_id  AND l.`lock_trx_id`=b.`trx_id`  
      INNER JOIN information_schema.INNODB_LOCKS m ON m.`lock_id`=w.`requested_lock_id` AND m.`lock_trx_id`=r.`trx_id`  
    INNER JOIN information_schema. PROCESSLIST p ON p.ID = b.trx_mysql_thread_id     
 INNER JOIN information_schema. PROCESSLIST p2 ON p2.ID = r.trx_mysql_thread_id   
    ORDER BY      
        等待時間 DESC;

-------------------------------------------------------------------

# 查找有碎片的表:
select table_schema db, table_name, data_free, engine    
from information_schema.tables
where table_schema not in ('information_schema', 'mysql')  and data_free > 0;


SELECT TABLE_NAME,(DATA_LENGTH+INDEX_LENGTH)/1024/1024 size_mb,data_free/1024/1024 free_mb,TABLE_ROWS
FROM information_schema.tables where table_schema not in ('information_schema', 'mysql')
 and data_free/1024/1024>=1 order by free_mb desc;
---------------------------------------------------------------------------
#找出沒有主鍵的表
Select t.table_schema,t.table_name,t.engine,t.table_rows
From information_schema.tables as t
 Left join information_schema.table_constraints as tc
 On tc.table_schema=t.table_schema
 And tc.table_name=t.table_name
 And tc.constraint_type='PRIMARY KEY'
 Where t.table_type='BASE TABLE'
 And tc.constraint_type is null
 And t.table_schema  not in ('mysql','performance_schema','information_schema');

 

------------------------------

ySQL中一些查看事務和鎖情況的常用語句

一些查看數據庫中事務和鎖情況的常用語句

查看事務等待狀況:

select
r.trx_id waiting_trx_id,
r.trx_mysql_thread_id waiting_thread,
r.trx_query waiting_query,
b.trx_id blocking_trx_id,
b.trx_mysql_thread_id blocking_thread,
b.trx_query blocking_query
from
information_schema.innodb_lock_waits w
inner join information_schema.innodb_trx b on b.trx_id = w.blocking_trx_id
inner join information_schema.innodb_trx r on r.trx_id = w.requesting_trx_id;

查看更具體的事務等待狀況:

select
b.trx_state,
e.state,
e.time,
d.state as block_state,
d.time as block_time,
a.requesting_trx_id,
a.requested_lock_id,
b.trx_query,
b.trx_mysql_thread_id,
a.blocking_trx_id,
a.blocking_lock_id,
c.trx_query as block_trx_query,
c.trx_mysql_thread_id as block_trx_mysql_tread_id
from
information_schema.innodb_lock_waits a
left join information_schema.innodb_trx b on a.requesting_trx_id = b.trx_id
left join information_schema.innodb_trx c on a.blocking_trx_id = c.trx_id
left join information_schema.processlist d on c.trx_mysql_thread_id = d.id
left join information_schema.processlist e on b.trx_mysql_thread_id = e.id
order by
a.requesting_trx_id;

查看未關閉的事務:
–mysql 5.6

select
a.trx_id,
a.trx_state,
a.trx_started,
a.trx_query,
b.id,
b.user,
b.db,
b.command,
b.time,
b.state,
b.info,
c.processlist_user,
c.processlist_host,
c.processlist_db,
d.sql_text
from
information_schema.innodb_trx a
left join information_schema.processlist b on a.trx_mysql_thread_id = b.id
and b.command = 'sleep'
left join performance_schema.threads c on b.id = c.processlist_id
left join performance_schema.events_statements_current d on d.thread_id = c.thread_id;

–mysql 5.5

select
a.trx_id,
a.trx_state,
a.trx_started,
a.trx_query,
b.id,
b. user,
b. host,
b.db,
b.command,
b.time,
b.state,
b.info
from
information_schema.innodb_trx a
left join information_schema.processlist b on a.trx_mysql_thread_id = b.id
where
b.command = 'sleep';

 

查看某段時間以來未關閉事務:

select
trx_id,
trx_started,
trx_mysql_thread_id
from
information_schema.innodb_trx
where
trx_started < date_sub(now(), interval 1 minute)
and trx_operation_state is null
and trx_query is null;

 


 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM