views:

1920

answers:

5

In plan English what are the disadvanges and advantages of using SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED in query for .NET applications and reporting services application?

+5  A: 

The advantage is that it can be faster in some situations. The disadvantage is the result can be wrong (data which hasn't been committed yet could be returned) and there is no guarantee that the result is repeatable.

If you care about accuracy, don't use this.

More information is on MSDN:

Implements dirty read, or isolation level 0 locking, which means that no shared locks are issued and no exclusive locks are honored. 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. This option has the same effect as setting NOLOCK on all tables in all SELECT statements in a transaction. This is the least restrictive of the four isolation levels.

Mark Byers
How would this affect the speed of the query?
Kip Birgen
@Kip - `select` statements wouldn't have to wait to acquire shared locks on resources that are exclusively locked by other transactions.
Jarrod Dixon
+2  A: 

In all seriousness: Don't. Unless you really know what you are doing (and even then, there is probably a better solution)

Mitch Wheat
What would be an alternative solutions to this?
Kip Birgen
THere are none. Mitch just does not know what he says. THe general advice is: do no do it UNLESS it makes sense. It makes sense, for example, in a daa warehouse scneario where the data is snapshotted and thus there are no writes anyway.
TomTom
@TomTom: a warehouse with no writes should be marked as read-only.
Mitch Wheat
No, because writes may happen on staging tables during the day, to be consolidated during off hours ;)
TomTom
+8  A: 

This isolation level allows dirty reads. One transaction may see uncommitted changes made by some other transaction.

To maintain the highest level of isolation, a DBMS usually acquires locks on data, which may result in a loss of concurrency and a high locking overhead. This isolation level relaxes this property.

You may want to check out the Wikipedia article for a few examples and further reading:


You may also be interested in checking out Jeff Atwood's blog article on how he and his team tackled a deadlock issue in the early days of Stack Overflow:

According to Jeff:

But is nolock dangerous? Could you end up reading invalid data with read uncommitted on? Yes, in theory. You'll find no shortage of database architecture astronauts who start dropping ACID science on you and all but pull the building fire alarm when you tell them you want to try nolock. It's true: the theory is scary. But here's what I think: "In theory there is no difference between theory and practice. In practice there is."

I would never recommend using nolock as a general "good for what ails you" snake oil fix for any database deadlocking problems you may have. You should try to diagnose the source of the problem first.

But in practice adding nolock to queries that you absolutely know are simple, straightforward read-only affairs never seems to lead to problems... As long as you know what you're doing.

One alternative to the READ UNCOMMITTED level that you may want to consider is the READ COMMITTED SNAPSHOT. Quoting Jeff again:

Snapshots rely on an entirely new data change tracking method ... more than just a slight logical change, it requires the server to handle the data physically differently. Once this new data change tracking method is enabled, it creates a copy, or snapshot of every data change. By reading these snapshots rather than live data at times of contention, Shared Locks are no longer needed on reads, and overall database performance may increase.

Daniel Vassallo
Very helpful article
Kip Birgen
+2  A: 

This can be useful to see the progress of long insert queries, make any rough estimates (like COUNT(*) or rough SUM(*)) etc.

In other words, the results the dirty read queries return are fine as long as you treat them as estimates and don't make any critical decisions based upon them.

Quassnoi
A: 

You asked for this in "plan English" I have no idea what that is but I'll state it in plain English. :)

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

When this is passed as part of you SQL query, SQL Server's default setting is overided.

The tables used in the query are not locked while the query is processed and returned to your application.

That means if someone or something can edit a table in your database (in the middle of your query being run) you won't get that update in your query since the table wasn't locked.

Honestly, in most cases this is not a problem. It is always better to let SQL do it's normal thing and lock the table, but I really doubt it will have a majority adverse effect in your case.

Justin Jenkins
Sorry, this is just wrong. READ UNCOMMITED is not about locking tables for update, but about reading data that has been written by other not-yet-commited transactions.
vwegert
I didn't mention anything about running an UPDATE I was talking about an SELECT. I was trying to be simple. What I said was not wrong. "... you won't get that update in your query since the table wasn't locked." IE ... as you said the query will not account for "not-yet-commited transactions" it's *committed* btw.I think it is rather harsh to give a -1 for attempting to more simply explain the matter.
Justin Jenkins