syntax for nolock in sql

前端 未结 5 710
感动是毒
感动是毒 2020-12-30 01:15

I have seen sql statements using nolock and with(nolock) e.g -

select * from table1 nolock where column1 > 10

AND

selec         


        
5条回答
  •  一生所求
    2020-12-30 01:54

    The list of deprecated features is at Deprecated Database Engine Features in SQL Server 2008:

    • Specifying NOLOCK or READUNCOMMITTED in the FROM clause of an UPDATE or DELETE statement.
    • Specifying table hints without using the WITH keyword.
    • HOLDLOCK table hint without parenthesis
    • Use of a space as a separator between table hints.
    • The indirect application of table hints to an invocation of a multi-statement table-valued function (TVF) through a view.

    They are all in the list of features that will be removed sometimes after the next release of SQL, meaning they'll likely be supported in the enxt release only under a lower database compatibility level.

    That being said my 2c on the issue are as such:

    • Both from table nolock and from table with(nolock) are wrong. If you need dirty reads, you should use appropiate transaction isolation levels: set transaction isolation level read uncommitted. This way the islation level used is explictily stated and controlled from one 'knob', as opposed to being spread out trough the source and subject to all the quirks of table hints (indirect application through views and TVFs etc).
    • Dirty reads are an abonimation. What is needed, in 99.99% of the cases, is reduction of contention, not read uncommitted data. Contention is reduced by writing proper queries against a well designed schema and, if necessary, by deploying snapshot isolation. The best solution, that solves works almost always save a few extreme cases, is to enable read commited snapshot in the database and let the engine work its magic:

      ALTER DATABASE MyDatabase SET ALLOW_SNAPSHOT_ISOLATION ON
      ALTER DATABASE MyDatabase SET READ_COMMITTED_SNAPSHOT ON

    Then remove ALL hints from the selects.

提交回复
热议问题