如何在JOIN查询中使用WITH(NOLOCK)

时间:2017-05-24 14:11:21

标签: sql sql-server-2008

如何对此查询使用WITH(NOLOCK)。我知道如何将它用于普通的选择查询。但对于带有连接的查询???任何人指导我

 SELECT 
    DISTINCT Amendmentdetails.BadgeNo, ContractNo,isnull(Amendmentdetails.ContractAmendmentNo,'')AS ContractAmendmentNo,
    value As AnnualSalary,
    Amendmentdetails.ContractType,TimesheetCategory,Rotation,RM.CRotDayOn,RM.CRotDayOff ,TSCatDays                         
    from Amendmentdetails 
    Left Join 
    RotationMaster RM
    On
    Amendmentdetails.Rotation =RM.CRotCode  
    Left Join
    TimesheetCategoryMaster TM
    On
    Amendmentdetails.TimesheetCategory=TM.TSCatCode
    Left Join
    SalaryDetails 
    On
    SalaryDetails.contractAmendmentNo =Amendmentdetails.ContractAmendmentNo AND
    Paycode in(1001,1002,1003,1004,1005)

3 个答案:

答案 0 :(得分:1)

如果没有别名,

with(nolock)就在表名后面。如果有别名,那就在那之后。

SELECT 
    DISTINCT Amendmentdetails.BadgeNo, ContractNo,isnull(Amendmentdetails.ContractAmendmentNo,'')AS ContractAmendmentNo,
    value As AnnualSalary,
    Amendmentdetails.ContractType,TimesheetCategory,Rotation,RM.CRotDayOn,RM.CRotDayOff ,TSCatDays                         
    from Amendmentdetails with (nolock)
   Left Join  RotationMaster RM with (nolock) On Amendmentdetails.Rotation =RM.CRotCode  
    Left Join TimesheetCategoryMaster TM  with (nolock) On    Amendmentdetails.TimesheetCategory=TM.TSCatCode
    Left Join SalaryDetails   with (nolock)    On
    SalaryDetails.contractAmendmentNo =Amendmentdetails.ContractAmendmentNo AND
    Paycode in(1001,1002,1003,1004,1005)

您可以在此处详细了解表格提示:https://docs.microsoft.com/en-us/sql/t-sql/queries/hints-transact-sql-table

答案 1 :(得分:0)

您可以在表/别名后添加NOLOCK。

SELECT t1.col1, t2.col1
FROM table1 t1 NOLOCK 
INNER JOIN table2 t2 NOLOCK ON t1.x = t2.x
WHERE t1.col2 = 42

关于NOLOCK的事情是,它似乎不是神奇的子弹。它基本上是READUNCOMMITTED,因此您可以在读取它之后立即读取更改的脏数据。如果它不是问题,那么请继续使用它,但如果脏数据会影响您的最终结果,您可能需要查看其他一些方法来处理阻止问题。

另见:

https://blogs.msdn.microsoft.com/davidlean/2009/04/05/sql-server-nolock-hint-other-poor-ideas/

http://itknowledgeexchange.techtarget.com/sql-server/what-does-with-nolock-actually-mean/

答案 2 :(得分:0)

在表名或表别名后添加WITH(NOLOCK)提示。

SELECT DISTINCT
    Amendmentdetails.BadgeNo
    , ContractNo
    , ISNULL(Amendmentdetails.ContractAmendmentNo, '') ContractAmendmentNo
    , [value] AnnualSalary
    , Amendmentdetails.ContractType
    , TimesheetCategory
    , Rotation
    , RM.CRotDayOn
    , RM.CRotDayOff 
    , TSCatDays                         
FROM Amendmentdetails WITH(NOLOCK)
LEFT JOIN RotationMaster RM WITH(NOLOCK) ON Amendmentdetails.Rotation = RM.CRotCode  
LEFT JOIN TimesheetCategoryMaster TM WITH(NOLOCK) ON Amendmentdetails.TimesheetCategory = TM.TSCatCode
LEFT JOIN SalaryDetails WITH(NOLOCK) ON SalaryDetails.contractAmendmentNo = Amendmentdetails.ContractAmendmentNo AND Paycode IN (1001,1002,1003,1004,1005)

请确保您应用WITH (NOLOCK)的所有表都具有聚集索引。

最简单的方法是添加一个基于整数的ID主键列,该列会自动递增。

请记住,结果集可以包含尚未提交的行,这些行通常在以后回滚。

如果将WITH(NOLOCK)应用于具有非聚集索引的表,则在将行数据流式传输到结果表时,其他事务可以更改行索引。这意味着结果集可能缺少行或多次显示同一行。

READ COMMITTED(读取已提交)又增加了一个问题,即数据在单个列中损坏,多个用户同时更改同一单元格。

牢记所有这些将帮助您有效地使用WITH(NOLOCK)。

如果在查询顶部添加命令SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED,则根本无需理会WITH(NOLOCK)。