LOCK Comparizon : NOLOCK vs. READPAST vs. ROWLOCK vs. UPLOCK vs. HOLDLOCK

1. TABLOCK VS. HOLDLOCK

 

SELECT
 
*
 
FROM
 
table
 
WITH
 (
HOLDLOCK
) 其他事务可以读取表,但不能更新删除 


SELECT * FROM table WITH (TABLOCKX) 其他事务不能读取表,更新和删除

2. NOLOCK VS. UPLOCK

http://www.mssqltips.com/tip.asp?tip=1257

The UPDLOCK hint tells the SQL Server query engine “Don’t allow any other reader of this row to acquire an UPDLOCK (“U” lock) because I will be promoting this lock to an exclusive “X” lock later in my processing”.

from envykok:

- the row i selected no allowed to be modify by others guys because i will promote modify later .

 

 

3. NOLOCK VS. ROWLOCK

 

Diff:

  • NOLOCK only use in SELECT Statement
  • ROWLOCK can be used in SELECT, UPDATE, DELETE

 

http://www.sql-server-performance.com/articles/per/lock_contention_nolock_rowlock_p2.aspx

Using NOLOCK

Using NOLOCK politely asks SQL Server to ignore locks and read directly from the tables. This means you completely circumvent the lock system, which is a major performance and scalability improvement. However, you also completely circumvent the lock system, which means your code is living dangerously. You might read the not-necessarily-valid uncommitted modifications of a running transaction. This is a calculated risk. 

For financial code and denormalized aggregates (those little counters of related data that you stash away and try desperately to keep accurate), you should play it safe and not use this technique. But I think you'll find that for better than 90% of your application, it would not be that big of a deal if a user (or even intermediate code) saw an uncommitted modification. In fact, you'll probably find that most of your data never or only very rarely changes, in which case the overhead of locking the data is almost always completely wasted. 

For example, if I want to count all users that joined Streamload.com between June 1 and August 31 of Y2K, there's no reason for me to lock anything: that number was cast in stone the moment September 1, 2000 rolled around. Another example is the file listings you see on Streamload.com: it doesn't much matter if you don't see the exact perfect data, since either you don't own the data and it doesn't much matter what you see, or you do own the data and you know perfectly well whether you just modified the data or not and whether new files have finished uploading. 

Just don't use this type of data as the basis for modifications to the database, and don't use it when it's really important that the user not see the wrong thing (an account statement or balance, for instance). 

 

Using ROWLOCK

Using ROWLOCK politely asks SQL Server to only use row-level locks. You can use this in SELECT, UPDATE, and DELETE statements, but I only use it in UPDATE and DELETE statements. You'd think that an UPDATE in which you specify the primary key would always cause a row lock, but when SQL Server gets a batch with a bunch of these, and some of them happen to be in the same page (depending on this situation, this can be quite likely, e.g. updating all files in a folder, files which were created at pretty much the same time), you'll see page locks, and bad things will happen. And if you don't specify a primary key for an UPDATE or DELETE, there's no reason the database wouldn't assume that a lot won't be affected, so it probably goes right to page locks, and bad things happen. 

By specifically requesting row-level locks, these problems are avoided. However, be aware that if you are wrong and lots of rows are affected, either the database will take the initiative and escalate to page locks, or you'll have a whole army of row locks filling your server's memory and bogging down processing. One thing to be particularly aware of is the "Management/Current Activity" folder with Enterprise Manager. It takes a long time to load information about a lot of locks. The information is valuable, and this technique is very helpful, but don't be surprised if you see hundreds of locks in the "Locks/Processes" folder after employing this technique. Just be glad you don't have lock timeouts or deadlocks. 

 

Additional Note

I get the sense that SQL Server honors NOLOCK requests religiously, but is more discretional with ROWLOCK requests. You can only use NOLOCK in SELECT statements. This includes inner queries, and the SELECT clause of the INSERT statement. You can and should use NOLOCK in joins, for example:

SELECT COUNT(Users.UserID)
FROM Users WITH (NOLOCK)
JOIN UsersInUserGroups WITH (NOLOCK) ON 
Users.UserID = UsersInUserGroups.UserID 

 

4. NOLOCK Vs. READPAST

Diff:

  • NOLOCK : Read Uncommitted record
  • READPAST : Ignore Uncommitted record

Same: only use for SELECT Statment

http://articles.techrepublic.com.com/5100-10878_11-6185492.html

 

NOLOCK

This table hint, also known as READUNCOMMITTED, is applicable to SELECT statements only. NOLOCK indicates that no shared locks are issued against the table that would prohibit other transactions from modifying the data in the table.

The benefit of the statement is that it allows you to keep the database engine from issuing locks against the tables in your queries; this increases concurrency and performance because the database engine does not have to maintain the shared locks involved. The downside is that, because the statement does not issue any locks against the tables being read, some "dirty," uncommitted data could potentially be read. A "dirty" read is one in which the data being read is involved in a transaction from another connection. If that transaction rolls back its work, the data read from the connection using NOLOCK will have read uncommitted data. This type of read makes processing inconsistent and can lead to problems. The trick is being able to know when you should use NOLOCK.

As a side note, NOLOCK queries also run the risk of reading "phantom" data, or data rows that are available in one database transaction read but can be rolled back in another. (I will take a closer look at this side effect in part two of this article series.)

The following example shows how NOLOCK works and how dirty reads can occur. In the script below, I begin a transaction and insert a record in the SalesHistory table.

BEGIN TRANSACTION
      INSERT INTO SalesHistory
      (Product, SaleDate, SalePrice)         
      VALUES           
      ('PoolTable', GETDATE(), 500)                  

The transaction is still open, which means that the record that was inserted into the table still has locks issued against it. In a new query window, run the following script, which uses the NOLOCK table hint in returning the number of records in the SalesHistory table.

SELECT COUNT(*) FROM SalesHistory WITH(NOLOCK)

The number of records returned is 301. Since the transaction that entered the record into the SalesHistory table has not been committed, I can undo it. I'll roll back the transaction by issuing the following statement:

ROLLBACK TRANSACTION

This statement removes the record from the SalesHistory table that I previously inserted. Now I run the same SELECT statement that I ran earlier:

SELECT COUNT(*) FROM SalesHistory WITH(NOLOCK)

This time the record count returned is 300. My first query read a record that was not yet committed -- this is a dirty read.
READPAST

This is a much less commonly used table hint than NOLOCK. This hint specifies that the database engine not consider any locked rows or data pages when returning results.

The advantage of this table hint is that, like NOLOCK, blocking does not occur when issuing queries. In addition, dirty reads are not present in READPAST because the hint will not return locked records. The downside of the statement is that, because records are not returned that are locked, it is very difficult to determine if your result set, or modification statement, includes all of the necessary rows. You may need to include some logic in your application to ensure that all of the necessary rows are eventually included.

The READPAST table hint example is very similar to the NOLOCK table hint example. I'll begin a transaction and update one record in the SalesHistory table.

BEGIN TRANSACTION
      UPDATE TOP(1) SalesHistory
      SET SalePrice = SalePrice + 1

Because I do not commit or roll back the transaction, the locks that were placed on the record that I updated are still in effect. In a new query editor window, run the following script, which uses READPAST on the SalesHistory table to count the number of records in the table.

SELECT COUNT(*)

FROM SalesHistory WITH(READPAST)

My SalesHistory table originally had 300 records in it. The UPDATE statement is currently locking one record in the table. The script above that uses READPAST returns 299 records, which means that because the record I am updating is locked, it is ignored by the READPAST hint.

 

 

Others:

 

HOLDLOCK 持有共享锁,直到整个事务完成,应该在被锁对象不需要时立即释放,等于SERIALIZABLE事务隔离级别

NOLOCK 语句执行时不发出共享锁,允许脏读 ,等于 READ UNCOMMITTED事务隔离级别

PAGLOCK 在使用一个表锁的地方用多个页锁

READPAST 让sql server跳过任何锁定行,执行事务,适用于READ UNCOMMITTED事务隔离级别只跳过RID锁,不跳过页,区域和表锁

ROWLOCK 强制使用行锁

TABLOCKX 强制使用独占表级锁,这个锁在事务期间阻止任何其他事务使用这个表

UPLOCK 强制在读表时使用更新而不用共享锁
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
出现这个错误的原因可能是因为NFS服务器端没有开启nfs服务或者nfs服务开启了但是没有配置正确。您可以尝试以下方法解决这个问题: 1. 确认NFS服务已经启动。可以使用以下命令检查NFS服务状态: ``` systemctl status nfs-server ``` 如果NFS服务没有启动,可以使用以下命令启动NFS服务: ``` systemctl start nfs-server ``` 2. 确认NFS服务已经正确配置。可以使用以下命令检查NFS配置文件内容: ``` cat /etc/exports ``` 如果NFS配置文件没有正确配置,可以使用以下命令修改NFS配置文件: ``` vi /etc/exports ``` 然后在该文件中添加需要共享的目录和相关配置信息,例如: ``` /home/lkuser/mnt 10.1.45.0/24(rw,no_root_squash,sync,no_subtree_check) ``` 其中,/home/lkuser/mnt是需要共享的目录路径,10.1.45.0/24是允许访问该共享目录的IP地址范围,rw表示可读写权限,no_root_squash表示禁用root权限映射,sync表示同步写入,no_subtree_check表示禁用子树检查。 3. 在NFS服务器端重新加载配置文件。可以使用以下命令重新加载NFS配置文件: ``` exportfs -r ``` 4. 在NFS服务器端开放NFS端口。可以使用以下命令开放NFS端口: ``` firewall-cmd --add-service=nfs --permanent firewall-cmd --reload ``` 5. 在客户端尝试重新挂载NFS共享目录。可以使用以下命令重新挂载NFS共享目录: ``` mount -t nfs -o nolock 10.1.45.199:/home/lkuser/mnt /home ``` 其中,10.1.45.199是NFS服务器的IP地址,/home/lkuser/mnt是共享目录的路径,/home是客户端上的挂载点。 如果以上步骤还是无法解决问题,您可以尝试查看服务器端和客户端的系统日志,确定具体的错误信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值