⌘BABAKOV.NET Let's code!
100% [light] [ru]

Table Hints - MSSQL

Table Hints

Table Hints relate only to tables and views for which they are specified. These hints are intended to determine the locking method, specify a particular index, and the method of data processing in tables, for example, scanning or searching.

Syntax:

WITH  ( <table_hint> [ [ , ] ...n ] )

<table_hint> ::=
{ NOEXPAND
  | INDEX ( <index_value> [ , ...n ] ) | INDEX = ( <index_value> )
  | FORCESEEK [ ( <index_value> ( <index_column_name> [ , ... ] ) ) ]
  | FORCESCAN
  | HOLDLOCK
  | NOLOCK
  | NOWAIT
  | PAGLOCK
  | READCOMMITTED
  | READCOMMITTEDLOCK
  | READPAST
  | READUNCOMMITTED
  | REPEATABLEREAD
  | ROWLOCK
  | SERIALIZABLE
  | SNAPSHOT
  | SPATIAL_WINDOW_MAX_CELLS = <integer_value>
  | TABLOCK
  | TABLOCKX
  | UPDLOCK
  | XLOCK
}

<table_hint_limited> ::=
{
    KEEPIDENTITY
  | KEEPDEFAULTS
  | HOLDLOCK
  | IGNORE_CONSTRAINTS
  | IGNORE_TRIGGERS
  | NOLOCK
  | NOWAIT
  | PAGLOCK
  | READCOMMITTED
  | READCOMMITTEDLOCK
  | READPAST
  | REPEATABLEREAD
  | ROWLOCK
  | SERIALIZABLE
  | SNAPSHOT
  | TABLOCK
  | TABLOCKX
  | UPDLOCK
  | XLOCK
}


Arguments:

WITH ( <table_hint> ) [ [ , ] ...n ]


With the keyword WITH hints must be specified in parentheses.

The absence of the keyword WITH is an outdated possibility: this possibility will be removed in a future version of SQL Server. It is recommended to avoid using this possibility in new developments and plan the change of applications that currently use this possibility.

Hints that can be used with the keyword WITH and without it: NOLOCK, READUNCOMMITTED, UPDLOCK, REPEATABLEREAD, SERIALIZABLE, READCOMMITTED, TABLOCK, TABLOCKX, PAGLOCK, ROWLOCK, NOWAIT, READPAST, XLOCK, SNAPSHOT, NOEXPAND.

If a hint is used without the keyword
WITH then it must be one. For example:

FROM t (TABLOCK)


If multiple hints are used then the keyword WITH is mandatory:

FROM t WITH (TABLOCK, INDEX(myindex))

It is recommended to use commas to separate hints.

Separating hints with spaces instead of commas is an outdated function: it will be removed in future versions of SQL Server. It is recommended to avoid using this function in new developments and plan the change of scripts that currently use it.



NOLOCK

Allows «dirty reading». Behavior becomes similar to the READ UNCOMMITTED isolation level. The NOLOCK hint is equivalent to the READUNCOMMITTED hint.

select * from dbo.MyTable WITH (nolock)

 

INDEX

SPECIFIES the index that will be forcibly used by the query optimizer for processing the instruction.

The most common application scenario:

SELECT column1, column2
FROM dbo.MyTable WITH (INDEX (MyNonClusteredIndex))
WHERE column3 = \'some_value\';



  • FORCESEEK – specifies that the query optimizer uses only the search operation in the index as the access path to the data or views;
  • FORCESCAN – specifies that the query optimizer uses only the scanning operation in the index as the access path to the data or views;
  • ROWLOCK – specifies that instead of page or table locks, row locks are applied;
  • HOLDLOCK – imposes additional restrictions on combined locking, in particular, it holds the lock until the end of the transaction instead of releasing it immediately after the table or page of data is no longer needed. In other words, behavior becomes similar to the SERIALIZABLE isolation level. The HOLDLOCK hint is equivalent to the SERIALIZABLE hint;
  • NOEXPAND – specifies that when processing a query, an indexed view is not expanded for access to the underlying tables. The query optimizer processes the view as a table with a clustered index. This hint is applied only to indexed views.

Comments (0)

No comments yet.

Comments are closed for this post.