本文介绍了SQL Server:在事务中包装SELECT查询是否会降低性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为学习练习,在尝试使用任何ORM(例如EF)之前,我想使用ADO.NET和存储过程来构建个人项目。

As learning exercise and before trying to use any ORM (like EF) I want to build a personal project using ADO.NET and stored procedures.

因为我不知道不想让我的代码随着时间的流逝而变得一团糟,我想使用诸如存储库和UoW模式之类的模式。

Because I don't want my code to become a mess over time, I want to use some patterns like the repository and UoW patterns.

我已经弄清楚了几乎所有内容,除了事务处理外。

I've got almost everything figured it out, except for the transaction handling.

为了以某种方式模拟 UoW,我使用了。使用默认的隔离级别(即读已提交),如果 SELECT 如果包装在事务中,则不会造成性能损失。如果尚未启动,SQL Server会在内部将语句包装在事务中,因此您的代码应具有几乎相同的行为。

It all depends on the transaction isolation level. Using the default isolation level (ie. read committed) then your SELECT should occur no performance penalty if is wrapped in a transaction. SQL Server internally wraps statements in a transaction anyway if one is not already started, so your code should behave almost identical.

但是,我必须问您为什么不使用已构建的语句-.Net ?这样,您的代码将与其他库和框架更好地交互,因为 TransactionScope 被普遍使用。如果您决定切换到该选项,我必须警告您,默认情况下, TransactionScope 使用SERIALIZABLE隔离级别,这会导致性能下降,请参见。

However, I must ask you why not use the built-in .Net TransactionScope? This way your code will interact much better with other libraries and frameworks, since TransactionScope is universally used. If you do decide to switch to this I must warn you that, by default, TransactionScope uses SERIALIZABLE isolation level and this does result in performance penalties, see using new TransactionScope() Considered Harmful.

这篇关于SQL Server:在事务中包装SELECT查询是否会降低性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-21 03:03