mysql那些事儿|mysql锁总结

Posted 迈莫coding

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql那些事儿|mysql锁总结相关的知识,希望对你有一定的参考价值。

mysql服务器没有实现。"],[20,"\n","24:\"mfbR\"|33:1|bullet-id:\"d5JP\"|bullet:\"circle\""],[20,"行级锁更适合于有大量按索引条件并发更新少量不同数据,同时又有并发查询的应用,如一些在线事务处理(OLTP)系统"],[20,"\n","24:\"U9iF\"|33:1|bullet-id:\"d5JP\"|bullet:\"circle\""],[20,"\n","24:\"6TFi\""],[20,"InnoDB共享锁和排他锁"],[20,"\n","24:\"cx63\"|32:1"],[20,"\n","24:\"tkRX\""],[20,"InnoDB实现了两种类型的"],[20,"行锁","8:1"],[20,":"],[20,"\n","24:\"fuCJ\""],[20,"\n","24:\"PWbH\""],[20,"共享锁(S): ","8:1"],[20,"允许一个事务去读一行,阻止其他事务获得相同数据集的排他锁"],[20,"\n","24:\"o6qA\"|bullet-id:\"ID7N\"|bullet:\"circle\""],[20,"排他锁(X): ","8:1"],[20,"允许获得排他锁的事务更新数据,阻止其他事务取得相同数据集的共享读锁和排他写锁"],[20,"\n","24:\"6IGf\"|bullet-id:\"ID7N\"|bullet:\"circle\""],[20,"\n","24:\"AzJv\""],[20,"为了允许行锁和表锁共存,实现多粒度锁机制,InnoDB还有两种内部使用的意向锁(Intention Locks),这两种意向锁都是"],[20,"表锁:","8:1"],[20,"\n","24:\"Uml4\""],[20,"意向共享锁(IS):事务打算给数据行加行共享锁,事务在给一个数据行加共享锁前必须先取得该表的IS锁"],[20,"\n","24:\"dyR9\"|bullet-id:\"N3EY\"|bullet:\"circle\""],[20,"意向排他锁(IX):事务打算给数据行加行排他锁,事务在给一个数据行加排他锁前必须先取得该表的IX锁"],[20,"\n","24:\"I8lq\"|bullet-id:\"N3EY\"|bullet:\"circle\""],[20,"\n","24:\"F5ru\""],[20,"InnoDB意向锁和排他锁"],[20,"\n","24:\"tXVK\"|32:1"],[20,"\n","24:\"60PM\""],[20,"意向锁是 "],[20,"InnoDB","code:true"],[20," 引擎自动加的,不需要用户干预"],[20,"\n","24:\"QnBG\"|bullet-id:\"XB1C\"|bullet:\"circle\""],[20,"对于 "],[20,"UPDATE","code:true"],[20," "],[20,"INSERT","code:true"],[20," "],[20,"DELETE","code:true"],[20," 语句,InnoDB引擎会自动给涉及数据集加排他锁(X)"],[20,"\n","24:\"y81V\"|bullet-id:\"XB1C\"|bullet:\"circle\""],[20,"对于普通 "],[20,"SELECT","code:true"],[20," 语句,InnoDB不会加任何锁"],[20,"\n","24:\"aIVH\"|bullet-id:\"XB1C\"|bullet:\"circle\""],[20,"事务可以通过以下语句显式地给结果集加共享锁或排它锁"],[20,"\n","24:\"0Bcw\"|bullet-id:\"XB1C\"|bullet:\"circle\""],[20,"共享锁(S): "],[20,"SELECT * FROM table_name WHERE ... LOCK IN SHARE MODE","code:true"],[20," 。 其他 "],[20,"session","code:true"],[20," 仍然可以查询记录,并也可以对该记录加 "],[20,"share mode","code:true"],[20," 的共享锁。但是如果当前事务需要对该记录进行更新操作,则很有可能造成死锁。"],[20,"\n","24:\"QnTO\"|33:1|bullet-id:\"XB1C\"|bullet:\"circle\""],[20,"排它锁(X): "],[20,"SELECT * FROM table_name WHERE ... FOR UPDATE","code:true"],[20," 。其他 "],[20,"session","code:true"],[20," 可以查询该记录,但是不能对该记录加共享锁或排他锁,而是等待获得锁"],[20,"\n","24:\"Riwp\"|33:1|bullet-id:\"XB1C\"|bullet:\"circle\""],[20,"\n","24:\"AZHS\""],[20,"\n","24:\"tx5y\""],[20,"InnoDB行锁"],[20,"\n","24:\"GBCN\"|32:1"],[20,"\n","24:\"uvnG\""],[20,"innoDB行锁通过索引上的索引项加锁来实现的,这一点 MySQL 与 Oracle 不同,后者是通过在数据块中对相应数据行加锁来实现的。InnoDB 这种行锁实现特点意味着:只有通过索引条件检索数据,InnoDB 才使用行级锁,否则,InnoDB 将使用表锁!"],[20,"\n","24:\"FSEx\"|bullet-id:\"8VrI\"|bullet:\"circle\""],[20,"不论是使用主键索引、唯一索引或普通索引,InnoDB 都会使用行锁来对数据加锁。","0:\"%23494949\"|27:\"11\""],[20,"\n","0:\"%23494949\"|24:\"B8QZ\"|27:\"11\"|bullet-id:\"wQwQ\"|bullet:\"circle\""],[20,"只有执行计划真正使用了索引,才能使用行锁:即便在条件中使用了索引字段,但是否使用索引来检索数据是由 MySQL 通过判断不同执行计划的代价来决定的,如果 MySQL 认为全表扫描效率更高,比如对一些很小的表,它就不会使用索引,这种情况下 InnoDB 将使用表锁,而不是行锁。","0:\"%23494949\"|27:\"11\""],[20,"\n","0:\"%23494949\"|24:\"Rnko\"|27:\"11\"|bullet-id:\"Xbqn\"|bullet:\"circle\""],[20,"由于 MySQL 的行锁是针对索引加的锁,不是针对记录加的锁,所以虽然多个session访问不同行的记录, 但是如果是使用相同的索引键, 是会出现锁冲突的(后使用这些索引的session需要等待先使用索引的session释放锁后,才能获取锁)。 应用设计的时候要注意这一点。","0:\"%23494949\"|27:\"11\""],[20,"\n","0:\"%23494949\"|24:\"CHcW\"|27:\"11\"|bullet-id:\"Wy0e\"|bullet:\"circle\""],[20,"\n","24:\"WUTp\""],[20,"InnoDB间隙锁"],[20,"\n","24:\"q2XG\"|32:1"],[20,"概念"],[20,"\n","24:\"g50E\"|32:2"],[20,"\n","24:\"xJNg\""],[20,"当我们用范围条件而不是相等条件检索数据,并请求共享或排他锁时,InnoDB会给符合条件的已有数据记录的索引项加锁;对于键值在条件范围内但并不存在的记录,叫做“间隙(GAP)”,InnoDB也会对这个“间隙”加锁,这种锁机制就是所谓的间隙锁(Next-Key锁)。"],[20,"\n","24:\"ch5S\""],[20,"\n","24:\"ZWPL\""],[20,"\n","24:\"3oHD\""],[20,"很显然,在使用范围条件检索并锁定记录时,InnoDB这种加锁机制会阻塞符合条件范围内键值的并发插入,这往往会造成严重的锁等待。因此,在实际应用开发中,尤其是并发插入比较多的应用,我们要尽量优化业务逻辑,尽量使用相等条件来访问更新数据,避免使用范围条件。"],[20,"\n","24:\"1JkB\""],[20,"\n","24:\"dE34\""],[20,"InnoDB使用间隙锁的目的:"],[20,"\n","24:\"XGwi\"|32:2"],[20,"\n","24:\"uHS8\""],[20,"防止幻读,以满足相关隔离级别的要求;满足恢复和复制的需要"],[20,"\n","24:\"ZK5q\"|bullet-id:\"lRL4\"|bullet:\"circle\""],[20,"MySQL 通过 "],[20,"BINLOG","code:true"],[20," 录入执行成功的 "],[20,"INSERT","code:true"],[20," 、 "],[20,"UPDATE","code:true"],[20," 、 "],[20,"DELETE","code:true"],[20," 等更新数据的 SQL 语句,并由此实现 MySQL 数据库的恢复和主从复制。MySQL 的恢复机制(复制其实就是在 Slave Mysql 不断做基于 BINLOG 的恢复)有以下特点:"],[20,"\n","24:\"q5I0\"|bullet-id:\"udq3\"|bullet:\"circle\""],[20,"一是 MySQL 的恢复是 SQL 语句级的,也就是重新执行 "],[20,"BINLOG","code:true"],[20," 中的 SQL 语句。"],[20,"\n","24:\"qsUh\"|33:1|bullet-id:\"Qu2k\"|bullet:\"circle\""],[20,"二是 MySQL 的 "],[20,"Binlog","code:true"],[20," 是按照事务提交的先后顺序记录的, 恢复也是按这个顺序进行的。"],[20,"\n","24:\"coLw\"|33:1|bullet-id:\"gOjo\"|bullet:\"circle\""],[20,"\n","24:\"BmYR\""],[20,"由此可见,"],[20,"MySQL 的恢复机制","8:1"],[20,"要求:在一个事务未提交前,其他并发事务不能插入满足其锁定条件的任何记录,也就是不允许出现幻读。"],[20,"\n","24:\"FM2J\""],[20,"\n","24:\"TuhU\""],[20,"InnoDB加锁方式"],[20,"\n","24:\"cSvL\"|32:1"],[20,"\n","24:\"9F8I\""],[20,"隐式锁定:","27:\"11\"|8:1"],[20,"\n","24:\"3CKn\"|27:\"11\"|bullet-id:\"AoWl\"|bullet:\"circle\""],[20,"\n","24:\"KVij\""],[20,"InnoDB在事务执行过程中,使用两阶段锁协议:"],[20,"\n","24:\"4Cb0\""],[20,"\n","24:\"4hA3\""],[20,"随时都可以执行锁定,InnoDB会根据隔离级别在需要的时候自动加锁;"],[20,"\n","24:\"gDyt\""],[20,"\n","24:\"X1in\""],[20,"锁只有在执行commit或者rollback的时候才会释放,并且所有的锁都是在同一时刻被释放。"],[20,"\n","24:\"wPB0\""],[20,"\n","24:\"aoLf\""],[20,"显式锁定 :","27:\"11\"|8:1"],[20,"\n","24:\"w2rP\"|27:\"11\"|bullet-id:\"34qI\"|bullet:\"circle\""],[20,"\n","24:\"meH8\""],[20,"select ... lock in share mode //共享锁 "],[20,"\n","24:\"xWrL\"|36:177"],[20,"select ... for update //排他锁 "],[20,"\n","24:\"cMYQ\"|36:177"],[20,"\n","24:\"5omk\""],[20,"select for update:","8:1"],[20,"\n","24:\"qwkN\"|bullet-id:\"tuNV\"|bullet:\"circle\""],[20,"\n","24:\"oFkr\""],[20,"在执行这个 select 查询语句的时候,会将对应的索引访问条目进行上排他锁(X 锁),也就是说这个语句对应的锁就相当于update带来的效果。"],[20,"\n","24:\"JWjD\""],[20,"\n","24:\"piZc\""],[20,"select *** for update 的使用场景","8:1"],[20,"\n","24:\"rGPq\"|bullet-id:\"xBKl\"|bullet:\"circle\""],[20,"\n","24:\"vEMx\""],[20,"为了让自己查到的数据确保是最新数据,并且查到后的数据只允许自己来修改的时候,需要用到 for update 子句。"],[20,"\n","24:\"2wei\""],[20,"\n","24:\"L1vX\""],[20,"select lock in share mode ","8:1"],[20,"\n","24:\"XYFi\"|bullet-id:\"ZXws\"|bullet:\"circle\""],[20,"\n","24:\"7KTZ\""],[20,"in share mode 子句的作用就是将查找到的数据加上一个 share 锁,这个就是表示其他的事务只能对这些数据进行简单的select 操作,并不能够进行 DML 操作。"],[20,"\n","24:\"Upty\""],[20,"\n","24:\"7tXi\""],[20,"select *** lock in share mode 使用场景","8:1"],[20,"\n","24:\"goUu\"|bullet-id:\"R7fV\"|bullet:\"circle\""],[20,"\n","24:\"Qzif\""],[20,"为了确保自己查到的数据没有被其他的事务正在修改,也就是说确保查到的数据是最新的数据,并且不允许其他人来修改数据。但是自己不一定能够修改数据,因为有可能其他的事务也对这些数据 使用了 in share mode 的方式上了 S 锁。"],[20,"\n","24:\"zjQW\""],[20,"\n","24:\"l5I0\""],[20,"性能影响","8:1"],[20,"\n","24:\"4A21\"|bullet-id:\"8gsR\"|bullet:\"circle\""],[20,"\n","24:\"9ONK\""],[20,"select for update 语句,相当于一个 update 语句。在业务繁忙的情况下,如果事务没有及时的commit或者rollback 可能会造成其他事务长时间的等待,从而影响数据库的并发使用效率。","27:\"11\""],[20,"\n","24:\"qx8e\"|27:\"11\"|33:1|bullet-id:\"9lfC\"|bullet:\"circle\""],[20,"select lock in share mode 语句是一个给查找的数据上一个共享锁(S 锁)的功能,它允许其他的事务也对该数据上S锁,但是不能够允许对该数据进行修改。如果不及时的commit 或者rollback 也可能会造成大量的事务等待。","27:\"12\"|31:2"],[20," "],[20,"\n","24:\"qzQl\"|33:1|bullet-id:\"n9IQ\"|bullet:\"circle\""],[20,"\n","24:\"f5D1\""],[20,"闲聊"],[20,"\n","24:\"DKQA\"|32:1"],[20,"读完文章,自己是不是和mysql锁的cp率又提高了"],[20,"\n","24:\"xfkm\"|bullet-id:\"56MS\"|bullet:\"circle\""],[20,"我是迈莫,欢迎大家和我交流"],[20,"\n","24:\"VxbB\"|bullet-id:\"56MS\"|bullet:\"circle\""],[20,"原创不易,觉得文章写得不错的小伙伴,点个赞

以上是关于mysql那些事儿|mysql锁总结的主要内容,如果未能解决你的问题,请参考以下文章

mysql索引失效相关那些事儿

mysql索引失效相关那些事儿

mysql索引失效相关那些事儿

MySQL的那些事儿

关于用好MySQL索引的那些事儿

后端优化那些事儿~ MySQL优化第6天