Pages

Tuesday, 15 July 2014

SQL Server: READPAST Vs NOWAIT Table Hint

When DBA/Developer includes the READPAST table hint in a query, the query executes immediately, but does not return any locked rows. In this scenario, DBA/Developer could use the following statement to execute the query immediately without any errors:

SELECT ShipMethodID,
SUM (Freight)
FROM PurchaseOrderHeader
WITH (READPAST)
GROUP BY ShipMethodID;

DBA/Developer should note that if DBA/Developer includes the READPAST table hint, the Freight value of the row that is locked would not be included in the sum returned in DBA/Developer query's result set. In this scenario, if DBA/Developer wanted to execute the query and includes the Freight value for the locked row, DBA/Developer could use the NOLOCK table hint. The NOLOCK table hint ignores all locking, but retrieves uncommitted data for locked rows. The NOLOCK table hint prevents blocking but allows dirty reads, and the READPAST table hint prevents blocking but does allow dirty
The NOWAIT table hint will cause the query to not wait for locks to be released, and will immediately return an error. Usually, queries use the LOCK_TIMEOUT setting value to determine how long the statement should wait for locks to be released before returning an error. Using the NOWAIT table hint would be the equivalent of setting LOCK_TIMEOUT to 0 before executing the query. DBA/Developer should not add the TABLOCK or UPDLOCK hint to DBA/Developer's query because both of these hints would cause the query to wait to obtain locks. The TABLOCK hint specifies that the query should acquire a table-level lock and keep the table locked until the statement completes. The UPDLOCK hint specifies that the query should acquire an update lock that remains in effect until the transaction ends.

No comments:

Post a Comment