• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            兔子的技術博客

            兔子

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              202 Posts :: 0 Stories :: 43 Comments :: 0 Trackbacks

            留言簿(10)

            最新評論

            閱讀排行榜

            評論排行榜

            摘自 

            《深入淺出MySQL——數據庫開發、優化與管理維護》

            20.3.3 InnoDB的行鎖模式及加鎖方法

            InnoDB實現了以下兩種類型的行鎖。 

            ? 共享鎖(S):允許一個事務去讀一行,阻止其他事務獲得相同數據集的排他鎖。

            ? 排他鎖(X):允許獲得排他鎖的事務更新數據,阻止其他事務取得相同數據集的共享讀鎖和排他寫鎖。

            另外,為了允許行鎖和表鎖共存,實現多粒度鎖機制,InnoDB還有兩種內部使用的意向鎖(Intention Locks),這兩種意向鎖都是表鎖。

            ? 意向共享鎖(IS):事務打算給數據行加行共享鎖,事務在給一個數據行加共享鎖前必須先取得該表的IS鎖。

            ? 意向排他鎖(IX):事務打算給數據行加行排他鎖,事務在給一個數據行加排他鎖前必須先取得該表的IX鎖。

            上述鎖模式的兼容情況具體如表20-6所示。

            表20-6     InnoDB行鎖模式兼容性列表

            請求鎖模式

             

               是否兼容

             

            當前鎖模式

            X

            IX

            S

            IS

            X

            沖突

            沖突

            沖突

            沖突

            IX

            沖突

            兼容

            沖突

            兼容

            S

            沖突

            沖突

            兼容

            兼容

            IS

            沖突

            兼容

            兼容

            兼容

            如果一個事務請求的鎖模式與當前的鎖兼容,InnoDB就將請求的鎖授予該事務;反之,如果兩者不兼容,該事務就要等待鎖釋放。

            意向鎖是InnoDB自動加的,不需用戶干預。對于UPDATE、DELETE和INSERT語句,InnoDB會自動給涉及數據集加排他鎖(X);對于普通SELECT語句,InnoDB不會加任何鎖;事務可以通過以下語句顯示給記錄集加共享鎖或排他鎖。

            ·共享鎖(S):SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE。

            ·排他鎖(X):SELECT * FROM table_name WHERE ... FOR UPDATE。

            用SELECT ... IN SHARE MODE獲得共享鎖,主要用在需要數據依存關系時來確認某行記錄是否存在,并確保沒有人對這個記錄進行UPDATE或者DELETE操作。但是如果當前事務也需要對該記錄進行更新操作,則很有可能造成死鎖,對于鎖定行記錄后需要進行更新操作的應用,應該使用SELECT... FOR UPDATE方式獲得排他鎖。

            在如表20-7所示的例子中,使用了SELECT ... IN SHARE MODE加鎖后再更新記錄,看看會出現什么情況,其中actor表的actor_id字段為主鍵。

            表20-7           InnoDB存儲引擎的共享鎖例子

            session_1

            session_2

            mysql> set autocommit = 0;

            Query OK, 0 rows affected (0.00 sec)

             

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.00 sec)

            mysql> set autocommit = 0;

            Query OK, 0 rows affected (0.00 sec)

             

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.00 sec)

            當前session對actor_id=178的記錄加share mode 的共享鎖:

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178 lock in share mode;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.01 sec)

             

             

            其他session仍然可以查詢記錄,并也可以對該記錄加share mode的共享鎖:

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178 lock in share mode;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.01 sec)

            當前session對鎖定的記錄進行更新操作,等待鎖:

            mysql> update actor set last_name = 'MONROE T' where actor_id = 178;

            等待

             

             

            其他session也對該記錄進行更新操作,則會導致死鎖退出:

            mysql> update actor set last_name = 'MONROE T' where actor_id = 178;

            ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

            獲得鎖后,可以成功更新:

            mysql> update actor set last_name = 'MONROE T' where actor_id = 178;

            Query OK, 1 row affected (17.67 sec)

            Rows matched: 1  Changed: 1 Warnings: 0

             

            當使用SELECT...FOR UPDATE加鎖后再更新記錄,出現如表20-8所示的情況。

            表20-8             InnoDB存儲引擎的排他鎖例子

            session_1

            session_2

            mysql> set autocommit = 0;

            Query OK, 0 rows affected (0.00 sec)

             

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.00 sec)

            mysql> set autocommit = 0;

            Query OK, 0 rows affected (0.00 sec)

             

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.00 sec)

            當前session對actor_id=178的記錄加for update的共享鎖:

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178 for update;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.00 sec)

             

             

            其他session可以查詢該記錄,但是不能對該記錄加共享鎖,會等待獲得鎖:

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE   |

            +----------+------------+-----------+

            1 row in set (0.00 sec)

             

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178 for update;

            等待

            當前session可以對鎖定的記錄進行更新操作,更新后釋放鎖:

            mysql> update actor set last_name = 'MONROE T' where actor_id = 178;

            Query OK, 1 row affected (0.00 sec)

            Rows matched: 1  Changed: 1 Warnings: 0

             

            mysql> commit;

            Query OK, 0 rows affected (0.01 sec)

             

             

            其他session獲得鎖,得到其他session提交的記錄:

            mysql> select actor_id,first_name,last_name from actor where actor_id = 178 for update;

            +----------+------------+-----------+

            | actor_id | first_name | last_name |

            +----------+------------+-----------+

            | 178      | LISA       | MONROE T |

            +----------+------------+-----------+

            1 row in set (9.59 sec)

             

             20.3.4 InnoDB行鎖實現方式

            InnoDB行鎖是通過給索引上的索引項加鎖來實現的,這一點MySQL與Oracle不同,后者是通過在數據塊中對相應數據行加鎖來實現的。InnoDB這種行鎖實現特點意味著:只有通過索引條件檢索數據,InnoDB才使用行級鎖,否則,InnoDB將使用表鎖!

            在實際應用中,要特別注意InnoDB行鎖的這一特性,不然的話,可能導致大量的鎖沖突,從而影響并發性能。下面通過一些實際例子來加以說明。

            (1)在不通過索引條件查詢的時候,InnoDB確實使用的是表鎖,而不是行鎖。

            在如表20-9所示的例子中,開始tab_no_index表沒有索引:

            mysql> create table tab_no_index(id int,name varchar(10)) engine=innodb;
            Query OK, 0 rows affected (0.15 sec)
            mysql> insert into tab_no_index values(1,'1'),(2,'2'),(3,'3'),(4,'4');
            Query OK, 4 rows affected (0.00 sec)
            Records: 4  Duplicates: 0  Warnings: 0

            表20-9         InnoDB存儲引擎的表在不使用索引時使用表鎖例子

            session_1

            session_2

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> select * from tab_no_index where id = 1 ;

            +------+------+

            | id   | name |

            +------+------+

            | 1    | 1    |

            +------+------+

            1 row in set (0.00 sec)

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> select * from tab_no_index where id = 2 ;

            +------+------+

            | id   | name |

            +------+------+

            | 2    | 2    |

            +------+------+

            1 row in set (0.00 sec)

            mysql> select * from tab_no_index where id = 1 for update;

            +------+------+

            | id   | name |

            +------+------+

            | 1    | 1    |

            +------+------+

            1 row in set (0.00 sec)

             

             

            mysql> select * from tab_no_index where id = 2 for update;

            等待

            在如表20-9所示的例子中,看起來session_1只給一行加了排他鎖,但session_2在請求其他行的排他鎖時,卻出現了鎖等待!原因就是在沒有索引的情況下,InnoDB只能使用表鎖。當我們給其增加一個索引后,InnoDB就只鎖定了符合條件的行,如表20-10所示。

            創建tab_with_index表,id字段有普通索引:

            mysql> create table tab_with_index(id int,name varchar(10)) engine=innodb;
            Query OK, 0 rows affected (0.15 sec)
            mysql> alter table tab_with_index add index id(id);
            Query OK, 4 rows affected (0.24 sec)
            Records: 4  Duplicates: 0  Warnings: 0

            表20-10    InnoDB存儲引擎的表在使用索引時使用行鎖例子

            session_1

            session_2

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> select * from tab_with_index where id = 1 ;

            +------+------+

            | id   | name |

            +------+------+

            | 1    | 1    |

            +------+------+

            1 row in set (0.00 sec)

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> select * from tab_with_index where id = 2 ;

            +------+------+

            | id   | name |

            +------+------+

            | 2    | 2    |

            +------+------+

            1 row in set (0.00 sec)

            mysql> select * from tab_with_index where id = 1 for update;

            +------+------+

            | id   | name |

            +------+------+

            | 1    | 1    |

            +------+------+

            1 row in set (0.00 sec)

             

             

            mysql> select * from tab_with_index where id = 2 for update;

            +------+------+

            | id   | name |

            +------+------+

            | 2    | 2    |

            +------+------+

            1 row in set (0.00 sec)

            (2)由于MySQL的行鎖是針對索引加的鎖,不是針對記錄加的鎖,所以雖然是訪問不同行的記錄,但是如果是使用相同的索引鍵,是會出現鎖沖突的。應用設計的時候要注意這一點。

            在如表20-11所示的例子中,表tab_with_index的id字段有索引,name字段沒有索引:

            mysql> alter table tab_with_index drop index name;
            Query OK, 4 rows affected (0.22 sec)
            Records: 4  Duplicates: 0  Warnings: 0
            mysql> insert into tab_with_index  values(1,'4');
            Query OK, 1 row affected (0.00 sec)
            mysql> select * from tab_with_index where id = 1;
            +------+------+
            | id   | name |
            +------+------+
            | 1    | 1    |
            | 1    | 4    |
            +------+------+
            2 rows in set (0.00 sec)

            表20-11    InnoDB存儲引擎使用相同索引鍵的阻塞例子

            session_1

            session_2

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> select * from tab_with_index where id = 1 and name = '1' for update;

            +------+------+

            | id   | name |

            +------+------+

            | 1    | 1    |

            +------+------+

            1 row in set (0.00 sec)

             

             

            雖然session_2訪問的是和session_1不同的記錄,但是因為使用了相同的索引,所以需要等待鎖:

            mysql> select * from tab_with_index where id = 1 and name = '4' for update;

            等待

            (3)當表有多個索引的時候,不同的事務可以使用不同的索引鎖定不同的行,另外,不論是使用主鍵索引、唯一索引或普通索引,InnoDB都會使用行鎖來對數據加鎖。

            在如表20-12所示的例子中,表tab_with_index的id字段有主鍵索引,name字段有普通索引:

            mysql> alter table tab_with_index add index name(name);
            Query OK, 5 rows affected (0.23 sec)
            Records: 5  Duplicates: 0  Warnings: 0

            表20-12    InnoDB存儲引擎的表使用不同索引的阻塞例子

            ·          session_1

            ·          session_2

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> set autocommit=0;

            Query OK, 0 rows affected (0.00 sec)

            mysql> select * from tab_with_index where id = 1 for update;

            +------+------+

            | id   | name |

            +------+------+

            | 1    | 1    |

            | 1    | 4    |

            +------+------+

            2 rows in set (0.00 sec)

             

             

            Session_2使用name的索引訪問記錄,因為記錄沒有被索引,所以可以獲得鎖:

            mysql> select * from tab_with_index where name = '2' for update;

            +------+------+

            | id   | name |

            +------+------+

            | 2    | 2    |

            +------+------+

            1 row in set (0.00 sec)

             

            由于訪問的記錄已經被session_1鎖定,所以等待獲得鎖。:

            mysql> select * from tab_with_index where name = '4' for update;

            (4)即便在條件中使用了索引字段,但是否使用索引來檢索數據是由MySQL通過判斷不同執行計劃的代價來決定的,如果MySQL認為全表掃描效率更高,比如對一些很小的表,它就不會使用索引,這種情況下InnoDB將使用表鎖,而不是行鎖。因此,在分析鎖沖突時,別忘了檢查SQL的執行計劃,以確認是否真正使用了索引。關于MySQL在什么情況下不使用索引的詳細討論,參見本章“索引問題”一節的介紹。

            在下面的例子中,檢索值的數據類型與索引字段不同,雖然MySQL能夠進行數據類型轉換,但卻不會使用索引,從而導致InnoDB使用表鎖。通過用explain檢查兩條SQL的執行計劃,我們可以清楚地看到了這一點。

            例子中tab_with_index表的name字段有索引,但是name字段是varchar類型的,如果where條件中不是和varchar類型進行比較,則會對name進行類型轉換,而執行的全表掃描。

            mysql> alter table tab_no_index add index name(name);
            Query OK, 4 rows affected (8.06 sec)
            Records: 4  Duplicates: 0  Warnings: 0
            mysql> explain select * from tab_with_index where name = 1 \G
            *************************** 1. row ***************************
            id: 1
            select_type: SIMPLE
            table: tab_with_index
            type: ALL
            possible_keys: name
            key: NULL
            key_len: NULL
            ref: NULL
            rows: 4
            Extra: Using where
            1 row in set (0.00 sec)
            mysql> explain select * from tab_with_index where name = '1' \G
            *************************** 1. row ***************************
            id: 1
            select_type: SIMPLE
            table: tab_with_index
            type: ref
            possible_keys: name
            key: name
            key_len: 23
            ref: const
            rows: 1
            Extra: Using where
            1 row in set (0.00 sec)

            標簽: Mysql事務
            轉自:
            http://www.cnblogs.com/zhizhesky/articles/2164089.html
            posted on 2012-10-15 11:59 會飛的兔子 閱讀(468) 評論(0)  編輯 收藏 引用 所屬分類: 數據庫,MIS系統
            蜜臀av性久久久久蜜臀aⅴ麻豆| 97久久超碰成人精品网站| 国产毛片久久久久久国产毛片 | 久久无码国产| 久久久久99这里有精品10| 久久久噜噜噜久久熟女AA片| 丁香久久婷婷国产午夜视频| 综合久久精品色| 久久综合九色综合欧美狠狠| 亚洲国产精品成人AV无码久久综合影院 | 久久毛片免费看一区二区三区| 亚洲国产成人精品91久久久 | 久久亚洲中文字幕精品有坂深雪| 久久成人国产精品二三区| 久久综合成人网| 69SEX久久精品国产麻豆| 中文字幕精品无码久久久久久3D日动漫 | 久久婷婷国产综合精品| 亚洲国产精品久久久久婷婷老年 | 亚洲欧美伊人久久综合一区二区| 国产99精品久久| 久久国语露脸国产精品电影| 精品无码久久久久久久久久| 久久国产精品成人片免费| 久久久久亚洲AV成人网人人网站 | 国产精品嫩草影院久久| 欧美黑人激情性久久| 波多野结衣久久精品| 精品多毛少妇人妻AV免费久久| 久久精品国产亚洲AV高清热| 亚洲午夜久久久久妓女影院 | 久久亚洲sm情趣捆绑调教| 国产福利电影一区二区三区,免费久久久久久久精 | 亚洲va国产va天堂va久久| 亚洲精品高清一二区久久| 欧美激情精品久久久久久久| 狠狠色伊人久久精品综合网 | 99久久人妻无码精品系列蜜桃| 久久夜色精品国产亚洲| 亚洲国产精品一区二区三区久久| 久久亚洲精品无码观看不卡|