Subscribe to Windows IT Pro

 

Get Newsletters

  • Get the Latest News
  • Product Updates
  • Helpful Tricks
  • Productivity Tips

Subscribe Now!

September 21, 2009 12:00 AM

Beware the NOLOCK Hint

SQL Server Pro
InstantDoc ID #102836
Rating: (3)

PASS Summit Unite 2009 Speaker Tip

Many people know that with the NOLOCK hint (equivalent to the READ UNCOMMITTED isolation level), a shared lock isn’t acquired and the query may return uncommitted reads. But this hint actually changes the storage engine’s decision-making process and may cause a query to return the same row multiple times or skip rows.

When the execution plan for a query that reads data has an Index Scan operator with the property Ordered: False, the storage engine can perform the request in one of two ways:

  1. Using an allocation order scan, based on IAM pages.
  2. Using an index order scan, based on the index leaf linked list.

If the engine uses an allocation order scan and page splits occur due to insertions or updates, you can get inconsistent reads. Say the scan visits a certain page and then a split in that page causes some rows to move to a new page that the scan hasn’t reached yet. In such a case, the scan will reread the moved rows. Similarly, if the scan hasn’t visited a page yet and the page splits, moving some rows to a page the scan already passed, those rows will be skipped.

Thus, the storage engine usually (e.g., under READ COMMITTED isolation) uses an index order scan to perform the request, even though logical index fragmentation can decrease the index order scan’s performance. If you use the NOLOCK hint, however, the storage engine assumes you’re willing to sacrifice consistency for speed and chooses an allocation order scan.

To avoid these problems and maintain read consistency, you can use READ COMMITTED SNAPSHOT. However, carefully test this isolation level, which uses row versioning and puts overhead on tempdb. SQL Server writes row versions whenever updates and deletes occur. So systems that involve mostly selects and inserts, with infrequent updates and deletes, typically benefit most from row-versioning-based isolations.

Read more from Itzik on his Nov. 6 PASS Summit 2009 post-conference seminar, "Query and Index Tuning for SQL Server 2005 and 2008."


Editor's Note: SQL Server Magazine would like to thank PASS for providing this technical tip.

Related Content:

ARTICLE TOOLS

Comments
  • Biazi
    3 years ago
    Oct 01, 2009

    Good

You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

White Papers

Get your Windows 7 deployment off to the right start by implementing PC lockdown. A locked-down environment is easier and cheaper to support since users are less likely to make unnecessary changes to the core system configuration - read more here!

Essential Guides

Is your iSCSI "lossy"? The reality is that most off-the-shelf Ethernet hardware deployed for iSCSI can lose packets, resulting in slow performance or application downtime. Learn how to assess your current iSCSI infrastructure and engineer an advanced iSCSI SAN infrastructure.

Web Seminars

What's the best way to keep your network safe from malware? In this web seminar, security expert Greg Shields suggests an alternative method to the traditional blacklisting approach that is common with anti-virus and anti-malware solutions.

eLearning Series

We bring the experts direct to you to share their real-world perspective and expertise. During each event, three sessions stream in real time, so you can learn, ask questions, and get solutions.
Upcoming event: Getting the Most with Exchange 2010 with Paul Robichaux

Subscribe to Windows IT Pro!

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.