• MySQL Transaction--事务相关查询


    MySQL支持的四种事务隔离级别

    READ-UNCOMMITTED
    READ-COMMITTED
    REPEATABLE-READ
    SERIALIZABLE

    查看全局事务隔离级别和会话事务隔离级别

    SHOW GLOBAL VARIABLES LIKE 'tx_isolation';
    SHOW SESSION VARIABLES LIKE 'tx_isolation';
    SELECT @@GLOBAL.tx_isolation, @@SESSION.tx_isolation; 

    修改事务隔离级别参数

    SET GLOBAL tx_isolation='REPEATABLE-READ'; 
    SET GLOBAL transaction_isolation='REPEATABLE-READ';
    SET SESSION tx_isolation='SERIALIZABLE';
    SET GLOBAL transaction_isolation='REPEATABLE-READ';

    tx_isolation和transaction_isolation两者等价相同,修改任意一个即可。

    修改已开启事务的隔离级别

    在事务中修改事务隔离级别,需要使用使用SET TRANSACTION ISOLATION LEVEL 命令来修改事务隔离级别。

    语法格式为:

    SET [SESSION | GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED | READ COMMITTED | REPEATABLE READ | SERIALIZABLE}

    主要修改时必须使用SESSION或GLOBAL进行修饰,SET TRANSACTION without GLOBAL or SESSION is not permitted while there is an active transaction,否者会报“ERROR 1568 (25001): Transaction characteristics can't be changed while a transaction is in progress”

    修改Demo

    START TRANSACTION;
    SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE;

    查看事务锁持有的锁

    SELECT  
    r.trx_id waiting_trx_id ,
    r.trx_query waiting_query ,
    b.trx_id blocking_trx_id ,
    b.trx_query blocking_query ,
    b.trx_mysql_thread_id blocking_thread ,
    b.trx_started ,
    b.trx_wait_started
    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 
    p.ID,
    P.USER,
    P.HOST,
    p.DB,
    P.TIME,
    T.trx_started,
    T.trx_isolation_level,
    T.trx_tables_locked,
    T.trx_rows_locked,
    t.trx_state,
    p.COMMAND AS process_state
    FROM `information_schema`.`INNODB_TRX` t
    INNER JOIN `information_schema`.`PROCESSLIST` p
    ON t.trx_mysql_thread_id=p.id;

    查看未提交事务

    ## 查看未提交的事务 ##
    SELECT 
    p.ID,
    P.USER,
    P.HOST,
    p.DB,
    P.TIME,
    T.trx_started,
    T.trx_isolation_level,
    T.trx_tables_locked,
    T.trx_rows_locked,
    t.trx_state,
    p.COMMAND AS process_state
    FROM `information_schema`.`INNODB_TRX` t
    INNER JOIN `information_schema`.`PROCESSLIST` p
    ON t.trx_mysql_thread_id=p.id
    WHERE t.trx_state='RUNNING'
    AND p.COMMAND='Sleep' 
    ORDER BY T.trx_started ASC G

    查看阻塞事务

    ##查看阻塞事务##
    SELECT
    P2.`ID` BlockProcessID,
    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.`ID` BlockingProcessID,  
    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 ;
  • 相关阅读:
    2016中国大学生程序设计竞赛
    HDU 1671 Phone List (Trie·数组实现)
    Codeforces Round #367 (Div. 2) Hard problem
    UVA 133 The Dole Queue
    SG函数模板
    Codeforces Round #366 (Div. 2) C Thor(模拟+2种stl)
    [Offer收割]编程练习赛4 A 满减优惠
    CF #365 (Div. 2) D
    Codeforces Round #365 (Div. 2) Chris and Road
    codeblocks AStyle修改格式和快捷键
  • 原文地址:https://www.cnblogs.com/gaogao67/p/10643084.html
Copyright © 2020-2023  润新知