ΪʲôҪͬʱʹÓà NOLOCK ºÍ NOWAIT?

Why use NOLOCK and NOWAIT together?(ΪʲôҪͬʱʹÓà NOLOCK ºÍ NOWAIT?)
±¾ÎĽéÉÜÁËΪʲôҪͬʱʹÓà NOLOCK ºÍ NOWAIT?µÄ´¦Àí·½·¨£¬¶Ô´ó¼Ò½â¾öÎÊÌâ¾ßÓÐÒ»¶¨µÄ²Î¿¼¼ÛÖµ£¬ÐèÒªµÄÅóÓÑÃÇÏÂÃæËæ×Ÿú°æÍøµÄС±àÀ´Ò»Æðѧϰ°É£¡

ÎÊÌâÃèÊö

һλͬʱàдÁËÒ»¸öʹÓÃÌáʾwith (NOLOCK,NOWAIT)"µÄ²éѯ.

A colleague wrote a query which uses the hints "with (NOLOCK,NOWAIT)".

ÀýÈç

select first_name, last_name, age
from people with (nolock,nowait)

¼ÙÉè:

NOLOCK ˵²»Óõ£ÐÄÈκμ¶±ðµÄÈκÎËø¶¨£¬ÏÖÔھͶÁÈ¡Êý¾Ý"

NOLOCK says "don't worry about any locks at any level, just read the data now"

NOWAIT ˵²»ÒªµÈ´ý£¬Èç¹û±í±»Ëø¶¨£¬Ö»»á³ö´í"

ÎÊÌâ:
ΪʲôͬʱʹÓÃÁ½Õß?µ±È» NOWAIT ÓÀÔ¶²»»á±»ÊµÏÖ£¬ÒòΪ NOLOCK Òâζ×ÅËüÎÞÂÛÈçºÎ¶¼²»»áµÈ´ýËø......?

NOWAIT says "don't wait, just error if the table is locked"

Question:
Why use both at the same time? Surely NOWAIT will never be realised, as NOLOCK means it wouldn't wait for locks anyway ... ?

ÍÆ¼ö´ð°¸

ÕâÊǶàÓàµÄ(»òÕßÖÁÉÙÊÇÎÞЧµÄ).ÔÚÒ»¸ö²éѯ´°¿ÚÖУ¬Ö´ÐÐ:

It's redundant (or at least, ineffective). In one query window, execute:

create table T (ID int not null)
begin transaction
alter table T add ID2 int not null

±£³ÖÕâ¸ö´°¿Ú´ò¿ª£¬´ò¿ªÁíÒ»¸ö²éѯ´°¿Ú²¢Ö´ÐÐ:

leave this window open, open another query window and execute:

select * from T WITH (NOLOCK,NOWAIT)

¾¡¹ÜÓÐ NOWAIT Ìáʾ£¬²¢ÇÒ¾¡¹Ü¼Ç¼ΪÔÚÓöµ½ÈκÎËøÊ±Á¢¼´·µ»ØÏûÏ¢£¬µ«µÚ¶þ¸ö²éѯ½«¹ÒÆð£¬µÈ´ý Schema Ëø.

Despite the NOWAIT hint, and despite it being documented as returning a message as soon as any lock is encountered, this second query will hang, waiting for the Schema lock.

<´ò»÷>ÔĶÁÓйرí¸ñÌáʾµÄÎĵµ:

NOWAIT:

ָʾÊý¾Ý¿âÒýÇæÔÚ±íÉÏÓöµ½ËøÊ±Á¢¼´·µ»ØÏûÏ¢

Instructs the Database Engine to return a message as soon as a lock is encountered on the table

Çë×¢Ò⣬ÕâÀïÖ¸µÄÊÇËø£¬ÈκÎËø.

Note that this is talking about a lock, any lock.

NOLOCK(àÅ£¬Êµ¼ÊÉÏÊÇREADUNCOMMITTED):

READUNCOMMITTED ºÍ NOLOCK Ìáʾ½öÊÊÓÃÓÚÊý¾ÝËø.ËùÓвéѯ£¬°üÀ¨ÄÇЩ´øÓÐ READUNCOMMITTED ºÍ NOLOCK ÌáʾµÄ²éѯ£¬¶¼»áÔÚ±àÒëºÍÖ´ÐÐÆÚ¼ä»ñÈ¡ Sch-S(¼Ü¹¹Îȶ¨ÐÔ)Ëø.Òò´Ë£¬µ±²¢·¢ÊÂÎñÔÚ±íÉϳÖÓÐ Sch-M(¼Ü¹¹ÐÞ¸Ä)ËøÊ±£¬²éѯ»á±»×èÈû.

READUNCOMMITTED and NOLOCK hints apply only to data locks. All queries, including those with READUNCOMMITTED and NOLOCK hints, acquire Sch-S (schema stability) locks during compilation and execution. Because of this, queries are blocked when a concurrent transaction holds a Sch-M (schema modification) lock on the table.

ËùÒÔ£¬NOLOCK ȷʵÐèÒªµÈ´ý some Ëø.

So, NOLOCK does need to wait for some locks.

ÕâÆª¹ØÓÚΪʲôҪͬʱʹÓà NOLOCK ºÍ NOWAIT?µÄÎÄÕ¾ͽéÉܵ½ÕâÁË£¬Ï£ÍûÎÒÃÇÍÆ¼öµÄ´ð°¸¶Ô´ó¼ÒÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö¸ú°æÍø£¡

±¾Õ¾²¿·ÖÄÚÈÝÀ´Ô´»¥ÁªÍø,Èç¹ûÓÐͼƬ»òÕßÄÚÈÝÇÖ·¸ÁËÄúµÄÈ¨Òæ£¬ÇëÁªÏµÎÒÃÇ£¬ÎÒÃÇ»áÔÚÈ·ÈϺóµÚһʱ¼ä½øÐÐɾ³ý£¡

Ïà¹ØÎĵµÍƼö

SQL query to group by day(°´Ìì·Ö×éµÄ SQL ²éѯ)
What does SQL clause quot;GROUP BY 1quot; mean?(SQL ×Ӿ䡰GROUP BY 1ÊÇʲôÒâ˼?Òâ˼ÊÇ?)
Include missing months in Group By query(ÔÚ Group By ²éѯÖаüº¬È±Ê§µÄÔ·Ý)
sql group by versus distinct(sql group by Ó벻ͬ)
How to return a incremental group number per group in SQL(ÈçºÎÔÚSQLÖзµ»ØÃ¿¸ö×éµÄÔöÁ¿×éºÅ)
Count number of records returned by group by(ͳ¼Æ·Ö×é·µ»ØµÄ¼Ç¼Êý)