我使用以下查询从数据库中检索客户详细信息。
方法1:
BEGIN TRAN
IF EXISTS(SELECT TOP 1 'X' From CUSTOMER Where CustId = @Code AND Status = 'D')
BEGIN
UPDATE CUSTOMER
SET Status = 'L'
WHERE CustId = @BorrowerCode AND ISNULL(Borrower,'') = 'Y'
SELECT CustId, MobileNo, PlaceDesc
FROM CUSTOMER C
JOIN PLACE P ON C.FKID = P.Pk_Id
WHERE Cust_Id = @Code AND C.Status = 'L'
END
COMMIT TRAN
方法2:
BEGIN TRAN
IF EXISTS(SELECT TOP 1 'X' From CUSTOMER Where CustId = @Code AND Status = 'D')
BEGIN
UPDATE CUSTOMER
SET Status = 'L'
WHERE CustId = @BorrowerCode AND ISNULL(Borrower,'') = 'Y'
SELECT CustId, MobileNo, PlaceDesc
FROM CUSTOMER C With(NoLock)
JOIN PLACE P With(NoLock) ON C.FKID = P.Pk_Id
WHERE Cust_Id = @Code AND C.Status = 'L'
END
COMMIT TRAN
如果数据库中存在打开的事务,则查询将因方法1的超时而失败。在事务中使用NoLock是否良好做法?
答案 0 :(得分:3)
很少......
1.首先,您的更新不是sargable,您可以尝试重写它,如下所示
UPDATE CUSTOMER
SET Status = 'L'
WHERE CustId = @BorrowerCode AND Borrower = 'Y'