将事务用于选择语句?

2023-10-25数据库问题
5

本文介绍了将事务用于选择语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我不经常使用存储过程,我想知道将我的选择查询包装在事务中是否有意义.

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.

这篇关于将事务用于选择语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12

sql group by 与不同
sql group by versus distinct(sql group by 与不同)...
2024-04-16 数据库问题
37

如何在SQL中返回每个组的增量组号
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)...
2024-04-16 数据库问题
8

统计分组返回的记录数
Count number of records returned by group by(统计分组返回的记录数)...
2024-04-16 数据库问题
10

带聚合函数的 SQL GROUP BY CASE 语句
SQL GROUP BY CASE statement with aggregate function(带聚合函数的 SQL GROUP BY CASE 语句)...
2024-04-16 数据库问题
23