SELECT ... *FOR UPDATE* 的目的是什么?

发布于 2024-10-26 09:12:42 字数 207 浏览 6 评论 0原文

我很困惑为什么你会指定 FOR UPDATE ——为什么数据库关心你要如何处理 SELECT 中的数据?

编辑:抱歉,我问的问题很糟糕。我知道文档说它会将事情变成“锁定读取”——我想知道的是“在哪些情况下,指定 FOR UPDATE 和不指定它之间可观察的行为会有所不同——也就是说,这个锁具体意味着什么?

I'm confused as to why you would specify FOR UPDATE -- why does the database care what you're going to do with the data from the SELECT?

EDIT: Sorry, I asked the question poorly. I know the docs say that it turns things into a "locking read" -- what I'd like to know is "what cases exist where the observable behavior will differ between specifying FOR UPDATE and not specifying it -- that is, what specifically does that lock entail?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不忘初心 2024-11-02 09:12:42

http://dev.mysql.com/doc/refman/5.0/en/ innodb-locking-reads.html

它与事务中锁定表有关。假设您有以下情况:

START TRANSACTION;
SELECT .. FOR UPDATE;
UPDATE .... ;
COMMIT;

在 SELECT 语句运行后,如果您有来自不同用户的另一个 SELECT,则它不会运行,直到您的第一个事务到达 COMMIT 行。

另请注意,事务之外的 FOR UPDATE 是没有意义的。

http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

It has to do with locking the table in transactions. Let's say you have the following:

START TRANSACTION;
SELECT .. FOR UPDATE;
UPDATE .... ;
COMMIT;

after the SELECT statement runs, if you have another SELECT from a different user, it won't run until your first transaction hits the COMMIT line.

Also note that FOR UPDATE outside of a transaction is meaningless.

萌辣 2024-11-02 09:12:42

其旨在解决的特定情况是当您需要读取和更新列中的值时。有时,您可以先更新列(锁定它),然后再读取它,例如:

UPDATE child_codes SET counter_field = counter_field + 1;
SELECT counter_field FROM child_codes;

这将返回 counter_field 的新值,但这在您的应用程序中可能是可以接受的。如果您尝试重置字段(因此需要原始值)或者您有无法在更新语句中表达的复杂计算,则这是不可接受的。在这种情况下,为了避免两个连接同时更新同一列,您需要锁定该行。

如果您的 RDBMS 不支持 FOR UPDATE 那么您可以通过执行无用的更新来模拟它,例如

UPDATE child_codes SET counter_field = counter_field;
SELECT counter_field FROM child_codes;
UPDATE child_codes SET counter_field = 0;

The specific case that this is designed to fix is when you need to read and update a value in a column. Sometimes you can get away with updating the column first (which locks it) and then reading it afterwards, for instance:

UPDATE child_codes SET counter_field = counter_field + 1;
SELECT counter_field FROM child_codes;

This will return the new value of counter_field, but that may be acceptable in your application. It would not be acceptable if you were trying to reset the field (and you therefore needed the original value) or if you had a complex calculation that could not be expressed in an update statement. In this case to avoid two connections racing to update the same column at the same time you need to lock the row.

If your RDBMS doesn't support FOR UPDATE then you can simulate it by performing a useless update e.g.

UPDATE child_codes SET counter_field = counter_field;
SELECT counter_field FROM child_codes;
UPDATE child_codes SET counter_field = 0;
百思不得你姐 2024-11-02 09:12:42

SELECT FOR UPDATE 告诉 RDBMS 您想要锁定这些行,以便其他人无法访问它们,直到您更新并提交或回滚它们并解锁它们:

http://www.techonthenet.com/oracle/cursors/for_update.php

SELECT FOR UPDATE tells the RDBMS that you want to lock those rows so no one else can access them until you UPDATE and commit or roll them back and unlock them:

http://www.techonthenet.com/oracle/cursors/for_update.php

薄荷梦 2024-11-02 09:12:42

它创建一个锁定读取,以便在完成之前没有人可以更新它,示例

SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;

请参见此处 http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

It creates a locking read so that nobody can update it until you are done, example

SELECT counter_field FROM child_codes FOR UPDATE;
UPDATE child_codes SET counter_field = counter_field + 1;

See here http://dev.mysql.com/doc/refman/5.0/en/innodb-locking-reads.html

奢望 2024-11-02 09:12:42

它将锁定行(或整个表),以便不能在另一个会话中同时更新这些行。锁定将一直保持到事务提交或回滚为止。

It will lock the rows (or the whole table) so that the rows can't be updated in another session concurrently. The lock is held until the transactions is committed or rolled back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文