简体   繁体   English

SQL Server - Dirty Reads Pros&Cons

[英]SQL Server - Dirty Reads Pros & Cons

Why should I or shouldn't I use dirty reads: 我为什么要或不应该使用脏读:

set transaction isolation level read uncommitted

in SQL Server? 在SQL Server中?

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. 这显然会对依赖数据完整性的应用程序部分产生严重影响。

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. 例如,在我最近工作的游戏网站上,有一些关于最近游戏的统计数据的摘要显示,这都是基于脏读,对我们来说更重要的是包括然后排除尚未提交的交易数据(无论如何,我们知道很少(如果有的话)交易会退出),我们觉得平均来说数据会更准确。

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中引入的快照隔离

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是你想在提交之前读取数据的时候,我们可以借助set transaction隔离级别读取未提交的数据,数据可能会或者可能不会改变。

We can read the data by using the query: 我们可以使用查询来读取数据:

Select * from table_name with(nolock) 

This is applicable to only read uncommitted isolation level. 这仅适用于读取未提交的隔离级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM