为什么要修改ISOLATION LEVEL,因为默认的SQL使用的ISOLATION级别是Read Commited,会产生共享锁,会影响性能,有时与update竞争甚至产生死锁。
不过,并不是所有的query都需要把isolation level改为read uncommitted,因为会带来dirty read。
可通过以下3种方式添加nolock
sql query :
SELECT xxx FROM table XXX nolock .
Sql statement :
SET TRANSACTION ISOLATION LEVEL
{ READ UNCOMMITTED
| READ COMMITTED
| REPEATABLE READ
| SNAPSHOT
| SERIALIZABLE
}
[ ; ]
https://msdn.microsoft.com/en-sg/library/ms173763.aspx
For Entity framework :
using (var t = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions {
IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted
}))
{
// query
}