简体   繁体   English

InnoDB中的MySQL Atomic UPDATE与MyISAM相比

[英]MySQL Atomic UPDATE in InnoDB vs MyISAM

Is this "compare and swap" statement always atomic regardless of engine (eg InnoDB or MyISAM)? 无论引擎如何(例如InnoDB或MyISAM),这种“比较和交换”语句是否始终是原子的? :

UPDATE tbl_name SET locked=1 WHERE id=ID AND locked <> 1;

I ask this because I intend to use this statement to do pseudo row-level locking that is compatible with both transactional and non-transactional database tables. 我问这个是因为我打算使用这个语句进行与事务和非事务数据库表兼容的伪行级锁定。

This is the method that is recommended for MyISAM , but I am uncertain as to whether this works for InnoDB since the documentation suggests using transactions instead. 这是MyISAM推荐的方法,但我不确定这是否适用于InnoDB,因为文档建议使用事务。

Yes. 是。 In InnoDB, the row will be locked (make you have an unique index on id, the update locks all rows it has to scan), updated and the lock released. 在InnoDB中,行将被锁定(使您在id上有唯一索引,更新锁定它必须扫描的所有行),更新并释放锁。 If you are not in an explicit transaction / auto-commit is on, each statement is run in its own transaction, but it's still a transaction and lockings are performed 如果您没有显式事务/自动提交已启用,则每个语句都在其自己的事务中运行,但它仍然是事务并且执行锁定

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM