1. 程式人生 > 其它 >檢視Mysql正在執行的事務、鎖、等待

檢視Mysql正在執行的事務、鎖、等待

一、關於鎖的三張表(MEMORY、InnoDB引擎)

## 當前執行的所有事務
mysql> select * from information_schema.innodb_trx\G;
*************************** 1. row ***************************
          trx_id: 613963
        trx_state: LOCK WAIT              #事務狀態
       trx_started: 2019-02-22 10:48:48
  trx_requested_lock_id: 613963:460:3:4
     trx_wait_started: 2019-02-22 11:08:41
        trx_weight: 2
   trx_mysql_thread_id: 140
        trx_query: update tx1 set c1='heyfffff',c2='heyffffff' where id =3
   trx_operation_state: starting index read
    trx_tables_in_use: 1
    trx_tables_locked: 1
     trx_lock_structs: 2
  trx_lock_memory_bytes: 1136
     trx_rows_locked: 7  #事務鎖住的行數
    trx_rows_modified: 0
 trx_concurrency_tickets: 0             #事務併發票數    
   trx_isolation_level: REPEATABLE READ
    trx_unique_checks: 1
  trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 0
     trx_is_read_only: 0
trx_autocommit_non_locking: 0
*************************** 2. row ***************************
          trx_id: 613962
        trx_state: RUNNING
       trx_started: 2019-02-22 10:46:29
  trx_requested_lock_id: NULL
     trx_wait_started: NULL
        trx_weight: 3
   trx_mysql_thread_id: 138
        trx_query: select * from information_schema.innodb_trx
   trx_operation_state: NULL
    trx_tables_in_use: 0
    trx_tables_locked: 1
     trx_lock_structs: 2
  trx_lock_memory_bytes: 1136
     trx_rows_locked: 1
    trx_rows_modified: 1
 trx_concurrency_tickets: 0
   trx_isolation_level: REPEATABLE READ
    trx_unique_checks: 1
  trx_foreign_key_checks: 1
trx_last_foreign_key_error: NULL
trx_adaptive_hash_latched: 0
trx_adaptive_hash_timeout: 0
     trx_is_read_only: 0
trx_autocommit_non_locking: 0
2 rows in set (0.00 sec)

## 當前出現的鎖
mysql> select * from information_schema.innodb_locks\G;
*************************** 1. row ***************************
  lock_id: 613963:460:3:4
lock_trx_id: 613963
 lock_mode: X
 lock_type: RECORD
lock_table: `test`.`tx1`
lock_index: PRIMARY
lock_space: 460
 lock_page: 3
 lock_rec: 4
 lock_data: 3
*************************** 2. row ***************************
  lock_id: 613962:460:3:4
lock_trx_id: 613962
 lock_mode: X
 lock_type: RECORD
lock_table: `test`.`tx1`
lock_index: PRIMARY
lock_space: 460
 lock_page: 3
 lock_rec: 4
 lock_data: 3
2 rows in set, 1 warning (0.00 sec)

## 鎖等待的對應關係 
mysql> select * from information_schema.innodb_lock_waits\G;
*************************** 1. row ***************************
requesting_trx_id: 613963         
requested_lock_id: 613963:460:3:4         #請求鎖的鎖ID
 blocking_trx_id: 613962 #當前擁有鎖的事務ID
blocking_lock_id: 613962:460:3:4
1 row in set, 1 warning (0.00 sec)

二、檢視鎖的情況

mysql> show status like 'innodb_row_lock_%';
+-------------------------------+--------+
| Variable_name         | Value |
+-------------------------------+--------+
| Innodb_row_lock_current_waits | 1   |
| Innodb_row_lock_time     | 479764 |
| Innodb_row_lock_time_avg   | 39980 |
| Innodb_row_lock_time_max   | 51021 |
| Innodb_row_lock_waits     | 12   |
+-------------------------------+--------+
5 rows in set (0.00 sec)

解釋如下:
Innodb_row_lock_current_waits : 當前等待鎖的數量
Innodb_row_lock_time : 系統啟動到現在,鎖定的總時間長度
Innodb_row_lock_time_avg : 每次平均鎖定的時間
Innodb_row_lock_time_max : 最長一次鎖定時間
Innodb_row_lock_waits : 系統啟動到現在總共鎖定的次數  



# 查詢是否鎖表
mysql> show OPEN TABLES where In_use > 0;
+----------+-------+--------+-------------+
| Database | Table | In_use | Name_locked |
+----------+-------+--------+-------------+
| test   | tx1  |   1 |      0 |
+----------+-------+--------+-------------+
1 row in set (0.00 sec)

三、殺死程序

# 1.查詢程序
mysql> show processlist\G                   # \G 結構旋轉90度變成縱向
*************************** 1. row ***************************
  Id: 138
 User: root
 Host: localhost:55106
  db: test
Command: Query
 Time: 0
 State: starting
 Info: show processlist
*************************** 2. row ***************************
  Id: 140
 User: root
 Host: localhost:56158
  db: test
Command: Sleep   # 正在等待客戶端向它傳送執行語句
 Time: 145
 State:
 Info: NULL
2 rows in set (0.00 sec)

# 2.殺死對應程序ID 
mysql> kill 140;
Query OK, 0 rows affected (0.00 sec)

mysql> show processlist\G
*************************** 1. row ***************************
  Id: 138
 User: root
 Host: localhost:55106
  db: test
Command: Query #該執行緒正在執行一個語句 Sleep:執行緒正在等待客戶端向其傳送新的語句。
Time: 0 State: starting Info: show processlist 1 row in set (0.00 sec)

四、SQL分析

explain使用方法

EXPLAIN命令,用於顯示SQL語句的查詢執行計劃。EXPLAIN為用於SELECT語句中的每個表返回一行資訊。

    EXPLAIN EXTENDED命令:顯示SQL語句的詳細的查詢執行計劃;之後可以通過"SHOW WARNINGS"命令檢視詳細資訊。
    SHOW WARNINGS命令:可以檢視MySQL優化器優化後的SQL語句。

    EXPLAIN PARTITIONS命令:顯示SQL語句的帶有分割槽表資訊的查詢執行計劃。
    EXPLAIN命令的輸出格式:
      TRADITIONAL:傳統型別,按行隔離,每行標識一個自操作。
      JSON:JSON格式。

使用方法為在SQL語句前加explain

得到結果如下:

  1. mysql> explain select id,c1 from t1 where c1=4398825;
  2. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
  5. | 1 | SIMPLE | t1 | ALL | NULL | NULL | NULL | NULL | 4992210 | Using where |
  6. +----+-------------+-------+------+---------------+------+---------+------+---------+-------------+
  7. 1 row in set (0.00 sec)

各列功能如下:

  • id: 按照sql語法解析後分層後的編號,可能重複

  • select_type:

    • SIMPLE,簡單的select查詢,不使用union及子查詢

    • PRIMARY,最外層的select查詢

    • UNION,UNION 中的第二個或隨後的 select 查詢,不依賴於外部查詢的結果集

    • DEPENDENT UNION,UNION 中的第二個或隨後的 select 查詢,依賴於外部查詢的結果集

    • SUBQUERY,子查詢中的第一個 select 查詢,不依賴於外部查詢的結果集

    • DEPENDENT SUBQUERY,子查詢中的第一個 select 查詢,依賴於外部查詢的結果集

    • DERIVED,用於 from子句裡有子查詢的情況。 MySQL會遞迴執行這些子查詢, 把結果放在臨時表裡。

    • UNCACHEABLE SUBQUERY,結果集不能被快取的子查詢,必須重新為外層查詢的每一行進行評估。

    • UNCACHEABLE UNION,UNION 中的第二個或隨後的 select 查詢,屬於不可快取的子查詢

  • table:涉及的表,如果SQL中表有賦別名,這裡出現的是別名

  • type:

    • system,從系統表讀一行。這是const聯接型別的一個特例。

    • const,表最多有一個匹配行,它將在查詢開始時被讀取。因為僅有一行,在這行的列值可被優化器剩餘部分認為是常數。const表很快,因為它們只讀取一次!

    • eq_ref,查詢條件為等於

    • ref,條件查詢不等於

    • ref_or_null,同ref(條件查詢),包含NULL值的行。

    • index_merge,索引聯合查詢

    • unique_subquery,利用唯一索引進行子查詢

    • index_subquery,用非唯一索引進行子查詢

    • range,索引範圍掃描

    • index,索引全掃描

    • ALL,全表掃描。

  • possible_keys:可能使用的索引

  • key:sql中使用的索引

  • key_len:索引長度

  • ref:使用哪個列或常數與key一起從表中選擇行。

  • rows:顯示MYSQL執行查詢的行數,簡單且重要,數值越大越不好,說明沒有用好索引

  • Extra:該列包含MySQL解決查詢的詳細資訊。

    • Distinct,去重,返回第一個滿足條件的值

    • Not exists 使用not exists查詢

    • Range checked for each record,有索引,但索引選擇率很低

    • Using filesort,有序查詢

    • Using index,索引全掃描

    • Using index condition,索引查詢

    • Using temporary,臨表表檢索

    • Using where,where條件查詢

    • Using sort_union,有序合併查詢

    • Using union,合併查詢

    • Using intersect,索引交叉合併

    • Impossible WHERE noticed after reading const tables,讀取const tables,查詢結果為空

    • No tables used,沒有使用表

    • Using join buffer (Block Nested Loop),使用join buffer(BNL演算法)

    • Using MRR(Multi-Range Read ) 使用輔助索引進行多範圍讀

五、資料庫的一些預設設定

# 檢視資料庫預設儲存引擎
mysql> show engines; 
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine       | Support | Comment                            | Transactions | XA  | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB       | DEFAULT | Supports transactions, row-level locking, and foreign keys   | YES     | YES | YES    |
| MRG_MYISAM     | YES   | Collection of identical MyISAM tables             | NO      | NO  | NO     |
| MEMORY       | YES   | Hash based, stored in memory, useful for temporary tables   | NO      | NO  | NO     |
| BLACKHOLE     | YES   | /dev/null storage engine (anything you write to it disappears) | NO      | NO  | NO     |
| MyISAM       | YES   | MyISAM storage engine                     | NO      | NO  | NO     |
| CSV        | YES   | CSV storage engine                       | NO      | NO  | NO     |
| ARCHIVE      | YES   | Archive storage engine                     | NO      | NO  | NO     |
| PERFORMANCE_SCHEMA | YES   | Performance Schema                       | NO      | NO  | NO     |
| FEDERATED     | NO   | Federated MySQL storage engine                 | NULL     | NULL | NULL    |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

# 檢視當前資料庫預設隔離級別
mysql> select @@global.tx_isolation; 
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ       |
+-----------------------+
1 row in set, 1 warning (0.00 sec)

# 預設自動提交事務
mysql> show global variables like 'autocommit';  
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit  | ON  |
+---------------+-------+
1 row in set (0.00 sec)

mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
|          1 |
+---------------------+
1 row in set (0.00 sec)

轉載於:https://blog.csdn.net/weixin_34321977/article/details/92516068

知止而後有定,定而後能靜,靜而後能安,安而後能慮,慮而後能得。 所謂誠其意者,毋自欺也。