Use transactions for select statements?(将事务用于选择语句?)
问题描述
我不经常使用存储过程,我想知道将我的选择查询包装在事务中是否有意义.
I don't use Stored procedures very often and was wondering if it made sense to wrap my select queries in a transaction.
我的程序有三个简单的选择查询,其中两个使用第一个的返回值.
My procedure has three simple select queries, two of which use the returned value of the first.
推荐答案
在高度并发的应用程序中,(理论上)可能会发生在第一个选择中读取的数据在其他选择执行之前被修改的情况.
In a highly concurrent application it could (theoretically) happen that data you've read in the first select is modified before the other selects are executed.
如果您的应用程序中可能发生这种情况,您应该使用事务来包装您的选择.请确保选择正确的隔离级别,但并非所有事务类型都能保证一致的读取.
If that is a situation that could occur in your application you should use a transaction to wrap your selects. Make sure you pick the correct isolation level though, not all transaction types guarantee consistent reads.
更新:您还可以找到关于并发更新/插入解决方案(又名upsert)有趣.它把几种常用的 upsert 方法放到测试中,看看是什么方法实际上保证数据在 select 和 next 语句之间没有被修改.结果是,嗯,令人震惊.
Update : You may also find this article on concurrent update/insert solutions (aka upsert) interesting. It puts several common methods of upsert to the test to see what method actually guarantees data is not modified between a select and the next statement. The results are, well, shocking I'd say.
这篇关于将事务用于选择语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将事务用于选择语句?
基础教程推荐
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
