程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MYSQL的偽行級鎖

MYSQL的偽行級鎖

編輯:MySQL綜合教程


MYSQL的偽行級鎖   之前一直以為mysql的innodb引擎所支持的行級鎖和oracle,postgresql是一樣的,是對數據行上加鎖。但其實是不一樣的,理解不一樣,對mysql的鎖機制就容易產生誤解。innodb的行級鎖實際上是基於索引項來鎖定的。以下是驗證測試過程    一.數據准備 mysql> use test; Database changed mysql> show create table t_kenyon \G *************************** 1. row ***************************        Table: t_kenyon Create Table: CREATE TABLE `t_kenyon` (   `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)   mysql> set autocommit = 0; Query OK, 0 rows affected (0.00 sec)   www.2cto.com   mysql> show variables like '%autocommit%'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit    | OFF   | +---------------+-------+ 1 row in set (0.00 sec)   mysql> select * from t_kenyon; +------+ | id   | +------+ |    1 | |  123 | |  789 | |  345 | |   78 | |   78 | +------+ 6 rows in set (0.00 sec) 以上是測試表t_kenyon,設置提交方式為手動提交.    二.過程(開啟兩個session,分別設置autocommit=off)    www.2cto.com   1.session one update mysql> update t_kenyon set id = 999 where id = 1; Query OK, 1 row affected (0.04 sec) Rows matched: 1  Changed: 1  Warnings: 0   mysql> select * from t_kenyon; +------+ | id   | +------+ |  999 | |  123 | |  789 | |  345 | |   78 | |   78 | +------+ 6 rows in set (0.00 sec) 2.session two update mysql> show variables like 'autocommit'; +---------------+-------+ | Variable_name | Value | +---------------+-------+ | autocommit    | OFF   | +---------------+-------+ 1 row in set (0.00 sec)   www.2cto.com   mysql> select * from t_kenyon; +------+ | id   | +------+ |    1 | |  123 | |  789 | |  345 | |   78 | |   78 | +------+ 6 rows in set (0.00 sec)   mysql> update t_kenyon set id = 88888 where id = 345; 第二個session更新的值是345,但是也一直被阻塞,直到session1被rollback或者commit,如果session1未做回滾或者提交,session2中的該阻塞在超出mysql的鎖時間限制時自動回滾,該參數為innodb_lock_wait_timeout,默認值50秒 現象如下 ERROR 1205 (HY000): Lock wait timeout exceeded; try restarting transaction 加索引後的測試  3.session one update   mysql> create index ind_kenyon on t_kenyon(id); Query OK, 0 rows affected (28.58 sec) Records: 0  Duplicates: 0  Warnings: 0   www.2cto.com   mysql> update t_kenyon set id = 999 where id = 1; Query OK, 1 row affected (0.03 sec) Rows matched: 1  Changed: 1  Warnings: 0   mysql> select * from t_kenyon; +------+ | id   | +------+ |   78 | |   78 | |  123 | |  345 | |  789 | |  999 | +------+ 6 rows in set (0.00 sec) 4.session two update  mysql> select * from t_kenyon; +------+ | id   | +------+ |    1 | |   78 | |   78 | |  123 | |  345 | |  789 | +------+ 6 rows in set (0.00 sec)   www.2cto.com   mysql> update t_kenyon set id = 7777 where id = 345; Query OK, 1 row affected (0.03 sec) Rows matched: 1  Changed: 1  Warnings: 0   mysql> select * from t_kenyon; +------+ | id   | +------+ |    1 | |   78 | |   78 | |  123 | |  789 | | 7777 | +------+ 6 rows in set (0.00 sec) 執行計劃 mysql> explain select * from t_kenyon where id = 345 \G *************************** 1. row ***************************            id: 1  www.2cto.com     select_type: SIMPLE         table: t_kenyon          type: ref possible_keys: ind_kenyon           key: ind_kenyon       key_len: 5           ref: const          rows: 1         Extra: Using where; Using index 1 row in set (0.00 sec) 可以看到加了索引後,不同的數據更新並沒有被阻塞,實現了真正意義上行鎖  三.行級鎖的擴展限制  參考:http://www.BkJia.com/database/201208/145888.html     作者 kenyon

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved