zoukankan      html  css  js  c++  java
  • 转 How to Find Out Who Is Locking a Table in MySQL

    MySQL is adding more tools to monitor its internals with every new release, but one thing it still lacks is a way to find out who is locking what, and therefore which transactions block which other ones. This is such a vital feature that I’m considering writing my own patch to the source! Still, it is possible, to a limited extent, to find out who’s locking resources. In this article I’ll explain how you can do that.

    This article is the second in a series on how to use the innotop MySQL and InnoDB monitor.

    Introduction

    Here’s the situation: you are trying to update a table and every time you issue the query, it hangs until it times out and tells you the lock wait timeout was exceeded. Someone has locked the table you’re trying to update, but you have no idea who. This can be incredibly frustrating, because this could go on indefinitely. I’ve sometimes had to put work off till another day, because the table is locked all day long.

    I’ve found only a very limited set of circumstances in which MySQL will say what’s happening with locks. These are all printed out in the text of SHOW ENGINE INNODB STATUS.

    When there was a deadlock

    The first way to see locks is when there’s been a deadlock. The status text will show transaction information on the transactions that deadlocked, which locks they held, and which they were waiting for. Here is a sample. Look at the sections titled “WAITING FOR THIS LOCK TO BE GRANTED” and “HOLDS THE LOCKS.”

    ------------------------
    LATEST DETECTED DEADLOCK
    ------------------------
    060731 20:19:58
    *** (1) TRANSACTION:
    TRANSACTION 0 93698, ACTIVE 2 sec, process no 12767, OS thread id 1141946720 starting index read
    mysql tables in use 1, locked 1
    LOCK WAIT 4 lock struct(s), heap size 1216
    MySQL thread id 3, query id 19 localhost root Updating
    update test.innodb_deadlock_maker set a = 0 where a <> 0
    *** (1) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 0 page no 131120 n bits 72 index `GEN_CLUST_INDEX` of table `test/innodb_deadlock_maker` trx id 0 93698 lock_mode X waiting
    Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
     0: len 6; hex 000000019000; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 80000000320110; asc     2  ;; 3: len 4; hex 80000000; asc     ;;
    
    *** (2) TRANSACTION:
    TRANSACTION 0 93699, ACTIVE 2 sec, process no 12767, OS thread id 1142212960 starting index read, thread declared inside InnoDB 500
    mysql tables in use 1, locked 1
    4 lock struct(s), heap size 1216
    MySQL thread id 4, query id 20 localhost root Updating
    update test.innodb_deadlock_maker set a = 1 where a <> 1
    *** (2) HOLDS THE LOCK(S):
    RECORD LOCKS space id 0 page no 131120 n bits 72 index `GEN_CLUST_INDEX` of table `test/innodb_deadlock_maker` trx id 0 93699 lock mode S
    Record lock, heap no 1 PHYSICAL RECORD: n_fields 1; compact format; info bits 0
     0: len 8; hex 73757072656d756d; asc supremum;;
    
    Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
     0: len 6; hex 000000019000; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 80000000320110; asc     2  ;; 3: len 4; hex 80000000; asc     ;;
    
    Record lock, heap no 3 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
     0: len 6; hex 000000019001; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 8000000032011f; asc     2  ;; 3: len 4; hex 80000001; asc     ;;
    
    *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 0 page no 131120 n bits 72 index `GEN_CLUST_INDEX` of table `test/innodb_deadlock_maker` trx id 0 93699 lock_mode X waiting
    Record lock, heap no 2 PHYSICAL RECORD: n_fields 4; compact format; info bits 0
     0: len 6; hex 000000019000; asc       ;; 1: len 6; hex 000000016e01; asc     n ;; 2: len 7; hex 80000000320110; asc     2  ;; 3: len 4; hex 80000000; asc     ;;
    
    *** WE ROLL BACK TRANSACTION (2)
    

    More importantly, the lines beginning “RECORD LOCKS space id 0” show which index of which table was locked. That is the real meat of the matter—that’s what you need to know.

    There’s just one problem: after there’s been a deadlock, it’s too late. You don’t want to know what held locks in the past, you want to know what holds them now. The deadlock information isn’t usually helpful in finding out what transaction is blocking something from happening.

    When a transaction is waiting for locks

    The next place you can sometimes see lock information is in the transaction section of the output. Here’s a sample:

    ---TRANSACTION 0 93789802, ACTIVE 19 sec, process no 9544, OS thread id 389120018
    MySQL thread id 23740, query id 194861248 worker1.office 192.168.0.12 robot
    ---TRANSACTION 0 93789797, ACTIVE 20 sec, process no 9537, OS thread id 389005359 starting index read
    mysql tables in use 1, locked 1
    LOCK WAIT 2 lock struct(s), heap size 320
    MySQL thread id 23733, query id 194861215 elpaso 192.168.0.31 robot Updating
    update test.test set col1 = 4
    ------- TRX HAS BEEN WAITING 20 SEC FOR THIS LOCK TO BE GRANTED:
    RECORD LOCKS space id 0 page no 299998 n bits 200 index `PRIMARY` of table `test/test` trx id 0 93789797 lock_mode X locks rec but not gap waiting
    Record lock, heap no 77 PHYSICAL RECORD: n_fields 15; compact format; info bits 0 
     0: len 4; hex 80474fd6; asc  GO ;; 1: len 6; hex 000005970680; asc       ;; 2: len 7; hex 000017c02b176c; asc     + l;; 3: len 4; hex 80000003; asc     ;; 4: len 8; hex 800000000da0c93a; asc        :;; 5: len 8; hex 800000000eb2ea7e; asc        ~;; 6: len 4; hex c771fe44; asc  q D;; 7: len 4; hex 8000003e; asc    >;; 8: len 8; hex 8000123eb9e5dfd5; asc    >    ;; 9: len 4; hex 8000003a; asc    :;; 10: len 8; hex 8000123eb9e43603; asc    >  6 ;; 11: len 4; hex 80000035; asc    5;; 12: len 8; hex 8000123eb9d6c130; asc    >   0;; 13: len 4; hex 80000033; asc    3;; 14: len 8; hex 8000123eb9c7c853; asc    >   S;;
     
    ---------------------
    ---TRANSACTION 0 93789679, ACTIVE 31082 sec, process no 9535, OS thread id 388972583 starting index read, thread declared inside InnoDB 6
    mysql tables in use 4, locked 4
    11614 lock struct(s), heap size 683328
    MySQL thread id 23731, query id 194861117 elpaso 192.168.0.31 robot
    

    Notice the first transaction has been waiting 20 seconds for a lock to be granted, and it tells you which table and index as above. The other transaction I included (there were many in this section, but I omitted most) says it has 4 tables open and 4 locked. What it doesn’t say is which ones.

    Again, there’s some information here, but not a lot. If you issue a query and it hangs and waits for a lock, knowing what lock it’s waiting for isn’t really helpful. And knowing some other transaction holds a lock isn’t always helpful either.

    It can be useful sometimes though, and that’s better than nothing. If you only see two transactions with locks, you know the one that’s not waiting for a lock is probably the one that holds them. Notice something scary in the information above? Transaction “0 93789771”, on connection 23731, has been active for… eight and a half hours! Whoa. It’s time to find out what owns that connectionand possibly kill it.

    The take-away here is, if you’re getting blocked on an InnoDB table, and you’re lucky enough to see only one other transaction with locks, it’s probably the one blocking you.

    What about table locks?

    Ah, good question. What if the table isn’t InnoDB, or what if someone locked it with LOCK TABLES, and it doesn’t show up in the output of SHOW ENGINE INNODB STATUS? As far as I know, you’re helpless. I don’t know how to get any information on who’s locking the table then. Table lock information doesn’t seem to be exposed in any fashion—only row lock information.

    In fact, if you’re in a transaction, LOCK TABLES seems to “kick you out” of the transaction. Try experimenting with START TRANSACTION and LOCK TABLES on an InnoDB table, and you’ll see what I mean. If you lock a table for writing, then try to select from it in another connection, the other connection will block. If you then issue START TRANSACTION on the first connection, the second connection will immediately unblock, and the first connection’s transaction will disappear from the InnoDB status text.

    Who wants to read all that mess?

    Who, indeed? The text I included above is a pain to read, and it’s not even representative of what you’ll really be looking at. For one thing, you might have to scan through 40 or more transactions to find the ones you care about, and then there’s all the other information in the output, some of which can be voluminous (such as deadlocks). What a hassle!

    Fortunately, there’s a tool to do that for you: innotop. This tool formats the output neatly and gives you filtering options to display only transactions with locks (or just sort them to the top of the display). Here’s how you can do that:

    Start innotop and use the “T” key to enter InnoDB Transaction mode, if it’s not already in that mode. You will see a list of transactions. Next, make the “Locks,” “Tbl Used,” and “Tbl Lck” columns visible. Press the “c” key to activate the “choose columns” dialog.

    Now sort transactions with locks to the top by pressing the “s” key and choosing lock_structs as the sort column. You may need to press the “r” key afterwards to reverse the sort order if they go to the bottom instead. Alternatively, you can use the “w” key to add a filter on the lock_structscolumn, such as “[1-9]” to match only rows where the column isn’t zero (this is a handy filter to add in general, just so you can see how many transactions have locks).

    When you have a very busy server it can really help to hide all the transactions without locks.

    Isn’t that easier than digging through the output of SHOW ENGINE INNODB STATUS? I think so.

    Is there more?

    Though I’ve searched the Internet, searched the source code and the MySQL manual, I haven’t been able to find any other ways to get information on current locks in MySQL. But I’d be delighted if you prove me wrong! If you have anything to add, please comment.

    想看那些乱七八糟的东西?

    真的是谁?我上面的文章读起来很痛苦,甚至不能代表你真正要看的东西。首先,您可能需要扫描40个或更多事务,以找到您关心的事务,然后在输出中包含所有其他信息,其中一些信息可能非常庞大(例如死锁)。真麻烦!

    幸运的是,有一个工具可以帮你做到这一点:innotop。这个工具可以灵活地格式化输出,并为您提供筛选选项,以便仅显示带有锁的事务(或者将它们排序到显示的顶部)。你可以这样做:

    启动innotop并使用“t”键进入innodb事务模式(如果它还没有进入该模式)。您将看到一个事务列表。接下来,使“locks”、“tbl used”和“tbl lck”列可见。按“C”键激活“选择列”对话框。

    现在,通过按“s”键并选择lock_structs作为排序列,将带有锁的事务排序到顶部。如果它们转到底部,则可能需要在之后按“R”键来反转排序顺序。或者,您可以使用“w”键在lock-structscolumn上添加一个过滤器,例如“[1-9]”来只匹配列不为零的行(这是一个方便的过滤器,通常可以添加,这样您就可以看到有多少事务具有锁)。

    当您有一个非常繁忙的服务器,它可以真正帮助隐藏所有的事务没有锁。

    这难道不比挖掘show engine innodb status的输出更容易吗?我认为是这样。

    还有吗?

    尽管我搜索过互联网,搜索过源代码和mysql手册,但我还没有找到任何其他方法来获取mysql中当前锁的信息。但如果你证明我错了我会很高兴的!如果你有什么要补充的,请评论。

    ################# sample 0

     http://blog.itpub.net/28916011/viewspace-2158012/ 

    --sys库的介绍

        mysql 5.6也可以有sys库(基于performance_schema的视图)。sys库是一个开源项目,在githup上早就有,是一个DBA的开源工具,后来mysql感觉好,就放在了mysql5.7上。

        下载地址:https://github.com/mysql/mysql-sys

        [tms@m-db3 ~]$ cd mysql-sys-master

        [tms@m-db3 ~]$ mysql < sys_56.sql

        这样,就可以在mysql5.6里面加入sys库了,不过mysql 5.6只有88张表,而mysql 5.7有101张,这是因为Mysql 5.7的performace_schema库里面又多了几张表。

        sys库是performance_schema的视图。

    --MySQL锁等待

        当Mysql发生锁等待情况时,可以通过如下语句来在线查看:

    mysql> select * from sys.innodb_lock_waits G;
    *************************** 1. row ***************************
                    wait_started: 2018-07-16 16:25:17  //锁等待开始的时间,16:25开始等待
                        wait_age: 00:10:08    //发现问题时已经等待了10分钟了
                   wait_age_secs: 608   //608秒,也就是等10分钟了
                    locked_table: `iws`.`busi_reconciliationgbgsinfo_inputdetails` //被锁住的表名
                    locked_index: PRIMARY  //被锁住的索引
                     locked_type: RECORD //锁的类型为行锁
                  waiting_trx_id: 13666265  //waiting transaction id,正在等待事务的id号
             waiting_trx_started: 2018-07-16 16:24:54 //这个事务是从16:24开始等待
                 waiting_trx_age: 00:10:31 //等了10分钟了
         waiting_trx_rows_locked: 1 //正在等待的这个事务锁住了1行记录
       waiting_trx_rows_modified: 0 //正在等待的这个事务修改了0行记录
                     waiting_pid: 441805 //这个等待事务的线程id是多少,通过show processlist 命令可以查到它,结果看到是一个sleep的线程,没有执行具体sql语句,见下
                   waiting_query: update busi_reconciliationgbgs ...                where id = 4510 //等待锁释放的语句
                 waiting_lock_id: 13666265:2924:21:94 //正在等待的锁id
               waiting_lock_mode: X //等待锁的类型是排它锁
                 blocking_trx_id: 13666259 //这个事务id阻塞了waiting lock
                    blocking_pid: 441803 阻塞事务的pid
                  blocking_query: NULL  //阻塞事务的sql语句
                blocking_lock_id: 13666259:2924:21:94
              blocking_lock_mode: X
            blocking_trx_started: 2018-07-16 16:24:51
                blocking_trx_age: 00:10:34
        blocking_trx_rows_locked: 1
      blocking_trx_rows_modified: 1
         sql_kill_blocking_query: KILL QUERY 441803
    sql_kill_blocking_connection: KILL 441803
    1 row in set (0.00 sec)
    ERROR: 
    No query specified
    

        上面看到输出了很多的东西,看的我都蒙圈了。后来查看mysql官方文档,慢慢的才发现,其实只关注上面的waiting_pid、waiting_query和blocking_pid、blocking_query四个参数即可;其中waiting_pid和blocking_pid两个参数就是通过执行show processlist命令里面输出的线程id号,如下:

    mysql> show full processlist G;
    *************************** 8. row ***************************
         Id: 441803
       User: iws
       Host: 172.16.21.7:46121
         db: iws
    Command: Sleep
       Time: 655
      State: 
       Info: NULL
    *************************** 9. row ***************************
         Id: 441805
       User: iws
       Host: 172.16.21.7:46122
         db: iws
    Command: Query
       Time: 652
      State: updating
       Info: update busi_reconciliationgbgsinfo_inputdetails     set                bgs_id = 1622              ,         date = '2018-06-24 00:00:00'              ,         awbnumber = '006-85516771'              ,         incidental = 15.00              ,         entry_exit = 23.00              ,         warehousing_fee = 0.00              ,         loading_unloading = 0.00              ,         other = 0.00              ,         total = 38.00                     ,         state = 20              ,         comparison_resultsid = 30              ,         confirmation_method = '人工'              ,         confirmationid = 'root'              ,         confirmationtime = '2018-07-16 16:25:17'              ,         confirmation_note = '.'              ,         createtime = '2018-06-24 20:00:07'                     ,         createrid = '9862ebdbaf3249a88bcaa8f01bde0471'                        where id = 4510
    

        通过上面两个的输出结果,我们明白了,是441803线程锁住了表,造成线程441805的等待。 

        我们看到发生等待的线程441805对应的sql语句是:update busi_reconciliationgbgs ... where id = 4510,但是锁表的线程441803对应的sql语句竟然是Null。这就更让人迷惑了。

        于是我默默的翻开了ysql官方文档,原来里面已经对这个null专门做了说明。

        官方文档说,要想找到这个null值对应的阻塞语句,可以通过下面几个步骤寻找:

        a)、根据锁表的processlist id 441803,运用如下sql,找到null对应的sql语句,如下:

    SELECT  SQL_TEXT FROM performance_schema.events_statements_current WHERE THREAD_ID
    in 
    (SELECT THREAD_ID FROM performance_schema.threads WHERE PROCESSLIST_ID=441803)
    

        b)、如果上面找到的sql语句,你还是不能分析出为什么他们会锁表,一直拿着锁不释放,那么你可以查看 performance_schema.events_statements_history表里面最近执行过的10条sql(假设上面查到的thread_id=28):

    SELECT EVENT_ID,CURRENT_SCHEMA, SQL_TEXT FROM performance_schema.events_statements_history WHERE THREAD_ID
    in 
    (SELECT THREAD_ID FROM performance_schema.threads WHERE PROCESSLIST_ID=441803) order by event_id
    

       其他:

        上面查询锁的sql可以只关注已下几个列,如下:

    SELECT
    wait_started,
    wait_age,
    waiting_pid,
    waiting_query,
    blocking_trx_id,
    blocking_pid,
    blocking_query,
    blocking_lock_mode,
    sql_kill_blocking_query
    FROM
    sys.innodb_lock_waits
    

    ~~~~~~~~~~~~分割线~~~~~~~~~~~~~~~~~

        最近我用python 2.6写了个自动杀锁的脚本,只要发现sys.innodb_lock_waits表里面有锁表的内容,就杀死相应的sql线程,并输出杀死sql的内容到当前目录下: 

    #!/usr/bin/env python
    #-*-coding:utf8-*-
    #下载rpm包安装,下载地址:https://dev.mysql.com/downloads/connector/python/,注意mysql-connector-python版本需要是1.1.17的,2.x的版本运行会有问题
    from __future__ import print_function
    import mysql.connector as mdb
    import os
    #全局变量
    username = 'root'
    password = ''
    hostname = 'localhost'
    database = 'sys'
    #配置信息
    config = {
        'user': username,
        'password': password,
        'host': hostname,
        'database': database
    }
    #定义函数,查看锁表的行数
    def Get_sys_lock():
        show_locked_num = "select count(*) from sys.innodb_lock_waits"
        cursor.execute(show_locked_num)
        for i in cursor:
            locked_sql_num = i[0]
        return locked_sql_num
        
    #定义函数,如果有锁表,就重定向到locked_sql.txt文件里面
    def show_locked_sql():
        count = 0
        count1 = 0
        #如果日志文件存在就删除
        if os.path.isfile('locked_sql.txt'):
            os.remove('locked_sql.txt')
        if os.path.isfile('null_sql.txt'):
            os.remove('null_sql.txt')
        if os.path.isfile('last_10_null_sql.txt'):
            os.remove('last_10_null_sql.txt')
        #引用函数
        locked_sql_num = Get_sys_lock()
        print("锁表的行数是:{0}".format(locked_sql_num))
        if locked_sql_num > 0: #如果有锁表
            show_locked_sql = " SELECT 
                wait_started, 
                wait_age, 
                waiting_pid, 
                waiting_query, 
                blocking_trx_id, 
                blocking_pid, 
                blocking_query, 
                blocking_lock_mode, 
                sql_kill_blocking_query 
                FROM 
                sys.innodb_lock_waits 
                "
            cursor.execute(show_locked_sql)
            for i in cursor:
                wait_started = i[0]
                wait_age = i[1]
                waiting_pid = i[2]
                waiting_query = i[3]
                blocking_trx_id = i[4]
                blocking_pid = i[5]
                blocking_query = i[6]
                blocking_lock_mode = i[7]
                sql_kill_blocking_query = i[8]
                if not str(blocking_query).strip(): #如果blocking_query字符串为Null
                    #import pdb;pdb.set_trace()
                    show_null_sql = "SELECT  SQL_TEXT FROM performance_schema.events_statements_current WHERE THREAD_ID in (SELECT THREAD_ID FROM performance_schema.threads WHERE PROCESSLIST_ID=%s)" % blocking_pid
                    conn = mdb.connect(**config)
                    cursor1 = conn.cursor()
                    cursor1.execute(show_null_sql)
                    #print(cursor1.fetchall())
                    for j in cursor1:
                        SQL_TEXT = j[0]
                        print(SQL_TEXT)
                    cursor1.close
                    try:
                        count1 += 1
                        f = open('null_sql.txt','a') #a表示追加
                        f.write (
                            '##########' + 'The ' + str(count1) + ' rows ' + 'Blocking null query对应的具体sql为##########
    ' +
                            'blocking_pid: ' + str(blocking_pid) + '
    '
                            'sql_text: ' + str(SQL_TEXT) + '
    
    '
                        )
                    except OSError as reason:
                        print('出错了:' + str(reason))
                    finally:
                        f.close
                    #再查看null对应的最后10条sql
                    show_last_10_null_sql = "SELECT EVENT_ID,CURRENT_SCHEMA, SQL_TEXT FROM performance_schema.events_statements_history WHERE THREAD_ID in (SELECT THREAD_ID FROM performance_schema.threads WHERE PROCESSLIST_ID=%s) order by event_id" % blocking_pid
                    cursor2 = conn.cursor()
                    cursor2.execute(show_last_10_null_sql)
                    cursor2.close
                    #print(cursor1.fetchall())
                    count2 = 0
                    for j in cursor2:
                        EVENT_ID = j[0]
                        CURRENT_SCHEMA = j[1]
                        SQL_TEXT = j[2]
                        try:
                            count2 += 1
                            f = open('last_10_null_sql.txt','a') #a表示追加
                            f.write (
                                '##########' + 'The ' + str(count2) + ' rows ' + 'laster blocking null query对应的具体sql为##########
    ' +
                                'blocking_pid: ' + str(blocking_pid) + '
    '
                                'EVENT_ID: ' + str(EVENT_ID) + '
    '
                                'CURRENT_SCHEMA: ' + str(CURRENT_SCHEMA) + '
    '
                                'SQL_TEXT: ' + str(SQL_TEXT) + '
    
    '
                            )   
                        except OSError as reason:
                            print('出错了:' + str(reason))
                        finally:
                            f.close
                #把锁表的情况重定向到一个locked_sql.txt文件里面
                try:
                    count += 1
                    f = open('locked_sql.txt','a') #a表示追加
                    f.write('##########' + 'The ' + str(count) + ' rows' + '###########
    ')
                    f.write (
                        'wait_started: ' + str(wait_started) + '
    ' +
                        'wait_age: ' + str(wait_age) + '
    ' + 
                        'waiting_pid: ' + str(waiting_pid )  + '
    ' +
                        'waiting_query: ' + str(waiting_query) + '
    ' +
                        'blocking_trx_id: ' + str(blocking_trx_id) + '
    ' +
                        'blocking_pid: ' + str(blocking_pid) + '
    ' +
                        'blocking_query: ' + str(blocking_query) + '
    ' +
                        'blocking_lock_mode: ' + str(blocking_lock_mode)  + '
    ' +
                        'sql_kill_blocking_query: ' + str(sql_kill_blocking_query) + '
    
    '
                    )
                    '''
                    f.write (
                        '##########' + 'Blocking null query对应的具体sql为##########
    ' +
                        'blocking_pid:' + str(blocking_pid) +
                        'sql_text:' + str(SQL_TEXT)
                        
                    )
                    '''
                except OSError as reason:
                    print('出错了:' + str(reason))
                finally:
                    f.close
    #定义函数,列出当前所有执行的sql线程
    def show_processlist():
        count = 0
        #如果日志文件存在就删除
        if os.path.isfile('show_processlist.txt'):
            os.remove('show_processlist.txt')
        #引用函数
        locked_sql_num = Get_sys_lock()
        #print("锁表的行数是:{0}".format(locked_sql_num))
        if locked_sql_num > 0: #如果有锁表
            show_processlist = "select 
                id, 
                user, 
                host, 
                db, 
                time, 
                state, 
                info 
                from information_schema.`PROCESSLIST`  order  by time desc 
                "
            cursor.execute(show_processlist)
            for i in cursor:
                id = i[0]
                user = i[1]
                host = i[2]
                db = i[3]
                time = i[4]
                state = i[5]
                info = i[6]
                #把锁表的情况重定向到一个show_processlist.txt文件里面
                try:
                    count += 1
                    f = open('show_processlist.txt','a') #a表示追加
                    f.write('##########' + 'The ' + str(count) + ' rows' + '###########
    ')
                    f.write (
                        'id: ' + str(id) + '
    ' +
                        'user: ' + str(user) + '
    ' + 
                        'host: ' + str(host)  + '
    ' +
                        'db: ' + str(db) + '
    ' +
                        'time: ' + str(time) + '
    ' +
                        'state: ' + str(state) + '
    ' +
                        'info: ' + str(info) + '
    
    ' 
                    )
                except OSError as reason:
                    print('出错了:' + str(reason))
                finally:
                    f.close
    #定义函数,如果有锁表,就杀死
    def kill_locked_sql():
        #引用函数
        locked_sql_num = Get_sys_lock()
        #print("锁表的行数是:{0}".format(locked_sql_num))
        if locked_sql_num > 0: #如果有锁表
            execute_locked_sql = " SELECT 
                sql_kill_blocking_query 
                FROM 
                sys.innodb_lock_waits 
                "
            cursor.execute(execute_locked_sql)
            for i in cursor:
                sql_kill_blocking_query = i[0]
                conn = mdb.connect(**config)
                cursor1 = conn.cursor()
                try:
                    cursor1.execute(sql_kill_blocking_query)
                except:
                    print('出错了')
                cursor1.close
            
    #主程序
    conn = mdb.connect(**config)
    cursor = conn.cursor()
    show_locked_sql()
    show_processlist()
    kill_locked_sql()
    cursor.close
    conn.close
    

     --参考文档:

     https://dev.mysql.com/doc/refman/8.0/en/sys-innodb-lock-waits.html

     https://dev.mysql.com/doc/refman/8.0/en/innodb-information-schema-examples.html#innodb-information-schema-examples-null-blocking-query

    ### sample 1  show engine status 数据 挖掘

    https://mariadb.com/kb/en/library/show-engine-innodb-status/

    -》监控智能监控到innnodb lock table 的数目,无法查到innodb 的锁的源头。
    -》后继可以在 5.6  加入  sys 对象。使用这个对象监控。

    select waiting_pid as '被阻塞线程',
    waiting_query as '被阻塞SQL',
    blocking_pid as '阻塞线程',
    blocking_query as '阻塞SQL',
    wait_age as '阻塞时间',
    sql_kill_blocking_query as '建议操作'
    from sys.innodb_lock_waits
    where(unix_timestamp()-unix_timestamp(wait_started))>阻塞秒数

    (这个语句可以检查 行锁,但是无法检查 lock table test read; flush table 锁 )

    -》网上说innotop 可以图形化查找出来,但是还没调试出

    (可以检查 行锁,但是无法检查 lock table test read; flush table 锁 )

    -》 SHOW ENGINE INNODB STATUS 中的 TRANSACTIONS 段,可以看到 正在锁的表,和该锁的 session_id.

    (可以检查 行锁,也可以检查 lock table test read; flush table 锁 )

    SHOW ENGINE INNODB STATUS

    SHOW ENGINE INNODB STATUS is a specific form of the SHOW ENGINE statement that displays the InnoDB Monitor output, which is extensive InnoDB information which can be useful in diagnosing problems.

    The following sections are displayed

    • Status: Shows the timestamp, monitor name and the number of seconds, or the elapsed time between the current time and the time the InnoDB Monitor output was last displayed. The per-second averages are based upon this time.
    • BACKGROUND THREAD: srv_master_thread lines show work performed by the main background thread.
    • SEMAPHORES: Threads waiting for a semaphore and stats on how the number of times threads have needed a spin or a wait on a mutex or rw-lock semaphore. If this number of threads is large, there may be I/O or contention issues. Reducing the size of the innodb_thread_concurrency system variable may help if contention is related to thread scheduling. Spin rounds per wait shows the number of spinlock rounds per OS wait for a mutex.
    • LATEST FOREIGN KEY ERROR: Only shown if there has been a foreign key constraint error, it displays the failed statement and information about the constraint and the related tables.
    • LATEST DETECTED DEADLOCK: Only shown if there has been a deadlock, it displays the transactions involved in the deadlock and the statements being executed, held and required locked and the transaction rolled back to.
    • TRANSACTIONS: The output of this section can help identify lock contention, as well as reasons for the deadlocks.
    • FILE I/O: InnoDB thread information as well as pending I/O operations and I/O performance statistics.
    • INSERT BUFFER AND ADAPTIVE HASH INDEX: InnoDB insert buffer and adaptive hash index status information, including the number of each type of operation performed, and adaptive hash index performance.
    • LOG: InnoDB log information, including current log sequence number, how far the log has been flushed to disk, the position at which InnoDB last took a checkpoint, pending writes and write performance statistics.
    • BUFFER POOL AND MEMORY: Information on buffer pool pages read and written, which allows you to see the number of data file I/O operations performed by your queries. See InnoDB Buffer Pool for more. Similar information is also available from the INFORMATION_SCHEMA.INNODB_BUFFER_POOL_STATS table.
    • ROW OPERATIONS:Information about the main thread, including the number and performance rate for each type of row operation.

    If the innodb_status_output_locks system variable is set to 1, extended lock information will be displayed.

    Example output:

    =====================================
    2019-09-06 12:44:13 0x7f93cc236700 INNODB MONITOR OUTPUT
    =====================================
    Per second averages calculated from the last 4 seconds
    -----------------
    BACKGROUND THREAD
    -----------------
    srv_master_thread loops: 2 srv_active, 0 srv_shutdown, 83698 srv_idle
    srv_master_thread log flush and writes: 83682
    ----------
    SEMAPHORES
    ----------
    OS WAIT ARRAY INFO: reservation count 15
    OS WAIT ARRAY INFO: signal count 8
    RW-shared spins 0, rounds 20, OS waits 7
    RW-excl spins 0, rounds 0, OS waits 0
    RW-sx spins 0, rounds 0, OS waits 0
    Spin rounds per wait: 20.00 RW-shared, 0.00 RW-excl, 0.00 RW-sx
    ------------
    TRANSACTIONS
    ------------
    Trx id counter 236
    Purge done for trx's n:o < 236 undo n:o < 0 state: running
    History list length 22
    LIST OF TRANSACTIONS FOR EACH SESSION:
    ---TRANSACTION 421747401994584, not started
    0 lock struct(s), heap size 1136, 0 row lock(s)
    ---TRANSACTION 421747401990328, not started
    0 lock struct(s), heap size 1136, 0 row lock(s)
    --------
    FILE I/O
    --------
    I/O thread 0 state: waiting for completed aio requests (insert buffer thread)
    I/O thread 1 state: waiting for completed aio requests (log thread)
    I/O thread 2 state: waiting for completed aio requests (read thread)
    I/O thread 3 state: waiting for completed aio requests (read thread)
    I/O thread 4 state: waiting for completed aio requests (read thread)
    I/O thread 5 state: waiting for completed aio requests (read thread)
    I/O thread 6 state: waiting for completed aio requests (write thread)
    I/O thread 7 state: waiting for completed aio requests (write thread)
    I/O thread 8 state: waiting for completed aio requests (write thread)
    I/O thread 9 state: waiting for completed aio requests (write thread)
    Pending normal aio reads: [0, 0, 0, 0] , aio writes: [0, 0, 0, 0] ,
     ibuf aio reads:, log i/o's:, sync i/o's:
    Pending flushes (fsync) log: 0; buffer pool: 0
    286 OS file reads, 171 OS file writes, 22 OS fsyncs
    0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
    -------------------------------------
    INSERT BUFFER AND ADAPTIVE HASH INDEX
    -------------------------------------
    Ibuf: size 1, free list len 0, seg size 2, 0 merges
    merged operations:
     insert 0, delete mark 0, delete 0
    discarded operations:
     insert 0, delete mark 0, delete 0
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    Hash table size 34679, node heap has 0 buffer(s)
    0.00 hash searches/s, 0.00 non-hash searches/s
    ---
    LOG
    ---
    Log sequence number 445926
    Log flushed up to   445926
    Pages flushed up to 445926
    Last checkpoint at  445917
    0 pending log flushes, 0 pending chkp writes
    18 log i/o's done, 0.00 log i/o's/second
    ----------------------
    BUFFER POOL AND MEMORY
    ----------------------
    Total large memory allocated 167772160
    Dictionary memory allocated 50768
    Buffer pool size   8012
    Free buffers       7611
    Database pages     401
    Old database pages 0
    Modified db pages  0
    Percent of dirty pages(LRU & free pages): 0.000
    Max dirty pages percent: 75.000
    Pending reads 0
    Pending writes: LRU 0, flush list 0, single page 0
    Pages made young 0, not young 0
    0.00 youngs/s, 0.00 non-youngs/s
    Pages read 264, created 137, written 156
    0.00 reads/s, 0.00 creates/s, 0.00 writes/s
    No buffer pool page gets since the last printout
    Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
    LRU len: 401, unzip_LRU len: 0
    I/O sum[0]:cur[0], unzip sum[0]:cur[0]
    --------------
    ROW OPERATIONS
    --------------
    0 queries inside InnoDB, 0 queries in queue
    0 read views open inside InnoDB
    Process ID=4267, Main thread ID=140272021272320, state: sleeping
    Number of rows inserted 1, updated 0, deleted 0, read 1
    0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
    Number of system rows inserted 0, updated 0, deleted 0, read 0
    0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
    ----------------------------
    END OF INNODB MONITOR OUTPUT
    ============================
    

    Comments

    #######sample 2    模拟 innodb 锁 

    https://www.cnblogs.com/phpfeng/p/9374002.html

    当选择存储引擎为innodb时,默认会为每一条insertupdatedelete语句启动事务,根据mysql的autocommit参数设置来决定是否自动提交,每次执行修改语句时,不会直接修改物理表,会将结果写入缓冲区,直到触发commit才会真正的修改表数据.

    autocommit参数都是开启状态[1],条件说明:

    1.autocommit=0,不会自动提交,需要手动commit;

    2.autocommit=1,每次执行修改语句会自动执行commit,但是在transcation流程控制中不会触发。

    命令:

    select @@autocommit;   --查看autocommit配置

    set autocommit=1;     --设置参数[0,1]

    InnoDB的行锁模式及加锁方法

    InnoDB实现了以下两种类型的行锁。
    l  共享锁(S):允许一个事务去读一行,阻止其他事务获得相同数据集的排他锁。
    l  排他锁(X):允许获得排他锁的事务更新数据,阻止其他事务取得相同数据集的共享读锁和排他写锁。
    另外,为了允许行锁和表锁共存,实现多粒度锁机制,InnoDB还有两种内部使用的意向锁(Intention Locks),这两种意向锁都是表锁。
    l  意向共享锁(IS):事务打算给数据行加行共享锁,事务在给一个数据行加共享锁前必须先取得该表的IS锁。
    l  意向排他锁(IX):事务打算给数据行加行排他锁,事务在给一个数据行加排他锁前必须先取得该表的IX锁。
    上述锁模式的兼容情况具体如表20-6所示。
    表20-6                                            InnoDB行锁模式兼容性列表
    请求锁模式
       是否兼容
    当前锁模式
    X
    IX
    S
    IS
    X
    冲突
    冲突
    冲突
    冲突
    IX
    冲突
    兼容
    冲突
    兼容
    S
    冲突
    冲突
    兼容
    兼容
    IS
    冲突
    兼容
    兼容
    兼容
    如果一个事务请求的锁模式与当前的锁兼容,InnoDB就将请求的锁授予该事务;反之,如果两者不兼容,该事务就要等待锁释放。
    意向锁是InnoDB自动加的,不需用户干预。对于UPDATE、DELETE和INSERT语句,InnoDB会自动给涉及数据集加排他锁(X);对于普通SELECT语句,InnoDB不会加任何锁;事务可以通过以下语句显示给记录集加共享锁或排他锁。
    ¡  共享锁(S):SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE。
    ¡  排他锁(X):SELECT * FROM table_name WHERE ... FOR UPDATE。
    用SELECT ... IN SHARE MODE获得共享锁,主要用在需要数据依存关系时来确认某行记录是否存在,并确保没有人对这个记录进行UPDATE或者DELETE操作。但是如果当前事务也需要对该记录进行更新操作,则很有可能造成死锁,对于锁定行记录后需要进行更新操作的应用,应该使用SELECT... FOR UPDATE方式获得排他锁。
    在如表20-7所示的例子中,使用了SELECT ... IN SHARE MODE加锁后再更新记录,看看会出现什么情况,其中actor表的actor_id字段为主键。
    表20-7  InnoDB存储引擎的共享锁例子
    session_1
    session_2
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.00 sec)
    当前session对actor_id=178的记录加share mode 的共享锁:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178lock in share mode;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.01 sec)
     
     
    其他session仍然可以查询记录,并也可以对该记录加share mode的共享锁:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178lock in share mode;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.01 sec)
    当前session对锁定的记录进行更新操作,等待锁:
    mysql> update actor set last_name = 'MONROE T' where actor_id = 178;
    等待
     
     
    其他session也对该记录进行更新操作,则会导致死锁退出:
    mysql> update actor set last_name = 'MONROE T' where actor_id = 178;
    ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
    获得锁后,可以成功更新:
    mysql> update actor set last_name = 'MONROE T' where actor_id = 178;
    Query OK, 1 row affected (17.67 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
     
        当使用SELECT...FOR UPDATE加锁后再更新记录,出现如表20-8所示的情况。
    表20-8 InnoDB存储引擎的排他锁例子
    session_1
    session_2
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.00 sec)
    当前session对actor_id=178的记录加for update的排它锁:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178 for update;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.00 sec)
     
     
    其他session可以查询该记录,但是不能对该记录加共享锁,会等待获得锁:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE    |
    +----------+------------+-----------+
    1 row in set (0.00 sec)
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178 for update;
    等待
    当前session可以对锁定的记录进行更新操作,更新后释放锁:
    mysql> update actor set last_name = 'MONROE T' where actor_id = 178;
    Query OK, 1 row affected (0.00 sec)
    Rows matched: 1  Changed: 1  Warnings: 0
    mysql> commit;
    Query OK, 0 rows affected (0.01 sec)
     
     
    其他session获得锁,得到其他session提交的记录:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 178 for update;
    +----------+------------+-----------+
    | actor_id | first_name | last_name |
    +----------+------------+-----------+
    | 178      | LISA       | MONROE T  |
    +----------+------------+-----------+
    1 row in set (9.59 sec)

    InnoDB行锁实现方式

    InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的。InnoDB这种行锁实现特点意味着:只有通过索引条件检索数据,InnoDB才使用行级锁,否则,InnoDB将使用表锁!
    在实际应用中,要特别注意InnoDB行锁的这一特性,不然的话,可能导致大量的锁冲突,从而影响并发性能。下面通过一些实际例子来加以说明。
    (1)在不通过索引条件查询的时候,InnoDB确实使用的是表锁,而不是行锁。
    在如表20-9所示的例子中,开始tab_no_index表没有索引:
    mysql> create table tab_no_index(id int,name varchar(10)) engine=innodb;
    Query OK, 0 rows affected (0.15 sec)
    mysql> insert into tab_no_index values(1,'1'),(2,'2'),(3,'3'),(4,'4');
    Query OK, 4 rows affected (0.00 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    表20-9   InnoDB存储引擎的表在不使用索引时使用表锁例子
    session_1
    session_2
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from tab_no_index where id = 1 ;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    +------+------+
    1 row in set (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from tab_no_index where id = 2 ;
    +------+------+
    | id   | name |
    +------+------+
    | 2    | 2    |
    +------+------+
    1 row in set (0.00 sec)
    mysql> select * from tab_no_index where id = 1 for update;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    +------+------+
    1 row in set (0.00 sec)
     
     
    mysql> select * from tab_no_index where id = 2 for update;
    等待
    在如表20 -9所示的例子中,看起来session_1只给一行加了排他锁,但session_2在请求其他行的排他锁时,却出现了锁等待!原因就是在没有索引的情况下,InnoDB只能使用表锁。当我们给其增加一个索引后,InnoDB就只锁定了符合条件的行,如表20-10所示。
    创建tab_with_index表,id字段有普通索引:
    mysql> create table tab_with_index(id int,name varchar(10)) engine=innodb;
    Query OK, 0 rows affected (0.15 sec)
    mysql> alter table tab_with_index add index id(id);
    Query OK, 4 rows affected (0.24 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    表20-10   InnoDB存储引擎的表在使用索引时使用行锁例子
    session_1
    session_2
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from tab_with_index where id = 1 ;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    +------+------+
    1 row in set (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from tab_with_index where id = 2 ;
    +------+------+
    | id   | name |
    +------+------+
    | 2    | 2    |
    +------+------+
    1 row in set (0.00 sec)
    mysql> select * from tab_with_index where id = 1 for update;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    +------+------+
    1 row in set (0.00 sec)
     
     
    mysql> select * from tab_with_index where id = 2 for update;
    +------+------+
    | id   | name |
    +------+------+
    | 2    | 2    |
    +------+------+
    1 row in set (0.00 sec)
    (2)由于MySQL的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然是访问不同行的记录,但是如果是使用相同的索引键,是会出现锁冲突的。应用设计的时候要注意这一点。
    在如表20-11所示的例子中,表tab_with_index的id字段有索引,name字段没有索引:
    mysql> alter table tab_with_index drop index name;
    Query OK, 4 rows affected (0.22 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    mysql> insert into tab_with_index  values(1,'4');
    Query OK, 1 row affected (0.00 sec)
    mysql> select * from tab_with_index where id = 1;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    | 1    | 4    |
    +------+------+
    2 rows in set (0.00 sec)
    表20-11 InnoDB存储引擎使用相同索引键的阻塞例子       
    session_1
    session_2
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from tab_with_index where id = 1 and name = '1' for update;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    +------+------+
    1 row in set (0.00 sec)
     
     
    虽然session_2访问的是和session_1不同的记录,但是因为使用了相同的索引,所以需要等待锁:
    mysql> select * from tab_with_index where id = 1 and name = '4' for update;
    等待
    (3)当表有多个索引的时候,不同的事务可以使用不同的索引锁定不同的行,另外,不论是使用主键索引、唯一索引或普通索引,InnoDB都会使用行锁来对数据加锁。
    在如表20-12所示的例子中,表tab_with_index的id字段有主键索引,name字段有普通索引:
    mysql> alter table tab_with_index add index name(name);
    Query OK, 5 rows affected (0.23 sec)
    Records: 5  Duplicates: 0  Warnings: 0
    表20-12  InnoDB存储引擎的表使用不同索引的阻塞例子
    session_1
    session_2
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from tab_with_index where id = 1 for update;
    +------+------+
    | id   | name |
    +------+------+
    | 1    | 1    |
    | 1    | 4    |
    +------+------+
    2 rows in set (0.00 sec)
     
     
    Session_2使用name的索引访问记录,因为记录没有被索引,所以可以获得锁:
    mysql> select * from tab_with_index where name = '2' for update;
    +------+------+
    | id   | name |
    +------+------+
    | 2    | 2    |
    +------+------+
    1 row in set (0.00 sec)
     
    由于访问的记录已经被session_1锁定,所以等待获得锁。:
    mysql> select * from tab_with_index where name = '4' for update;
    (4)即便在条件中使用了索引字段,但是否使用索引来检索数据是由MySQL通过判断不同执行计划的代价来决定的,如果MySQL认为全表扫描效率更高,比如对一些很小的表,它就不会使用索引,这种情况下InnoDB将使用表锁,而不是行锁。因此,在分析锁冲突时,别忘了检查SQL的执行计划,以确认是否真正使用了索引。关于MySQL在什么情况下不使用索引的详细讨论,参见本章“索引问题”一节的介绍。
    在下面的例子中,检索值的数据类型与索引字段不同,虽然MySQL能够进行数据类型转换,但却不会使用索引,从而导致InnoDB使用表锁。通过用explain检查两条SQL的执行计划,我们可以清楚地看到了这一点。
    例子中tab_with_index表的name字段有索引,但是name字段是varchar类型的,如果where条件中不是和varchar类型进行比较,则会对name进行类型转换,而执行的全表扫描。
    mysql> alter table tab_no_index add index name(name);
    Query OK, 4 rows affected (8.06 sec)
    Records: 4  Duplicates: 0  Warnings: 0
    mysql> explain select * from tab_with_index where name = 1 G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: tab_with_index
             type: ALL
    possible_keys: name
              key: NULL
          key_len: NULL
              ref: NULL
             rows: 4
            Extra: Using where
    1 row in set (0.00 sec)
    mysql> explain select * from tab_with_index where name = '1' G
    *************************** 1. row ***************************
               id: 1
      select_type: SIMPLE
            table: tab_with_index
             type: ref
    possible_keys: name
              key: name
          key_len: 23
              ref: const
             rows: 1
            Extra: Using where
    1 row in set (0.00 sec)

    间隙锁(Next-Key锁)

    当我们用范围条件而不是相等条件检索数据,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录的索引项加锁;对于键值在条件范围内但并不存在的记录,叫做“间隙(GAP)”,InnoDB也会对这个“间隙”加锁,这种锁机制就是所谓的间隙锁(Next-Key锁)。
    举例来说,假如emp表中只有101条记录,其empid的值分别是 1,2,...,100,101,下面的SQL:
    Select * from  emp where empid > 100 for update;
    是一个范围条件的检索,InnoDB不仅会对符合条件的empid值为101的记录加锁,也会对empid大于101(这些记录并不存在)的“间隙”加锁。
    InnoDB使用间隙锁的目的,一方面是为了防止幻读,以满足相关隔离级别的要求,对于上面的例子,要是不使用间隙锁,如果其他事务插入了empid大于100的任何记录,那么本事务如果再次执行上述语句,就会发生幻读;另外一方面,是为了满足其恢复和复制的需要。有关其恢复和复制对锁机制的影响,以及不同隔离级别下InnoDB使用间隙锁的情况,在后续的章节中会做进一步介绍。
    很显然,在使用范围条件检索并锁定记录时,InnoDB这种加锁机制会阻塞符合条件范围内键值的并发插入,这往往会造成严重的锁等待。因此,在实际应用开发中,尤其是并发插入比较多的应用,我们要尽量优化业务逻辑,尽量使用相等条件来访问更新数据,避免使用范围条件。
    还要特别说明的是,InnoDB除了通过范围条件加锁时使用间隙锁外,如果使用相等条件请求给一个不存在的记录加锁,InnoDB也会使用间隙锁!
    在如表20-13所示的例子中,假如emp表中只有101条记录,其empid的值分别是1,2,......,100,101。
    表20-13                InnoDB存储引擎的间隙锁阻塞例子
    session_1
    session_2
    mysql> select @@tx_isolation;
    +-----------------+
    | @@tx_isolation  |
    +-----------------+
    | REPEATABLE-READ |
    +-----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select @@tx_isolation;
    +-----------------+
    | @@tx_isolation  |
    +-----------------+
    | REPEATABLE-READ |
    +-----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    当前session对不存在的记录加for update的锁:
    mysql> select * from emp where empid = 102 for update;
    Empty set (0.00 sec)
     
     
    这时,如果其他session插入empid为102的记录(注意:这条记录并不存在),也会出现锁等待:
    mysql>insert into emp(empid,...) values(102,...);
    阻塞等待
    Session_1 执行rollback:
    mysql> rollback;
    Query OK, 0 rows affected (13.04 sec)
     
     
    由于其他session_1回退后释放了Next-Key锁,当前session可以获得锁并成功插入记录:
    mysql>insert into emp(empid,...) values(102,...);
    Query OK, 1 row affected (13.35 sec)

    恢复和复制的需要,对InnoDB锁机制的影响

    MySQL通过BINLOG录执行成功的INSERT、UPDATE、DELETE等更新数据的SQL语句,并由此实现MySQL数据库的恢复和主从复制(可以参见本书“管理篇”的介绍)。MySQL的恢复机制(复制其实就是在Slave Mysql不断做基于BINLOG的恢复)有以下特点。
    l  一是MySQL的恢复是SQL语句级的,也就是重新执行BINLOG中的SQL语句。这与Oracle数据库不同,Oracle是基于数据库文件块的。
    l  二是MySQL的Binlog是按照事务提交的先后顺序记录的,恢复也是按这个顺序进行的。这点也与Oralce不同,Oracle是按照系统更新号(System Change Number,SCN)来恢复数据的,每个事务开始时,Oracle都会分配一个全局唯一的SCN,SCN的顺序与事务开始的时间顺序是一致的。
    从上面两点可知,MySQL的恢复机制要求:在一个事务未提交前,其他并发事务不能插入满足其锁定条件的任何记录,也就是不允许出现幻读,这已经超过了ISO/ANSI SQL92“可重复读”隔离级别的要求,实际上是要求事务要串行化。这也是许多情况下,InnoDB要用到间隙锁的原因,比如在用范围条件更新记录时,无论在Read Commited或是Repeatable Read隔离级别下,InnoDB都要使用间隙锁,但这并不是隔离级别要求的,有关InnoDB在不同隔离级别下加锁的差异在下一小节还会介绍。
    另外,对于“insert  into target_tab select * from source_tab where ...”和“create  table new_tab ...select ... From  source_tab where ...(CTAS)”这种SQL语句,用户并没有对source_tab做任何更新操作,但MySQL对这种SQL语句做了特别处理。先来看如表20-14的例子。
    表20-14                   CTAS操作给原表加锁例子
    session_1
    session_2
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from target_tab;
    Empty set (0.00 sec)
    mysql> select * from source_tab where name = '1';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 1    |  1 |
    |  5 | 1    |  1 |
    |  6 | 1    |  1 |
    |  7 | 1    |  1 |
    |  8 | 1    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from target_tab;
    Empty set (0.00 sec)
    mysql> select * from source_tab where name = '1';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 1    |  1 |
    |  5 | 1    |  1 |
    |  6 | 1    |  1 |
    |  7 | 1    |  1 |
    |  8 | 1    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
    mysql> insert into target_tab select d1,name from source_tab where name = '1';
    Query OK, 5 rows affected (0.00 sec)
    Records: 5  Duplicates: 0  Warnings: 0
     
     
    mysql> update source_tab set name = '1' where name = '8';
    等待
    commit;
     
     
    返回结果
    commit;
    在上面的例子中,只是简单地读 source_tab表的数据,相当于执行一个普通的SELECT语句,用一致性读就可以了。ORACLE正是这么做的,它通过MVCC技术实现的多版本数据来实现一致性读,不需要给source_tab加任何锁。我们知道InnoDB也实现了多版本数据,对普通的SELECT一致性读,也不需要加任何锁;但这里InnoDB却给source_tab加了共享锁,并没有使用多版本数据一致性读技术!
    MySQL为什么要这么做呢?其原因还是为了保证恢复和复制的正确性。因为不加锁的话,如果在上述语句执行过程中,其他事务对source_tab做了更新操作,就可能导致数据恢复的结果错误。为了演示这一点,我们再重复一下前面的例子,不同的是在session_1执行事务前,先将系统变量 innodb_locks_unsafe_for_binlog的值设置为“on”(其默认值为off),具体结果如表20-15所示。
    表20-15                   CTAS操作不给原表加锁带来的安全问题例子
    session_1
    session_2
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql>set innodb_locks_unsafe_for_binlog='on'
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from target_tab;
    Empty set (0.00 sec)
    mysql> select * from source_tab where name = '1';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 1    |  1 |
    |  5 | 1    |  1 |
    |  6 | 1    |  1 |
    |  7 | 1    |  1 |
    |  8 | 1    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from target_tab;
    Empty set (0.00 sec)
    mysql> select * from source_tab where name = '1';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 1    |  1 |
    |  5 | 1    |  1 |
    |  6 | 1    |  1 |
    |  7 | 1    |  1 |
    |  8 | 1    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
    mysql> insert into target_tab select d1,name from source_tab where name = '1';
    Query OK, 5 rows affected (0.00 sec)
    Records: 5  Duplicates: 0  Warnings: 0
     
     
    session_1未提交,可以对session_1的select的记录进行更新操作。
    mysql> update source_tab set name = '8' where name = '1';
    Query OK, 5 rows affected (0.00 sec)
    Rows matched: 5  Changed: 5  Warnings: 0
    mysql> select * from source_tab where name = '8';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 8    |  1 |
    |  5 | 8    |  1 |
    |  6 | 8    |  1 |
    |  7 | 8    |  1 |
    |  8 | 8    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
     
    更新操作先提交
    mysql> commit;
    Query OK, 0 rows affected (0.05 sec)
    插入操作后提交
    mysql> commit;
    Query OK, 0 rows affected (0.07 sec)
     
    此时查看数据,target_tab中可以插入source_tab更新前的结果,这符合应用逻辑:
    mysql> select * from source_tab where name = '8';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 8    |  1 |
    |  5 | 8    |  1 |
    |  6 | 8    |  1 |
    |  7 | 8    |  1 |
    |  8 | 8    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
    mysql> select * from target_tab;
    +------+------+
    | id   | name |
    +------+------+
    | 4    | 1.00 |
    | 5    | 1.00 |
    | 6    | 1.00 |
    | 7    | 1.00 |
    | 8    | 1.00 |
    +------+------+
    5 rows in set (0.00 sec)
    mysql> select * from tt1 where name = '1';
    Empty set (0.00 sec)
    mysql> select * from source_tab where name = '8';
    +----+------+----+
    | d1 | name | d2 |
    +----+------+----+
    |  4 | 8    |  1 |
    |  5 | 8    |  1 |
    |  6 | 8    |  1 |
    |  7 | 8    |  1 |
    |  8 | 8    |  1 |
    +----+------+----+
    5 rows in set (0.00 sec)
    mysql> select * from target_tab;
    +------+------+
    | id   | name |
    +------+------+
    | 4    | 1.00 |
    | 5    | 1.00 |
    | 6    | 1.00 |
    | 7    | 1.00 |
    | 8    | 1.00 |
    +------+------+
    5 rows in set (0.00 sec)
    从上可见,设置系统变量innodb_locks_unsafe_for_binlog的值为“on”后,InnoDB不再对source_tab加锁,结果也符合应用逻辑,但是如果分析BINLOG的内容:
    ......
    SET TIMESTAMP=1169175130;
    BEGIN;
    # at 274
    #070119 10:51:57 server id 1  end_log_pos 105   Query   thread_id=1     exec_time=0     error_code=0
    SET TIMESTAMP=1169175117;
    update source_tab set name = '8' where name = '1';
    # at 379
    #070119 10:52:10 server id 1  end_log_pos 406   Xid = 5
    COMMIT;
    # at 406
    #070119 10:52:14 server id 1  end_log_pos 474   Query   thread_id=2     exec_time=0     error_code=0
    SET TIMESTAMP=1169175134;
    BEGIN;
    # at 474
    #070119 10:51:29 server id 1  end_log_pos 119   Query   thread_id=2     exec_time=0     error_code=0
    SET TIMESTAMP=1169175089;
    insert into target_tab select d1,name from source_tab where name = '1';
    # at 593
    #070119 10:52:14 server id 1  end_log_pos 620   Xid = 7
    COMMIT;
    ......
        可以发现,在BINLOG中,更新操作的位置在INSERT...SELECT之前,如果使用这个BINLOG进行数据库恢复,恢复的结果与实际的应用逻辑不符;如果进行复制,就会导致主从数据库不一致!
    通过上面的例子,我们就不难理解为什么MySQL在处理“Insert  into target_tab select * from source_tab where ...”和“create  table new_tab ...select ... From  source_tab where ...”时要给source_tab加锁,而不是使用对并发影响最小的多版本数据来实现一致性读。还要特别说明的是,如果上述语句的SELECT是范围条件,InnoDB还会给源表加间隙锁(Next-Lock)。
    因此,INSERT...SELECT...和 CREATE TABLE...SELECT...语句,可能会阻止对源表的并发更新,造成对源表锁的等待。如果查询比较复杂的话,会造成严重的性能问题,我们在应用中应尽量避免使用。实际上,MySQL将这种SQL叫作不确定(non-deterministic)的SQL,不推荐使用。
    如果应用中一定要用这种SQL来实现业务逻辑,又不希望对源表的并发更新产生影响,可以采取以下两种措施:
    ¡  一是采取上面示例中的做法,将innodb_locks_unsafe_for_binlog的值设置为“on”,强制MySQL使用多版本数据一致性读。但付出的代价是可能无法用binlog正确地恢复或复制数据,因此,不推荐使用这种方式。
    ¡  二是通过使用“select * from source_tab ... Into outfile”和“load data infile ...”语句组合来间接实现,采用这种方式MySQL不会给source_tab加锁。

    InnoDB在不同隔离级别下的一致性读及锁的差异

    前面讲过,锁和多版本数据是InnoDB实现一致性读和ISO/ANSI SQL92隔离级别的手段,因此,在不同的隔离级别下,InnoDB处理SQL时采用的一致性读策略和需要的锁是不同的。同时,数据恢复和复制机制的特点,也对一些SQL的一致性读策略和锁策略有很大影响。将这些特性归纳成如表20-16所示的内容,以便读者查阅。
    表20-16                                          InnoDB存储引擎中不同SQL在不同隔离级别下锁比较
    隔离级别
            一致性读和锁
    SQL
    Read Uncommited
    Read Commited
    Repeatable Read
    Serializable
    SQL
    条件
           
    select
    相等
    None locks
    Consisten read/None lock
    Consisten read/None lock
    Share locks
    范围
    None locks
    Consisten read/None lock
    Consisten read/None lock
    Share Next-Key
    update
    相等
    exclusive locks
    exclusive locks
    exclusive locks
    Exclusive locks
    范围
    exclusive next-key
    exclusive next-key
    exclusive next-key
    exclusive next-key
    Insert
    N/A
    exclusive locks
    exclusive locks
    exclusive locks
    exclusive locks
    replace
    无键冲突
    exclusive locks
    exclusive locks
    exclusive locks
    exclusive locks
    键冲突
    exclusive next-key
    exclusive next-key
    exclusive next-key
    exclusive next-key
    delete
    相等
    exclusive locks
    exclusive locks
    exclusive locks
    exclusive locks
    范围
    exclusive next-key
    exclusive next-key
    exclusive next-key
    exclusive next-key
    Select ... from ... Lock in share mode
    相等
    Share locks
    Share locks
    Share locks
    Share locks
    范围
    Share locks
    Share locks
    Share Next-Key
    Share Next-Key
    Select * from ... For update
    相等
    exclusive locks
    exclusive locks
    exclusive locks
    exclusive locks
    范围
    exclusive locks
    Share locks
    exclusive next-key
    exclusive next-key
    Insert into ... Select ...
    (指源表锁)
    innodb_locks_unsafe_for_binlog=off
    Share Next-Key
    Share Next-Key
    Share Next-Key
    Share Next-Key
    innodb_locks_unsafe_for_binlog=on
    None locks
    Consisten read/None lock
    Consisten read/None lock
    Share Next-Key
    create table ... Select ...
    (指源表锁)
    innodb_locks_unsafe_for_binlog=off
    Share Next-Key
    Share Next-Key
    Share Next-Key
    Share Next-Key
    innodb_locks_unsafe_for_binlog=on
    None locks
    Consisten read/None lock
    Consisten read/None lock
    Share Next-Key
    从表20-16可以看出:对于许多SQL,隔离级别越高,InnoDB给记录集加的锁就越严格(尤其是使用范围条件的时候),产生锁冲突的可能性也就越高,从而对并发性事务处理性能的影响也就越大。因此,我们在应用中,应该尽量使用较低的隔离级别,以减少锁争用的机率。实际上,通过优化事务逻辑,大部分应用使用Read Commited隔离级别就足够了。对于一些确实需要更高隔离级别的事务,可以通过在程序中执行SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ或SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE动态改变隔离级别的方式满足需求。

    什么时候使用表锁

    对于InnoDB表,在绝大部分情况下都应该使用行级锁,因为事务和行锁往往是我们之所以选择InnoDB表的理由。但在个别特殊事务中,也可以考虑使用表级锁。
    ¡  第一种情况是:事务需要更新大部分或全部数据,表又比较大,如果使用默认的行锁,不仅这个事务执行效率低,而且可能造成其他事务长时间锁等待和锁冲突,这种情况下可以考虑使用表锁来提高该事务的执行速度。
    ¡  第二种情况是:事务涉及多个表,比较复杂,很可能引起死锁,造成大量事务回滚。这种情况也可以考虑一次性锁定事务涉及的表,从而避免死锁、减少数据库因事务回滚带来的开销。
    当然,应用中这两种事务不能太多,否则,就应该考虑使用MyISAM表了。
    在InnoDB下,使用表锁要注意以下两点。
    (1)使用LOCK TABLES虽然可以给InnoDB加表级锁,但必须说明的是,表锁不是由InnoDB存储引擎层管理的,而是由其上一层──MySQL Server负责的,仅当autocommit=0、innodb_table_locks=1(默认设置)时,InnoDB层才能知道MySQL加的表锁,MySQL Server也才能感知InnoDB加的行锁,这种情况下,InnoDB才能自动识别涉及表级锁的死锁;否则,InnoDB将无法自动检测并处理这种死锁。有关死锁,下一小节还会继续讨论。
    (2)在用 LOCK TABLES对InnoDB表加锁时要注意,要将AUTOCOMMIT设为0,否则MySQL不会给表加锁;事务结束前,不要用UNLOCK TABLES释放表锁,因为UNLOCK TABLES会隐含地提交事务;COMMIT或ROLLBACK并不能释放用LOCK TABLES加的表级锁,必须用UNLOCK TABLES释放表锁。正确的方式见如下语句:
    例如,如果需要写表t1并从表t读,可以按如下做:
    SET AUTOCOMMIT=0;
    LOCK TABLES t1 WRITE, t2 READ, ...;
    [do something with tables t1 and t2 here];
    COMMIT;
    UNLOCK TABLES;

    关于死锁

    上文讲过,MyISAM表锁是deadlock free的,这是因为MyISAM总是一次获得所需的全部锁,要么全部满足,要么等待,因此不会出现死锁。但在InnoDB中,除单个SQL组成的事务外,锁是逐步获得的,这就决定了在InnoDB中发生死锁是可能的。如表20-17所示的就是一个发生死锁的例子。
    表20-17 InnoDB存储引擎中的死锁例子
    session_1
    session_2
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from table_1 where where id=1 for update;
    ...
    做一些其他处理...
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select * from table_2 where id=1 for update;
    ...
    select * from table_2 where id =1 for update;
    因session_2已取得排他锁,等待
    做一些其他处理...
     
    mysql> select * from table_1 where where id=1 for update;
    死锁
    在上面的例子中,两个事务都需要获得对方持有的排他锁才能继续完成事务,这种循环锁等待就是典型的死锁。
    发生死锁后,InnoDB一般都能自动检测到,并使一个事务释放锁并回退,另一个事务获得锁,继续完成事务。但在涉及外部锁,或涉及表锁的情况下,InnoDB并不能完全自动检测到死锁,这需要通过设置锁等待超时参数 innodb_lock_wait_timeout来解决。需要说明的是,这个参数并不是只用来解决死锁问题,在并发访问比较高的情况下,如果大量事务因无法立即获得所需的锁而挂起,会占用大量计算机资源,造成严重性能问题,甚至拖跨数据库。我们通过设置合适的锁等待超时阈值,可以避免这种情况发生。
    通常来说,死锁都是应用设计的问题,通过调整业务流程、数据库对象设计、事务大小,以及访问数据库的SQL语句,绝大部分死锁都可以避免。下面就通过实例来介绍几种避免死锁的常用方法。
    (1)在应用中,如果不同的程序会并发存取多个表,应尽量约定以相同的顺序来访问表,这样可以大大降低产生死锁的机会。在下面的例子中,由于两个session访问两个表的顺序不同,发生死锁的机会就非常高!但如果以相同的顺序来访问,死锁就可以避免。
    表20-18        InnoDB存储引擎中表顺序造成的死锁例子
    session_1
    session_2
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select first_name,last_name from actor where actor_id = 1 for update;
    +------------+-----------+
    | first_name | last_name |
    +------------+-----------+
    | PENELOPE   | GUINESS   |
    +------------+-----------+
    1 row in set (0.00 sec)
     
     
    mysql> insert into country (country_id,country) values(110,'Test');
    Query OK, 1 row affected (0.00 sec)
    mysql>  insert into country (country_id,country) values(110,'Test');
    等待
     
     
    mysql> select first_name,last_name from actor where actor_id = 1 for update;
    +------------+-----------+
    | first_name | last_name |
    +------------+-----------+
    | PENELOPE   | GUINESS   |
    +------------+-----------+
    1 row in set (0.00 sec)
    mysql>  insert into country (country_id,country) values(110,'Test');
    ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
     
    (2)在程序以批量方式处理数据的时候,如果事先对数据排序,保证每个线程按固定的顺序来处理记录,也可以大大降低出现死锁的可能。
    表20-19        InnoDB存储引擎中表数据操作顺序不一致造成的死锁例子
    session_1
    session_2
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select first_name,last_name from actor where actor_id = 1 for update;
    +------------+-----------+
    | first_name | last_name |
    +------------+-----------+
    | PENELOPE   | GUINESS   |
    +------------+-----------+
    1 row in set (0.00 sec)
     
     
    mysql> select first_name,last_name from actor where actor_id = 3 for update;
    +------------+-----------+
    | first_name | last_name |
    +------------+-----------+
    | ED         | CHASE     |
    +------------+-----------+
    1 row in set (0.00 sec)
    mysql> select first_name,last_name from actor where actor_id = 3 for update;
    等待
     
     
    mysql> select first_name,last_name from actor where actor_id = 1 for update;
    ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
    mysql> select first_name,last_name from actor where actor_id = 3 for update;
    +------------+-----------+
    | first_name | last_name |
    +------------+-----------+
    | ED         | CHASE     |
    +------------+-----------+
    1 row in set (4.71 sec)
     
    (3)在事务中,如果要更新记录,应该直接申请足够级别的锁,即排他锁,而不应先申请共享锁,更新时再申请排他锁,因为当用户申请排他锁时,其他事务可能又已经获得了相同记录的共享锁,从而造成锁冲突,甚至死锁。具体演示可参见20.3.3小节中的例子。
           (4)前面讲过,在REPEATABLE-READ隔离级别下,如果两个线程同时对相同条件记录用SELECT...FOR UPDATE加排他锁,在没有符合该条件记录情况下,两个线程都会加锁成功。程序发现记录尚不存在,就试图插入一条新记录,如果两个线程都这么做,就会出现死锁。这种情况下,将隔离级别改成READ COMMITTED,就可避免问题,如表20-20所示。
    表20-20   InnoDB存储引擎中隔离级别引起的死锁例子1
    session_1
    session_2
    mysql> select @@tx_isolation;
    +-----------------+
    | @@tx_isolation  |
    +-----------------+
    | REPEATABLE-READ |
    +-----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    mysql> select @@tx_isolation;
    +-----------------+
    | @@tx_isolation  |
    +-----------------+
    | REPEATABLE-READ |
    +-----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit = 0;
    Query OK, 0 rows affected (0.00 sec)
    当前session对不存在的记录加for update的锁:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 201 for update;
    Empty set (0.00 sec)
     
     
    其他session也可以对不存在的记录加for update的锁:
    mysql> select actor_id,first_name,last_name from actor where actor_id = 201 for update;
    Empty set (0.00 sec)
    因为其他session也对该记录加了锁,所以当前的插入会等待:
    mysql> insert into actor (actor_id , first_name , last_name) values(201,'Lisa','Tom');
    等待
     
     
    因为其他session已经对记录进行了更新,这时候再插入记录就会提示死锁并退出:
    mysql> insert into actor (actor_id, first_name , last_name) values(201,'Lisa','Tom');
    ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
    由于其他session已经退出,当前session可以获得锁并成功插入记录:
    mysql> insert into actor (actor_id , first_name , last_name) values(201,'Lisa','Tom');
    Query OK, 1 row affected (13.35 sec)
     
    (5)当隔离级别为READ COMMITTED时,如果两个线程都先执行SELECT...FOR UPDATE,判断是否存在符合条件的记录,如果没有,就插入记录。此时,只有一个线程能插入成功,另一个线程会出现锁等待,当第1个线程提交后,第2个线程会因主键重出错,但虽然这个线程出错了,却会获得一个排他锁!这时如果有第3个线程又来申请排他锁,也会出现死锁。
    对于这种情况,可以直接做插入操作,然后再捕获主键重异常,或者在遇到主键重错误时,总是执行ROLLBACK释放获得的排他锁,如表20-21所示。
    表20-21   InnoDB存储引擎中隔离级别引起的死锁例子2
    session_1
    session_2
    session_3
    mysql> select @@tx_isolation;
    +----------------+
    | @@tx_isolation |
    +----------------+
    | READ-COMMITTED |
    +----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.01 sec)
    mysql> select @@tx_isolation;
    +----------------+
    | @@tx_isolation |
    +----------------+
    | READ-COMMITTED |
    +----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.01 sec)
    mysql> select @@tx_isolation;
    +----------------+
    | @@tx_isolation |
    +----------------+
    | READ-COMMITTED |
    +----------------+
    1 row in set (0.00 sec)
    mysql> set autocommit=0;
    Query OK, 0 rows affected (0.01 sec)
    Session_1获得for update的共享锁:
    mysql> select actor_id, first_name,last_name from actor where actor_id = 201 for update;
    Empty set (0.00 sec)
    由于记录不存在,session_2也可以获得for update的共享锁:
    mysql> select actor_id, first_name,last_name from actor where actor_id = 201 for update;
    Empty set (0.00 sec)
     
    Session_1可以成功插入记录:
    mysql> insert into actor (actor_id,first_name,last_name) values(201,'Lisa','Tom');
    Query OK, 1 row affected (0.00 sec)
       
     
    Session_2插入申请等待获得锁:
    mysql> insert into actor (actor_id,first_name,last_name) values(201,'Lisa','Tom');
    等待
     
    Session_1成功提交:
    mysql> commit;
    Query OK, 0 rows affected (0.04 sec)
       
     
    Session_2获得锁,发现插入记录主键重,这个时候抛出了异常,但是并没有释放共享锁:
    mysql> insert into actor (actor_id,first_name,last_name) values(201,'Lisa','Tom');
    ERROR 1062 (23000): Duplicate entry '201' for key 'PRIMARY'
     
       
    Session_3申请获得共享锁,因为session_2已经锁定该记录,所以session_3需要等待:
    mysql> select actor_id, first_name,last_name from actor where actor_id = 201 for update;
    等待
     
    这个时候,如果session_2直接对记录进行更新操作,则会抛出死锁的异常:
    mysql> update actor set last_name='Lan' where actor_id = 201;
    ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction
     
       
    Session_2释放锁后,session_3获得锁:
    mysql> select first_name, last_name from actor where actor_id = 201 for update;
    +------------+-----------+
    | first_name | last_name |
    +------------+-----------+
    | Lisa       | Tom       |
    +------------+-----------+
    1 row in set (31.12 sec)
    尽管通过上面介绍的设计和SQL优化等措施,可以大大减少死锁,但死锁很难完全避免。因此,在程序设计中总是捕获并处理死锁异常是一个很好的编程习惯。
    如果出现死锁,可以用SHOW INNODB STATUS命令来确定最后一个死锁产生的原因。返回结果中包括死锁相关事务的详细信息,如引发死锁的SQL语句,事务已经获得的锁,正在等待什么锁,以及被回滚的事务等。据此可以分析死锁产生的原因和改进措施。下面是一段SHOW INNODB STATUS输出的样例:
    mysql> show innodb status G
    …….
    ------------------------
    LATEST DETECTED DEADLOCK
    ------------------------
    070710 14:05:16
    *** (1) TRANSACTION:
    TRANSACTION 0 117470078, ACTIVE 117 sec, process no 1468, OS thread id 1197328736 inserting
    mysql tables in use 1, locked 1
    LOCK WAIT 5 lock struct(s), heap size 1216
    MySQL thread id 7521657, query id 673468054 localhost root update
    insert into country (country_id,country) values(110,'Test')
    ………
    *** (2) TRANSACTION:
    TRANSACTION 0 117470079, ACTIVE 39 sec, process no 1468, OS thread id 1164048736 starting index read, thread declared inside InnoDB 500
    mysql tables in use 1, locked 1
    4 lock struct(s), heap size 1216, undo log entries 1
    MySQL thread id 7521664, query id 673468058 localhost root statistics
    select first_name,last_name from actor where actor_id = 1 for update
    *** (2) HOLDS THE LOCK(S):
    ………
    *** (2) WAITING FOR THIS LOCK TO BE GRANTED:
    ………
    *** WE ROLL BACK TRANSACTION (1)
    ……
    小结
    本章重点介绍了MySQL中MyISAM表级锁和InnoDB行级锁的实现特点,并讨论了两种存储引擎经常遇到的锁问题和解决办法。
    对于MyISAM的表锁,主要讨论了以下几点:
    (1)共享读锁(S)之间是兼容的,但共享读锁(S)与排他写锁(X)之间,以及排他写锁(X)之间是互斥的,也就是说读和写是串行的。
    (2)在一定条件下,MyISAM允许查询和插入并发执行,我们可以利用这一点来解决应用中对同一表查询和插入的锁争用问题。
    (3)MyISAM默认的锁调度机制是写优先,这并不一定适合所有应用,用户可以通过设置LOW_PRIORITY_UPDATES参数,或在INSERT、UPDATE、DELETE语句中指定LOW_PRIORITY选项来调节读写锁的争用。
    (4)由于表锁的锁定粒度大,读写之间又是串行的,因此,如果更新操作较多,MyISAM表可能会出现严重的锁等待,可以考虑采用InnoDB表来减少锁冲突。
    对于InnoDB表,本章主要讨论了以下几项内容。
    l         InnoDB的行锁是基于锁引实现的,如果不通过索引访问数据,InnoDB会使用表锁。
    l         介绍了InnoDB间隙锁(Next-key)机制,以及InnoDB使用间隙锁的原因。
    l         在不同的隔离级别下,InnoDB的锁机制和一致性读策略不同。
    l         MySQL的恢复和复制对InnoDB锁机制和一致性读策略也有较大影响。
    l         锁冲突甚至死锁很难完全避免。
    在了解InnoDB锁特性后,用户可以通过设计和SQL调整等措施减少锁冲突和死锁,包括:
    l         尽量使用较低的隔离级别;
    l         精心设计索引,并尽量使用索引访问数据,使加锁更精确,从而减少锁冲突的机会;
    l         选择合理的事务大小,小事务发生锁冲突的几率也更小;
    l         给记录集显示加锁时,最好一次性请求足够级别的锁。比如要修改数据的话,最好直接申请排他锁,而不是先申请共享锁,修改时再请求排他锁,这样容易产生死锁;
    l         不同的程序访问一组表时,应尽量约定以相同的顺序访问各表,对一个表而言,尽可能以固定的顺序存取表中的行。这样可以大大减少死锁的机会;
    l         尽量用相等条件访问数据,这样可以避免间隙锁对并发插入的影响;
    l         不要申请超过实际需要的锁级别;除非必须,查询时不要显示加锁;
    l         对于一些特定的事务,可以使用表锁来提高处理速度或减少死锁的可能。
     
    好文要顶 关注我 收藏该文  
  • 相关阅读:
    003. 爬楼梯
    ZFlie网盘框架说明
    WPF基础:Dispatcher介绍
    WPF自定义控件三:消息提示框
    GO Time 类型方法处理集合
    UserControl 加载动画
    WPF自定义控件二:Border控件与TextBlock控件轮播动画
    WPF自定义控件一:StackPanel 控件轮播
    WPF 图表控件之曲线绘制与移动
    VueApp 自动更新解决plus is not defined问题
  • 原文地址:https://www.cnblogs.com/feiyun8616/p/11517990.html
Copyright © 2011-2022 走看看