最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MySQL鎖阻塞的深入分析

 更新時間:2020年12月02日 16:49:00   作者:bosco1986  
這篇文章主要給大家介紹了關(guān)于MySQL鎖阻塞的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

日常維護中,經(jīng)常會碰到線程被阻塞,導(dǎo)致數(shù)據(jù)庫響應(yīng)非常慢,下面就看看如何獲取是哪個線程導(dǎo)致了阻塞的。

1. 環(huán)境說明

RHEL 6.4 x86_64 + MySQL 5.6.19

事務(wù)隔離級別:RR

2. 測試過程

3. 查看鎖阻塞線程信息

這里用幾中方法進行分析:

3.1  使用show processlist查看

MySQL [(none)]> show processlist;
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
| Id | User | Host  | db | Command | Time | State  | Info          |
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
| 2 | root | localhost | NULL | Query | 0 | init   | show processlist       |
| 3 | root | localhost | test | Query | 70 | Sending data | select count(*) from t3 a,t3 b   |
| 4 | root | localhost | test | Query | 65 | updating  | delete from emp where empno=7788   |
| 7 | root | localhost | test | Query | 68 | updating  | update emp set sal=3500 where empno=7788 |
+----+------+-----------+------+---------+------+--------------+------------------------------------------+
4 rows in set (0.00 sec)

如果數(shù)據(jù)庫存在較多線程的話,這種方法確實不太好確認的。

3.2  直接使用show engine innodb status查看

------------
TRANSACTIONS
------------
Trx id counter 4131
Purge done for trx's n:o < 4119 undo n:o < 0 state: running but idle
History list length 126
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 2, OS thread handle 0x7f953ffff700, query id 115 localhost root init
show engine innodb status
---TRANSACTION 4130, ACTIVE 41 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 4, OS thread handle 0x7f953ff9d700, query id 112 localhost root updating
delete from emp where empno=7788
------- TRX HAS BEEN WAITING 41 SEC FOR THIS LOCK TO BE GRANTED: ## 等待了41s
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4130 lock_mode X locks rec but not gap waiting
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0 ## 線程4在等待往test.emp中的主鍵上加X鎖,page num=3
 0: len 4; hex 80001e6c; asc l;;
 1: len 6; hex 000000001018; asc  ;;
 2: len 7; hex 91000001420084; asc  B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc  ;;
 6: len 4; hex 208794f0; asc  ;;
 7: len 4; hex 80000bb8; asc  ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc  ;;
 
------------------
---TRANSACTION 4129, ACTIVE 45 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 7, OS thread handle 0x7f953ff6c700, query id 111 localhost root updating
update emp set sal=3500 where empno=7788
------- TRX HAS BEEN WAITING 45 SEC FOR THIS LOCK TO BE GRANTED: ## 等待了45s
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4129 lock_mode X locks rec but not gap waiting
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0 ## 線程7在等待往test.emp中的主鍵上加X鎖,page num=3
 0: len 4; hex 80001e6c; asc l;;
 1: len 6; hex 000000001018; asc  ;;
 2: len 7; hex 91000001420084; asc  B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc  ;;
 6: len 4; hex 208794f0; asc  ;;
 7: len 4; hex 80000bb8; asc  ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc  ;;
 
------------------
---TRANSACTION 4128, ACTIVE 51 sec
2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 3, OS thread handle 0x7f953ffce700, query id 110 localhost root cleaning up

我們知道,主要根因還是thread=3引起的,但從innodb status中卻無法分析得到這個結(jié)果。

從上面來看,線程4和線程7都在等待往test.emp中的主鍵上加X鎖,page num=3,但是線程7等待的時間為45s,而線程4等待的時間為41s,是較線程7之后申請的鎖,所以可以判斷是線程7阻塞了線程4。至于線程7為什么出現(xiàn)等待,這里分析不到根因。

3.3  使用mysqladmin debug查看

# mysqladmin -S /tmp/mysql3306.sock debug

然后在error日志中,會看到:

Thread database.table_name   Locked/Waiting  Lock_type
 
 
3  test.t3      Locked - read   Low priority read lock
7  test.emp     Locked - write  High priority write lock

這種方法中,能找到線程ID=3和7是阻塞者,但還是不太準確,判斷不出來線程7也是被線程ID=3阻塞的。

3.4  使用innodb_lock_monitor來獲取阻塞鎖線程

MySQL [test]> CREATE TABLE innodb_lock_monitor (a INT) ENGINE=INNODB; ## 隨便在一個數(shù)據(jù)庫中創(chuàng)建這個表,就會打開lock monitor
Query OK, 0 rows affected, 1 warning (0.07 sec)
 
MySQL [test]> show warnings\G
*************************** 1. row ***************************
 Level: Warning
 Code: 131
Message: Using the table name innodb_lock_monitor to enable diagnostic output is deprecated and may be removed in future releases. Use INFORMATION_SCHEMA or PERFORMANCE_SCHEMA tables or SET GLOBAL innodb_status_output=ON.
1 row in set (0.00 sec)

說明:這個在5.6中有一個warning,但不影響使用。

然后再使用show engine innodb status查看:

------------
TRANSACTIONS
------------
Trx id counter 4667
Purge done for trx's n:o < 4659 undo n:o < 0 state: running but idle
History list length 138
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 9, OS thread handle 0x7f813c5f7700, query id 152 localhost root init
show engine innodb status
---TRANSACTION 4663, ACTIVE 78 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 4, OS thread handle 0x7f813c628700, query id 149 localhost root updating
delete from emp where empno=7788
------- TRX HAS BEEN WAITING 78 SEC FOR THIS LOCK TO BE GRANTED:  ## 等待了78s
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4663 lock_mode X locks rec but not gap waiting
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0 ## 線程4在等待往test.emp中的主鍵上加X鎖,page num=3
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc   ;;
 
------------------
TABLE LOCK table `test`.`emp` trx id 4663 lock mode IX  ## 在給主鍵行上加X鎖之前,先要在表上加意向鎖IX
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4663 lock_mode X locks rec but not gap waiting
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc   ;;
 
---TRANSACTION 4662, ACTIVE 81 sec starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 7, OS thread handle 0x7f813c5c6700, query id 148 localhost root updating
update emp set sal=3500 where empno=7788
------- TRX HAS BEEN WAITING 81 SEC FOR THIS LOCK TO BE GRANTED: ## 等待了81s
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4662 lock_mode X locks rec but not gap waiting
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0 ## 線程7在等待往test.emp中的主鍵上加X鎖,page num=3
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc   ;;
 
------------------
TABLE LOCK table `test`.`emp` trx id 4662 lock mode IX  ## 在給主鍵行上加X鎖之前,先要在表上加意向鎖IX
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4662 lock_mode X locks rec but not gap waiting
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc   ;;
 
---TRANSACTION 4615, ACTIVE 1579 sec, thread declared inside InnoDB 1222
mysql tables in use 2, locked 0
2 lock struct(s), heap size 360, 1 row lock(s)
MySQL thread id 3, OS thread handle 0x7f813c659700, query id 147 localhost root Sending data
select count(*) from t3 a,t3 b  ## 這是線程3當(dāng)前正在執(zhí)行的SQL
Trx read view will not see trx with id >= 4662, sees < 4659
TABLE LOCK table `test`.`emp` trx id 4615 lock mode IX ## 線程3中正在擁有表上的意向IX鎖,并且有test.emp表上主鍵的行級X鎖,page num=3
RECORD LOCKS space id 16 page no 3 n bits 88 index `PRIMARY` of table `test`.`emp` trx id 4615 lock_mode X locks rec but not gap
Record lock, heap no 9 PHYSICAL RECORD: n_fields 10; compact format; info bits 0
 0: len 4; hex 80001e6c; asc  l;;
 1: len 6; hex 000000001018; asc    ;;
 2: len 7; hex 91000001420084; asc   B ;;
 3: len 5; hex 53434f5454; asc SCOTT;;
 4: len 7; hex 414e414c595354; asc ANALYST;;
 5: len 4; hex 80001d8e; asc   ;;
 6: len 4; hex 208794f0; asc   ;;
 7: len 4; hex 80000bb8; asc   ;;
 8: SQL NULL;
 9: len 4; hex 80000014; asc   ;;

為什么線程3當(dāng)前執(zhí)行的是一個select t3表操作,但卻鎖住了test.emp表上page num=3?

有可能是線程3之前對test.emp表的操作事務(wù)沒有及時提交導(dǎo)致。

所以得出:線程3阻塞了線程7,而線程7又阻塞了線程4,所以根因就是線程3,讓線程3盡快提交或是kill掉即可。

4. 結(jié)論

在分析innodb中鎖阻塞時,幾種方法的對比情況:

(1)使用show processlist查看不靠譜;

(2)直接使用show engine innodb status查看,無法判斷到問題的根因;

(3)使用mysqladmin debug查看,能看到所有產(chǎn)生鎖的線程,但無法判斷哪個才是根因;

(4)開啟innodb_lock_monitor后,再使用show engine innodb status查看,能夠找到鎖阻塞的根因。

原文鏈接:https://blog.csdn.net/hw_libo/article/details/39080809

到此這篇關(guān)于MySQL鎖阻塞深入分析的文章就介紹到這了,更多相關(guān)MySQL鎖阻塞內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • mysql 5.6.37(zip)下載安裝配置圖文教程

    mysql 5.6.37(zip)下載安裝配置圖文教程

    這篇文章主要為大家詳細介紹了mysql 5.6.37(zip)下載安裝配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • mysql 5.7.12 winx64安裝配置方法圖文教程

    mysql 5.7.12 winx64安裝配置方法圖文教程

    這篇文章主要為大家分享了mysql 5.7.12winx64安裝配置方法圖文教程,感興趣的朋友可以參考一下
    2016-05-05
  • MySQL中Distinct和Group By語句的基本使用教程

    MySQL中Distinct和Group By語句的基本使用教程

    這篇文章主要介紹了MySQL中Distinct和Group By語句的基本使用教程,這里主要是針對查詢結(jié)果去重的用法,需要的朋友可以參考下
    2015-12-12
  • SQL字段拼接成新字段幾種常見的方法

    SQL字段拼接成新字段幾種常見的方法

    這篇文章主要給大家介紹了關(guān)于SQL字段拼接成新字段幾種常見的方法,如我們在選擇商品的時候不止需要知道商品的名字,還需要商品代碼型號等,這個時候需要把這些字段拼接為一個字段進行操作或者輸出,需要的朋友可以參考下
    2023-08-08
  • Mysql的列修改成行并顯示數(shù)據(jù)的簡單實現(xiàn)

    Mysql的列修改成行并顯示數(shù)據(jù)的簡單實現(xiàn)

    這篇文章主要介紹了Mysql的列修改成行并顯示數(shù)據(jù)的簡單實現(xiàn),本文給大家介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下
    2016-10-10
  • 一個 20 秒 SQL 慢查詢優(yōu)化處理方案

    一個 20 秒 SQL 慢查詢優(yōu)化處理方案

    這篇文章主要分享一個 20 秒 SQL 慢查詢優(yōu)化的經(jīng)歷與處理方案,頁面無法正確獲取數(shù)據(jù),經(jīng)排查原來是接口調(diào)用超時,而最后發(fā)現(xiàn)是因為SQL查詢長達到20多秒而導(dǎo)致了問題的發(fā)生,下面來看問題具體介紹吧
    2022-01-01
  • mysql中使用date_add()函數(shù)講解

    mysql中使用date_add()函數(shù)講解

    這篇文章主要介紹了mysql中使用date_add()函數(shù)講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • Mysql時間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條

    Mysql時間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條

    這篇文章主要介紹了Mysql時間軸數(shù)據(jù) 獲取同一天數(shù)據(jù)的前三條 ,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-07-07
  • MySQL語句之MD5()的使用方式

    MySQL語句之MD5()的使用方式

    這篇文章主要介紹了MySQL語句之MD5()的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Mysql Workbench查詢mysql數(shù)據(jù)庫方法

    Mysql Workbench查詢mysql數(shù)據(jù)庫方法

    在本篇文章里小編給大家分享了個關(guān)于Mysql Workbench查詢mysql數(shù)據(jù)庫方法和步驟,有需要的朋友們學(xué)習(xí)下。
    2019-03-03

最新評論

胶南市| 正蓝旗| 贞丰县| 长白| 天水市| 台湾省| 阳春市| 宁陵县| 洪洞县| 济源市| 西贡区| 荥阳市| 丘北县| 海宁市| 隆子县| 兴山县| 香港 | 什邡市| 始兴县| 元朗区| 齐齐哈尔市| 宁武县| 余江县| 河北省| 岳西县| 辉县市| 丰顺县| 忻城县| 新龙县| 丹凤县| 尼玛县| 永清县| 苏尼特左旗| 华容县| 钟祥市| 南昌县| 南丰县| 乌拉特中旗| 海盐县| 盐池县| 陆河县|