views:

582

answers:

3

I've read articles like these: http://www.codinghorror.com/blog/archives/001166.html http://www.databasejournal.com/features/mssql/article.php/3566746/Controlling-Transactions-and-Locks-Part-5-SQL-2005-Snapshots.htm

And from what I understand, SQL Server has a very pessimistic locking strategy. And to improve performance, I should change the locking Read Committed Snapshot.

But I can't find where to do this. Where do I change the locking strategy?

+2  A: 

You can read up on Using Row Versioning-based Isolation Levels with examples on how to set them using the ALTER command.

It is set at the database level as follows:

ALTER DATABASE YourDatabaseName SET READ_COMMITTED_SNAPSHOT ON;

A better starting point is the parent of the above documentation, which covers related topics: Row Versioning-based Isolation Levels in the Database Engine.

EDIT: added links mentioned in my comments below.

Ahmad Mageed
Ok, but then how do I see which is the current isolation level?And does this isolation level apply only to queries running in transactions, or all queries?
Allrameest
You can run DBCC USEROPTIONS and one of the items returned will be the current setting for the "isolation level." Here's the MSDN link: http://msdn.microsoft.com/en-us/library/ms180065.aspx
Ahmad Mageed
Regarding the 2nd part of your question, to make use of the isolation level in a transaction you would need to specifically set it using "SET TRANSACTION ISOLATION LEVEL SNAPSHOT" before the "BEGIN TRAN" statement. Thus my understanding is it only affects the transaction queries that you explicitly declare it for. An example of this is in the above links. A nice article explaining this can also be read here: http://www.databasejournal.com/features/mssql/article.php/3566746/Controlling-Transactions-and-Locks-Part-5-SQL-2005-Snapshots.htm
Ahmad Mageed
Using the SNAPSHOT Isolation Level will add a lot of load to the tempdb as your database load increases.
mrdenny
+1 And this is the setting that Stack Overflow itself seems to use, see http://www.codinghorror.com/blog/archives/001166.html
Andomar
A: 

Using the SNAPSHOT Isolation Level will add a lot of load to the tempdb as your database load increases.

Changing the locking methods is best done via locking hints in the queries, or by changing the ISOLATION LEVEL in general for the stored procedure or connection. This is done with the SET ISOLATION LEVEL command, or by changing the isolation level on the connection object in .NET.

If you want SQL Server to handle its locking at a level other than the default page level (ie. row level locking) that has to be handled on a statement by statement level by using the WITH (ROWLOCK) hint within your statements.

UPDATE YourTable (WITH ROWLOCK)
   SET Col2 = 3
WHERE Col1 = 'test'

There is no global setting to change this locking level, and if ROWLOCK is used in combination with the snapshot isolation level operations will still take place at the page level as the entire page has to be copied off to the tempdb database then updated then the old version has to be dropped from the tempdb database.

mrdenny
-1 You can't choose "READ_COMMITTED_SNAPSHOT" through SET ISOLATION LEVEL, or through the .NET connection object. You have to ALTER DATABASE, as in Ahmad Mageed's answer.
Andomar
@Andomar Yes if you are going to use SNAPSHOT you have to enable it at the database level. For your transacations to use it, you must still use the SET TRANSACTION ISOLATION LEVEL SNAPSHOT for the isolation level of that transaction to use SNAPSHOT instead of READ COMMITTED which is always the default.
mrdenny
A: 

You can set the lock isolation level when you set up the connection to the database, by calling

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

Where I work we do this for every connection we set up, they are all set up in the same place as we use a shared connection pool for access to the database. The READ UNCOMMITTED option then applies to all statements issued by that connection.

Robin
Hmm I wouldn't recommend setting `READ UNCOMMITTED` across the board - it really depends on your usage of the database. SQL Server 2005+ suports snapshot isolation that essentialy versions rows in a similar way to how Oracle works.
pjp
You're confusing "Read Committed Snapshot" with "Read Uncommitted". The former is a relatively unknown setting that you can't choose with "set transaction isolation level", see Ahmad Mageed's post.
Andomar