zoukankan      html  css  js  c++  java
  • 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;

     


     

  • 相关阅读:
    第二次冲刺——第5天
    第二次冲刺——第4天
    第二次冲刺——第3天
    第二次冲刺——第2天
    第二次冲刺——第1天
    团队开发——第一次冲刺第8天
    团队开发——第一次冲刺第7天
    团队开发——第一次冲刺第1天
    软件工程课堂练习——找出1-n中1出现的个数
    软件工程——《你的灯亮着吗》读书笔记2
  • 原文地址:https://www.cnblogs.com/moss_tan_jun/p/7357890.html
Copyright © 2011-2022 走看看