SQL Server uses locking to support concurrency of data in Multi user environment .The locking can be applied to the Database resources such ROWS, INDEX, PAGE, EXTENT, TABLE or the Database itself. SQL Server automatically escalates row, key, or page locks to table locks as appropriate to protects system resources and increases efficiency. Locking at smaller granularity increases concurrency but create high overhead on the Database on the other hand Locking at larger granularity reduces overhead but expense in terms of concurrency.The best practice is to avoid lock escalation .The following tips can be useful to minimize locking
- Keep all Transact-SQL transactions as short as possible.
- Avoid interleaving reads and database changes in same transaction.
- Do all the conditional logic and variable assignment outside of a Transaction.
- Encapsulate all transactions within stored procedures.
- Avoid Transact-SQL statements inside the transaction that affect large numbers of rows at once.
- Although WHILE nesting transactions is perfectly legal avoid using inside the transactions.
- For lookup tables consider altering the default lock level for the table (Use SP_INDEXOPTION).
- Do not create temporary tables from within a stored procedure that is invoked by the INSERT INTO #temp EXECUTE statement.