SQL Server - 脏读的优点和缺点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20840/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 23:08:51  来源:igfitidea点击:

SQL Server - Dirty Reads Pros & Cons

sqlsql-server

提问by Seibar

Why should I or shouldn't I use dirty reads:

为什么我应该或不应该使用脏读:

set transaction isolation level read uncommitted

in SQL Server?

在 SQL Server 中?

回答by Yaakov Ellis

From MSDN:

MSDN

When this option is set, it is possible to read uncommitted or dirty data; values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction.

设置此选项时,可以读取未提交或脏数据;在事务结束之前,数据中的值可以更改,行可以在数据集中出现或消失。

Simply put, when you are using this isolation level, and you are performing multiple queries on an active table as part of one transaction, there is no guarantee that the information returned to you within different parts of the transaction will remain the same. You could query the same data twice within one transaction and get different results (this might happen in the case where a different user was updating the same data in the midst of your transaction). This can obviously have severe ramifications for parts of your application that rely on data integrity.

简而言之,当您使用此隔离级别,并且您在一个活动表上作为一个事务的一部分执行多个查询时,不能保证在事务的不同部分返回给您的信息将保持不变。您可以在一个事务中两次查询相同的数据并获得不同的结果(这可能发生在不同用户在您的事务中更新相同数据的情况下)。这显然会对依赖数据完整性的应用程序部分产生严重影响。

回答by Tokabi

Generally when you need to do a sizeable (or frequent) queries to busy tables, where read committed would possibly be blocked by locks from uncommited transactions, but ONLY when you can live with inaccurate data.

通常,当您需要对繁忙的表进行大量(或频繁)查询时,提交的读取可能会被未提交事务的锁阻塞,但仅当您可以接受不准确的数据时。

As an example, on a gaming web site I worked on recently there was a summary display of some stats about recent games, this was all based on dirty reads, it was more important for us to include then exclude the transactional data not yet committed (we knew anyway that few, if any, transactions would be backed out), we felt that on average the data would be more accurate that way.

举个例子,在我最近工作的一个游戏网站上,有一个关于最近游戏的一些统计数据的摘要显示,这都是基于脏读,对我们来说更重要的是包括然后排除尚未提交的交易数据(无论如何,我们知道很少有交易(如果有的话)会被撤销),我们认为平均而言,这样数据会更准确。

回答by SQLMenace

use it if you want the data back right away and it is not that important if it is right
do not use if if the data is important to be correct or if you are doing updates with it

如果您希望立即恢复数据,请使用它,如果正确则不那么重要,如果
数据对正确很重要或者您正在使用它进行更新,请不要使用

Also take a look at snapshot isolation which has been introduced in sql server 2005

还可以查看 sql server 2005 中引入的快照隔离

回答by K ROHAN

The Thing is when you want to read the data before committing, we can do with the help of set transaction isolation level read uncommitted, the data may, or may not change.

Thing是当你想在提交之前读取数据时,我们可以借助设置事务隔离级别读取未提交,数据可能会或可能不会改变。

We can read the data by using the query:

我们可以使用查询来读取数据:

Select * from table_name with(nolock) 

This is applicable to only read uncommitted isolation level.

这适用于只读未提交隔离级别。