gpt4 book ai didi

mysql - 设置可序列化会使 2 个 select 语句成为原子吗? (两次选择之间的表格没有变化)

转载 作者:行者123 更新时间:2023-11-29 00:25:54 25 4
gpt4 key购买 nike

我想使用 2 个选择语句从两个表中进行选择。但是当第一个表被选中时,另一个用户可能会更改第二个表

如果隔离级别设置为serializable,是否保证在选择第一个表时第二个表不会发生变化?

示例:

Select * from Burgers where ID = 299 -- returns 1 rows

Select * from BurgerIngredients where BurgerID = 299 -- returns many rows

如果在检索汉堡时 BurgerIngredients 可能会发生变化,我该怎么做才能确保成分在检索之前保持不变?

谢谢

最佳答案

根据 the documentation for MySQL , 是的。您只需要确保两者都是 InnoDB 表并且两个 SELECT 都发生在同一事务中:

START TRANSACTION;
Select * from Burgers where ID = 299;
Select * from BurgerIngredients where BurgerID = 299;
COMMIT;

您不需要将隔离级别设置为 SERIALIZABLE,默认的 REPEATABLE READ 足以实现此目的。

All consistent reads within the same transaction read the snapshot established by the first read. This convention means that if you issue several plain (nonlocking) SELECT statements within the same transaction, these SELECT statements are consistent also with respect to each other.

请注意,这不会阻止其他进程写入表,只是此事务不会看到更改。

编辑:

现在我看到您还用 MS SQL 服务器标记了问题。在这种情况下,the same principle holds如果您指定事务隔离级别 SNAPSHOT

关于mysql - 设置可序列化会使 2 个 select 语句成为原子吗? (两次选择之间的表格没有变化),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18927798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com