在SQL中使用(NOLOCK) [英] Use of (NOLOCK) in SQL

查看:431
本文介绍了在SQL中使用(NOLOCK)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友们,



SQL中使用(Nolock)有什么用。

是否还有其他类似功能可供使用在sql。

请在这个问题上给出答案,如果我的问题是错的,那就告诉我这个。



问候,

AP

解决方案

NOLOCK意味着当您查询数据库时,不会有任何锁定。虽然这可能会加快您的查询速度,因为它不会等待释放锁定,但您可能会看到''false''(未提交)数据。



有一个看看这个当使用NOLOCK提示时应该考虑什么 [ ^ ]


NOLOCK

NOLOCK不会锁定任何对象。

这是 SELECT 操作的默认值。

它不适用于 INSERT UPDATE 删除语句。



示例:

  SELECT  OrderID 
FROM 订单 WITH (ROWLOCK)
WHERE OrderID BETWEEN 100
AND 2000

更新产品 WITH NOLOCK
SET ProductCat = ' Machine'
WHERE ProductSubCat = ' Mac'



参考:SQLAuthority.com



WITH( NOLOCK )相当于使用 READ UNCOMMITED as事务隔离级别。因此,您将承担随后回滚的读取未提交行的风险,即数据从未进入数据库。因此,虽然它可以防止读取被其他操作陷入僵局,但它会带来风险。



请参考:使用NOLOCK [ ^ ]



使用MS-SQL的NOLOCK进行更快速的查询 [ ^ ]



注意:不要使用NOLOCK进行任何数据写入,甚至不要使用快速读取。

您可以获得更多详细信息在上面提供的链接上。



希望它有所帮助!


WITH(NOLOCK)提示在对象上采用Schema Lock访问,

也称为READUNCOMMITTED,仅适用于SELECT语句。



语法:

< pre lang =sql> SELECT * FROM tableName a WITH NOLOCK


Dear Friends,

What is the use of (Nolock) in SQL.
Is there any other feature like this to use in sql.
Kindly give answer on this and if my question is wrong then educate me on this.

Regards,
AP

解决方案

NOLOCK means that when you query the database, no locks will be honored. While this may speed your query since it''s not waiting for locks to be released, you may see ''false'' (uncommitted) data.

Have a look at this What should be considered when NOLOCK hint is used[^]


NOLOCK
NOLOCK does not lock any object.
This is the default for SELECT operations.
It does not apply to INSERT, UPDATE, and DELETE statements.

Examples:

SELECT OrderID
FROM Orders WITH (ROWLOCK)
WHERE OrderID BETWEEN 100
AND 2000

UPDATE Products WITH (NOLOCK)
SET ProductCat = 'Machine'
WHERE ProductSubCat = 'Mac'


Ref: SQLAuthority.com

WITH (NOLOCK) is the equivalent of using READ UNCOMMITED as a transaction isolation level. So, you stand the risk of reading an uncommitted row that is subsequently rolled back, i.e. data never made it into the database. So, while it can prevent reads being deadlocked by other operations, it comes with a risk.

Please refer: Using NOLOCK[^]

Using MS-SQL''s NOLOCK for faster queries[^]

Note: Don''t use NOLOCK to do any writing of data, or even for quick reads.
You can get more details on links provided above.

Hope it helps!


WITH(NOLOCK) hint takes Schema Lock on the object which is accessed,
Is also known as READUNCOMMITTED, and applicable to SELECT statements only.

Syntax:

SELECT * FROM tableName a WITH (NOLOCK)


这篇关于在SQL中使用(NOLOCK)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆