Baronwrote nice article comparing locking hints in MySQL and SQL Server.

In MySQL/Innodb LOCK IN SHARE MODE and SELECT FOR UPDATE are more than hints. Behavior will be different from normal SELECT statements. Here is simple example:

 SESSION1:
mysql> begin;
Query OK, 0 rows affected (0.00 sec)
mysql> insert into tst values(1);
Query OK, 1 row affected (0.00 sec)
SESSION2:
mysql> begin ;
Query OK, 0 rows affected (0.00 sec)
mysql> select * from tst;
Empty set (0.01 sec)
#Session2 does not see any rows as transaction was not commited yet.
SESSION1:
mysql> commit;
Query OK, 0 rows affected (0.01 sec)
SESSION2:
mysql> select * from tst;
Empty set (0.00 sec)
mysql> select * from tst lock in share mode;
+---+
| i |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql> select * from tst for update;
+---+
| i |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
#Standard SELECT does not see rows while SELECT for UPDATE and LOCK IN SHARE MODE sees it.

What is happening ? SELECT for UPDATE and LOCK IN SHARE MODE modifiers effectively run in READ-COMMITTED isolation mode even if current isolation mode is REPEATABLE-READ. This is done beause Innodb can only lock current version of row. Think about similar case and row being deleted. Even if Innodb would be able to set locks on rows which no more exist – would it do any good for you ? Not really – for example you could try to update the row which you just locked with SELECT FOR UPDATE but this row is already gone so you would get quite unexpected error updating the row which you thought you locked successfully. Anyway it is done this way for good all other decisions would be even more troublesome. This complexity is what you have to pay for multiversioning.

Lets also think how these modifiers can be user and what do we expect from them in practice. LOCK IN SHARE MODE is actually often used to bypass multiversioning and make sure we’re reading most current data, plus to ensure it can’t be changed. This for example can be used to read set of the rows, compute new values for some of them and write them back. If we would not use LOCK IN SHARE MODE we could be in trouble as rows could be update before we write new values to them and such update could be lost. Note I said some of them. If you want to read set of rows and modify all of them you may chose to use SELECT FOR UPDATE. This will ensure you get write locks for all rows at once which reduces chance of deadlocks – lock will not need to be upgraded when update happens. SELECT FOR UPDATE also blocks access to the data using LOCK IN SHARE MODE. So by using these two modifiers you may effectively implement instant data invalidation – using SELECT FOR UPDATE to quickly lock data which is no more correct so it is not used while you recompute it. Note it also works if LOCK IN SHARE MODE is used with selects – standard selects are run in non-locking mode which means they never lock any rows and just use old row versions if they were updated.

All said above applies to default REPEATABLE-READ mode. With different isolation modes there could be some differences but logic stills the same.

So these hints are very powerful and helpful for application development but should be used wisely. Do not assume you can simply add SELECT FOR UPDATE to your select and reduce deadlocks if you’re updating selected rows. As query results may chance you need to access how it affects your application and perform changes required.

What is missing in Innodb locking.? In my oppinion few rather important peices missing in Locking implementation of Innodb are:

Lock table Innodb can lock tables but it will still need to set row level locks which is memory and CPU overhead. For some bulk operations it would be more efficient to use table locks. As I tested it really takes some resources.

Unlocking non matched rows Imagine you’re running DELETE FROM USERS WHERE NAME LIKE “%Heikki%”; How any rows do you think will be locked ? Actually all of them, not only ones which are matched by like because locks are taken on Innodb level before MySQL performs like matching, and row is not unlocked if it does not match.

Smarter deadlock victum scheduling At this point transaction which made least updates is killed to resolve deadlock. Which means if transaction takes a lot of locks but does not do much updates it may never have chance to complete. The best example would be
INSERT INTO MyISAMTable SELECT * FROM INNODBTable; – A lot of shared locks on Innodb table but no updates. Supporting MySQL hints
“HIGH_PRIORITY” and “LOW_PRIORITY” would probably be good start.

参考:

http://www.mysqlperformanceblog.com/2006/08/06/select-lock-in-share-mode-and-for-update/

SELECT LOCK IN SHARE MODE and FOR UPDATE的更多相关文章

  1. SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE locks在RR模式下可以看到最新的记录

    14.5.2.4 Locking Reads 锁定读: 如果你查询数据然后插入或者修改相关数据在相同的事务里, 常规的SELECT 语句不能给予足够的保护. 其他事务可以修改或者删除你刚查询相同的记录 ...

  2. 转 MYSQL SELECT ... FOR UPDATE and SELECT ... LOCK IN SHARE MODE Locking Reads

    原文: http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html In some circumstances, a consis ...

  3. 浅谈select for update 和select lock in share mode的区别

    有些情况下为了保证数据逻辑的一致性,需要对SELECT的操作加锁.InnoDB存储引擎对于SELECT语句支持两种一致性的锁定读(locking read)操作. . SELECT …… FOR UP ...

  4. 深入理解SELECT ... LOCK IN SHARE MODE和SELECT ... FOR UPDATE

    概念和区别 SELECT ... LOCK IN SHARE MODE走的是IS锁(意向共享锁),即在符合条件的rows上都加了共享锁,这样的话,其他session可以读取这些记录,也可以继续添加IS ...

  5. [MySQL] 行级锁SELECT ... LOCK IN SHARE MODE 和 SELECT ... FOR UPDATE

    一.译文 翻译来自官方文档:Locking Reads If you query data and then insert or update related data within the same ...

  6. Select for update/lock in share mode 对事务并发性影响

    select for update/lock in share mode 对事务并发性影响 事务并发性理解 事务并发性,粗略的理解就是单位时间内能够执行的事务数量,常见的单位是 TPS( transa ...

  7. Mysql加锁过程详解(4)-select for update/lock in share mode 对事务并发性影响

    Mysql加锁过程详解(1)-基本知识 Mysql加锁过程详解(2)-关于mysql 幻读理解 Mysql加锁过程详解(3)-关于mysql 幻读理解 Mysql加锁过程详解(4)-select fo ...

  8. lock in share mode 和 select for update

    lock in share mode 和 select for update 2018年07月11日 01:57:58 道不虚行只在人 阅读数 146    版权声明:欢迎转载,请注明出处 https ...

  9. (转载)Select for update/lock in share mode 对事务并发性影响

    select for update/lock in share mode 对事务并发性影响 事务并发性理解 事务并发性,粗略的理解就是单位时间内能够执行的事务数量,常见的单位是 TPS( transa ...

随机推荐

  1. UniMelb Comp30022 IT Project (Capstone) - 2.Vuforia in Unity

    2 Vuforia in Unity Tutorial: https://www.youtube.com/watch?v=X6djed8e4n0&t=213s Preparation: Dow ...

  2. Linux文件系统简介和软链接和硬链接的区别

    Linux有着极其丰富的文件系统,大体可分为如下几类: 网络文件系统:如nfs.cifs等: 磁盘文件系统:如ext3.ext4等: 特殊文件系统:如prco.sysfs.ramfs.tmpfs等: ...

  3. 日历(Calendar)模块

    #usr/bin/python3 #! -*-conding : utf-8 -*- #2018.3.14 """ 日历(Calendar)模块 此模块的函数都是日历相关 ...

  4. bug 调试

    系统性能分析中,CPU.内存和 IO 是主要关注项.----系统层面 1. 对于 CPU,如果是常见的 Linux,可以先用 top 命令查看负载状况. top -H  -p [pid] pstree ...

  5. win32绘制自定义类窗口导致绘制11个窗口的解决办法

    上网查了一圈也没有找到解决问题的办法,一旦创建了一个窗口,并且在过程函数中绘制窗口,尤其是一些非子窗口的自定义类窗口,都会生成11个窗口(算上主窗口就是12个),但是使用系统通用控件就不会有这种情况的 ...

  6. ArcFaceDemo 第二版【C#】——视频人脸识别

    啥话不说,不用跪求,直接给下载地址:http://common.tenzont.com/comdll/arcface2demo.zip(话说附件的大小不限制,还是说我的文件太大,实际上确实有点大,60 ...

  7. ZOJ 3229 Shoot the Bullet(有源汇的上下界最大流)

    Description Gensokyo is a world which exists quietly beside ours, separated by a mystical border. It ...

  8. 20172333 2017-2018-2 《Java程序设计》第7周学习总结

    20172333 2017-2018-2 <Java程序设计>第7周学习总结 教材学习内容 1.继承是创建新类的快捷方式之一,继承可以使用父类的所有方法及对象. 2.继承具有单向性,父类不 ...

  9. BluetoothClass详解

    一. BluetoothClass简介 1. 继承关系 public final class BluetoothClass extends Object implements Parcelable 该 ...

  10. HashCode和equals的理解

    -------------------------------------------------------------------------------------------第一篇博客---- ...