简体   繁体   中英

The use WITH(NOLOCK) in SQL Server

I have a question about the use of WITH (NOLOCK / READPAST) in SQL Server.

When I used the WITH (NOLOCK / READPAST) hint on one or two tables, and I have other tables in JOINs, should I use that hint for all tables, or just the tables that I know about this need?

As GSerg pointed out in a comment table hint are used per table.

OP commented she/he is using it to read logs. I assume log tables are not "index rich" for many reasons, mainly insert performance.

With few or none indexes querying that log will be a pain and can easily degenerate to a table scan, doing it on a table being heavily updated (by inserts) can really lead to one of the few scenarios where a (NOLOCK) is justifiable.

Use the hint on any table in the join you are not afraid of getting dirty reads or missed locked rows.

Edit

Jeroen and Lamu give us some good insight in the comments. I know many times we cannot change the schema, merely adapt and try to do the best with whatever we got. But if you can improve your logging/audit feature by changing the table/schema please do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM