MySQL行級鎖測試
MySQL innodb儲存引擎使用與Oracle相同的行鎖機制,對如何檢視系統中存在的行鎖情況在下面的實驗中將會看到。下面是測試過程:
session 1:更新記錄
mysql> set autocommit=off;
Query OK, 0 rows affected (0.01 sec)
mysql> update t1 set email='test@test.com' where id=0;
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
session 2:也更新相同的記錄,出現等待
mysql> set autocommit=off;
Query OK, 0 rows affected (0.00 sec)
mysql> update t1 set email='abc' where id=0;
session 3:檢視系統等待事件:
mysql> show status like '%lock%';
+-------------------------------+---------+
| Variable_name | Value |
+-------------------------------+---------+
| Com_lock_tables | 0 |
| Com_unlock_tables | 0 |
| Innodb_row_lock_current_waits | 1 | --這裡
| Innodb_row_lock_time | 0 |
| Innodb_row_lock_time_avg | 0 |
| Innodb_row_lock_time_max | 0 |
| Innodb_row_lock_waits | 1 |
| Key_blocks_not_flushed | 0 |
| Key_blocks_unused | 14497 |
| Key_blocks_used | 0 |
| Qcache_free_blocks | 1 |
| Qcache_total_blocks | 1 |
| Table_locks_immediate | 2070991 |
| Table_locks_waited | 2 |
+-------------------------------+---------+
14 rows in set (0.01 sec)
session 1:提交記錄
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
session 2:update立刻完成
mysql> update t1 set email='abc' where id=0;
Query OK, 4 rows affected (2 min 43.44 sec) --這麼長時間完成更新操作
Rows matched: 4 Changed: 4 Warnings: 0
session 3:再次檢視系統等待事件
mysql> show status like '%lock%';
+-------------------------------+---------+
| Variable_name | Value |
+-------------------------------+---------+
| Com_lock_tables | 0 |
| Com_unlock_tables | 0 |
| Innodb_row_lock_current_waits | 0 | --這裡為0
| Innodb_row_lock_time | 163436 |
| Innodb_row_lock_time_avg | 163436 |
| Innodb_row_lock_time_max | 163436 |
| Innodb_row_lock_waits | 1 |
| Key_blocks_not_flushed | 0 |
| Key_blocks_unused | 14497 |
| Key_blocks_used | 0 |
| Qcache_free_blocks | 1 |
| Qcache_total_blocks | 1 |
| Table_locks_immediate | 2070991 |
| Table_locks_waited | 2 |
+-------------------------------+---------+
14 rows in set (0.01 sec)
查詢會話session 1,session 2的連線ID
session 1:
mysql> status;
--------------
mysql Ver 12.22 Distrib 4.0.24, for pc-solaris2.10 (i386)
Connection id: 15
session 2:
mysql> status;
--------------
mysql Ver 12.22 Distrib 4.0.24, for pc-solaris2.10 (i386)
Connection id: 13
在上面的session 1尚沒有提交的時候,可以執行下列命令,檢視一些事務阻塞資訊
mysql> show innodb status\G;
------------
TRANSACTIONS
------------
Trx id counter 0 3852351
Purge done for trx's n:o < 0 3852350 undo n:o < 0 0
History list length 11
Total number of lock structs in row lock hash table 7
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0 0, not started, OS thread id 15
MySQL thread id 18, query id 2071119 localhost root
show innodb status
---TRANSACTION 0 3852350, ACTIVE 6 sec, OS thread id 14 starting index read
mysql tables in use 1, locked 1
LOCK WAIT 2 lock struct(s), heap size 320
MySQL thread id 13, query id 2071118 localhost test Updating --這裡可以看到等待者
update t1 set email='abc' where id=0 --這裡可以看到等待者正在執行的SQL
------- TRX HAS BEEN WAITING 6 SEC FOR THIS LOCK TO BE GRANTED:
RECORD LOCKS space id 0 page no 32782 n bits 1056 index `idx_t1_id` of table `dc_test/t1` trx id 0 3852350 lock_mode X waiting
Record lock, heap no 2 PHYSICAL RECORD: n_fields 2; compact format; info bits 0
0: len 4; hex 80000000; asc ;; 1: len 6; hex 00000196fe5d; asc ];;
------------------
---TRANSACTION 0 3852348, ACTIVE 391 sec, OS thread id 12
7 lock struct(s), heap size 1024, undo log entries 4
MySQL thread id 15, query id 2071117 localhost test
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9034054/viewspace-1973727/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL 5.5 InnoDB表鎖行鎖測試MySql
- 你真的會用mysql行級鎖嗎?mysql 行級鎖全解析MySql
- MySQL複習筆記(05):MySQL表級鎖和行級鎖MySql筆記
- mysql行鎖和死鎖檢測MySql
- 測試MySQL鎖的問題MySql
- MySQL 5.5 MyISAM表鎖測試MySql
- MySQL 行級鎖之 間隙鎖、臨鍵鎖MySql
- oracle鎖級別相關測試Oracle
- mysql鎖之三種行級鎖介紹MySql
- mysql鎖 實戰測試程式碼MySql
- mysql 行級鎖(按照粒度分類)MySql
- Mysql鎖之行級鎖和表級意向鎖MySql
- MySQL 行級鎖的特點、分類MySql
- MySQL 5.5 隔離級別測試MySql
- MySQL 避免行鎖升級為表鎖——使用高效的索引MySql索引
- MySQL 行級鎖的使用以及死鎖的預防MySql
- Mysql在InnoDB引擎下索引失效行級鎖變表鎖案例MySql索引
- MySQL中的事務和鎖簡單測試MySql
- MySQL -- 行鎖MySql
- MySQL 行鎖MySql
- 執行database replay進行升級測試Database
- oracle封鎖測試Oracle
- MySQL鎖:03.InnoDB行鎖MySql
- MySQL全域性鎖、表鎖以及行鎖MySql
- 執行緒和鎖,鎖升級執行緒
- Oracle的TX鎖(行級鎖、事務鎖)Oracle
- JMeter 如何與 MySQL 進行整合測試JMeterMySql
- 【MySQL】利用sysbench進行基準測試MySql
- 測試物件和測試級別物件
- 使用tpcc-mysql對mysql進行TPCC效能測試MySql
- MySQL索引失效行鎖變表鎖MySql索引
- mysql innodb的行鎖MySql
- MySQL中InnoDB鎖機制介紹及一些測試MySql
- 利用sysbench進行MySQL OLTP基準測試MySql
- 用mysqlslap對MySQL進行壓力測試MySql
- sysbench 測試MySQLMySql
- mysql測試shellMySql
- MySQL鎖:InnoDB行鎖需要避免的坑MySql