91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

基于更新SQL語句理解MySQL鎖定詳解

發布時間:2020-09-26 09:44:14 來源:腳本之家 閱讀:173 作者:Leshami 欄目:MySQL數據庫

前言

MySQL數據庫鎖是實現數據一致性,解決并發問題的重要手段。數據庫是一個多用戶共享的資源,當出現并發的時候,就會導致出現各種各樣奇怪的問題,就像程序代碼一樣,出現多線程并發的時候,如果不做特殊控制的話,就會出現意外的事情,比如“臟“數據、修改丟失等問題。所以數據庫并發需要使用事務來控制,事務并發問題需要數據庫鎖來控制,所以數據庫鎖是跟并發控制和事務聯系在一起的。

本文主要描述基于更新SQL語句來理解MySQL鎖定。下面話不多說了,來一起看看詳細的介紹吧

一、構造環境

(root@localhost) [user]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.7.23-log |
+---------------+------------+

(root@localhost) [user]> desc t1;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| n | int(11) | YES | | NULL | |
| table_name | varchar(64) | YES | | NULL | |
| column_name | varchar(64) | YES | | NULL | |
| pad | varchar(100) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+

(root@localhost) [user]> select count(*) from t1;
+----------+
| count(*) |
+----------+
| 3406 |
+----------+

(root@localhost) [user]> create unique index idx_t1_pad on t1(pad);
Query OK, 0 rows affected (0.35 sec)
Records: 0 Duplicates: 0 Warnings: 0

(root@localhost) [user]> create index idx_t1_n on t1(n);
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
(root@localhost) [user]> show index from t1;
+-------+------------+------------+--------------+-------------+-----------+-------------+------+------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Null | Index_type |
+-------+------------+------------+--------------+-------------+-----------+-------------+------+------------+
| t1 | 0 | PRIMARY | 1 | id | A | 3462 | | BTREE |
| t1 | 0 | idx_t1_pad | 1 | pad | A | 3406 | YES | BTREE |
| t1 | 1 | idx_t1_n | 1 | n | A | 12 | YES | BTREE |
+-------+------------+------------+--------------+-------------+-----------+-------------+------+------------+
select 'Leshami' author,'http://blog.csdn.net/leshami' Blog;
+---------+------------------------------+
| author | Blog |
+---------+------------------------------+
| Leshami | http://blog.csdn.net/leshami |
+---------+------------------------------+

二、基于主鍵更新

(root@localhost) [user]> start transaction;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [user]> update t1 set table_name='t1' where id=1299;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SELECT trx_id,
 trx_state,
 trx_started,
 trx_mysql_thread_id,
 trx_tables_locked,
 trx_rows_locked,
 trx_rows_modified,
 trx_isolation_level
FROM INFORMATION_SCHEMA.INNODB_TRX \G

-- 從下面的結果可知,trx_rows_locked,一行被鎖定 
*************************** 1. row ***************************
 trx_id: 6349647
 trx_state: RUNNING
 trx_started: 2018-11-06 16:54:12
trx_mysql_thread_id: 2
 trx_tables_locked: 1
 trx_rows_locked: 1
 trx_rows_modified: 1
trx_isolation_level: REPEATABLE READ 

(root@localhost) [user]> rollback;
Query OK, 0 rows affected (0.01 sec)

三、基于二級唯一索引

(root@localhost) [user]> start transaction;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [user]> update t1 set table_name='t2' where pad='4f39e2a03df3ab94b9f6a48c4aecdc0b';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

SELECT trx_id,
 trx_state,
 trx_started,
 trx_mysql_thread_id,
 trx_tables_locked,
 trx_rows_locked,
 trx_rows_modified,
 trx_isolation_level
FROM INFORMATION_SCHEMA.INNODB_TRX \G

-- 從下面的查詢結果可知,trx_rows_locked,2行被鎖定
*************************** 1. row ***************************
 trx_id: 6349649
 trx_state: RUNNING
 trx_started: 2018-11-06 16:55:22
trx_mysql_thread_id: 2
 trx_tables_locked: 1
 trx_rows_locked: 2
 trx_rows_modified: 1
trx_isolation_level: REPEATABLE READ 

(root@localhost) [user]> rollback;
Query OK, 0 rows affected (0.00 sec)

三、基于二級非唯一索引

(root@localhost) [user]> start transaction;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [user]> update t1 set table_name='t3' where n=8;
Query OK, 350 rows affected (0.01 sec)
Rows matched: 351 Changed: 351 Warnings: 0

SELECT trx_id,
 trx_state,
 trx_started,
 trx_mysql_thread_id,
 trx_tables_locked,
 trx_rows_locked,
 trx_rows_modified,
 trx_isolation_level
FROM INFORMATION_SCHEMA.INNODB_TRX \G
 
--從下面的查詢結果可知,703行被鎖定
*************************** 1. row ***************************
  trx_id: 6349672
  trx_state: RUNNING
 trx_started: 2018-11-06 17:06:53
trx_mysql_thread_id: 2
 trx_tables_locked: 1
 trx_rows_locked: 703
 trx_rows_modified: 351
trx_isolation_level: REPEATABLE READ

(root@localhost) [user]> rollback;
Query OK, 0 rows affected (0.00 sec)

四、無索引更新

(root@localhost) [user]> start transaction;
Query OK, 0 rows affected (0.00 sec)

(root@localhost) [user]> update t1 set table_name='t4' where column_name='id';
Query OK, 26 rows affected (0.00 sec)
Rows matched: 26 Changed: 26 Warnings: 0

SELECT trx_id,
 trx_state,
 trx_started,
 trx_mysql_thread_id,
 trx_tables_locked,
 trx_rows_locked,
 trx_rows_modified,
 trx_isolation_level
FROM INFORMATION_SCHEMA.INNODB_TRX \G

-- 從下面的查詢結果可知,trx_rows_locked,3429行被鎖定,而被更新的僅僅為26行
-- 而且這個結果超出了表上的總行數3406
*************************** 1. row ***************************
  trx_id: 6349674
  trx_state: RUNNING
 trx_started: 2018-11-06 17:09:41
trx_mysql_thread_id: 2
 trx_tables_locked: 1
 trx_rows_locked: 3429
 trx_rows_modified: 26
trx_isolation_level: REPEATABLE READ

(root@localhost) [user]> rollback;
Query OK, 0 rows affected (0.00 sec)

-- 也可以通過show engine innodb status進行觀察

show engine innodb status\G

------------
TRANSACTIONS
------------
Trx id counter 6349584
Purge done for trx's n:o < 0 undo n:o < 0 state: running but idle
History list length 0
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421943222819552, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 6349583, ACTIVE 2 sec
2 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1


------------
TRANSACTIONS
------------
Trx id counter 6349586
Purge done for trx's n:o < 6349585 undo n:o < 0 state: running but idle
History list length 1
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 421943222819552, not started
0 lock struct(s), heap size 1136, 0 row lock(s)
---TRANSACTION 6349585, ACTIVE 8 sec
3 lock struct(s), heap size 1136, 2 row lock(s), undo log entries 1
MySQL thread id 2, OS thread handle 140467640694528, query id 29 localhost root

五、鎖相關查詢SQL

1:查看當前的事務

SELECT * FROM INFORMATION_SCHEMA.INNODB_TRX;

2:查看當前鎖定的事務

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCKS;

3:查看當前等鎖的事務

SELECT * FROM INFORMATION_SCHEMA.INNODB_LOCK_WAITS;

SELECT trx_id,
 trx_state,
 trx_started,
 trx_mysql_thread_id thr_id,
 trx_tables_locked tb_lck,
 trx_rows_locked rows_lck,
 trx_rows_modified row_mfy,
 trx_isolation_level is_lvl
FROM INFORMATION_SCHEMA.INNODB_TRX;

SELECT r.`trx_id` waiting_trx_id,
 r.`trx_mysql_thread_id` waiting_thread,
 r.`trx_query` waiting_query,
 b.`trx_id` bolcking_trx_id,
 b.`trx_mysql_thread_id` blocking_thread,
 b.`trx_query` block_query
FROM information_schema.`INNODB_LOCK_WAITS` w
 INNER JOIN information_schema.`INNODB_TRX` b
 ON b.`trx_id` = w.`blocking_trx_id`
 INNER JOIN information_schema.`INNODB_TRX` r
 ON r.`trx_id` = w.`requesting_trx_id`;

六、小結

1、MySQL表更新時,對記錄的鎖定根據更新時where謂詞條件來確定鎖定范圍

2、對于聚簇索引過濾,由于索引即數據,因為僅僅鎖定更新行,這是由聚簇索引的性質決定的

3、對于非聚簇唯一索引過濾,由于需要回表,因此鎖定為唯一索引過濾行數加上回表行數

4、對于非聚簇非唯一索引過濾,涉及到了間隙鎖,因此鎖定的記錄數更多

5、如果過濾條件無任何索引或無法使用到索引,則鎖定整張表上所有數據行

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對億速云的支持。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

壤塘县| 桦南县| 增城市| 太湖县| 秀山| 翼城县| 洛宁县| 海淀区| 闵行区| 常山县| 鲁山县| 凤凰县| 枣庄市| 辽中县| 西宁市| 理塘县| 宿州市| 肃北| 潢川县| 崇明县| 巨鹿县| 元谋县| 长兴县| 区。| 辉南县| 津市市| 娱乐| 牡丹江市| 资兴市| 岑溪市| 东源县| 奎屯市| 安西县| 贡觉县| 广宗县| 阿图什市| 东乌| 山阳县| 新丰县| 光泽县| 博野县|