时间:2021-07-01 10:21:17 帮助过:2人阅读
I used to see my senior developers use WITH (NOLOCK) when querying in SQL Server and wonder why they use. Now i explored it and found that it‘s useful to improve the performance in executing the query . However there is a disadvantage in using it. The disadvantage is that one may not be sure that they are getting the data which is currently being updated in the Table, ie, Without lock protection, you cannot be guaranteed that the data isn‘t changing during the time that the query is running. I referred this link and found it pretty useful.
要提升SQL的查询效能,一般来说大家会以建立索引(index)为第一考虑。其实除了index的建立之外,当我们在下SQL Command时,在语法中加一段WITH (NOLOCK)可以改善在线大量查询的环境中数据集被LOCK的现象藉此改善查询的效能。
不过有一点千万要注意的就是,WITH (NOLOCK)的SQL SELECT有可能会造成Dirty Read。
例如:
SELECT COUNT(UserID)因为SQL Server会执行对应的锁定一致性检查。 欲改善整体数据库查询的效能,请将WITH (NOLOCK)加在您的SELECT语法中Table名称的后面,虽然(NOLOCK)也可以,但是微软还是建议大家要加WITH。
FROM EMPLOYEE WITH (NOLOCK)
JOIN WORKING_GROUP WITH (NOLOCK)
ON EMPLOYEE.UserID = WORKING_GROUP.UserID
sql SELECT时的with(nolock)选项说明
标签: