14.04.2013 Views

SQL SERVER 2008

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

124 ❘ CHAPTER 5 QUERY PROCESSING AND EXECUTION<br />

Problems with using hints can happen at any time — from when you start using the hint, which can<br />

cause unexpected side effects that cause the query to fail to compile, to more complex and diffi cult<br />

to fi nd performance issues that occur later.<br />

As data in the relevant tables changes, without query hints the Query Optimizer automatically<br />

updates statistics and adjusts query plans as needed; but if you have locked the Query Optimizer<br />

into a specifi c set of optimizations using query hints, then the plan cannot be changed, and you may<br />

end up with a considerably worse plan, requiring further action (from you) to identify and resolve<br />

the root cause of the new performance issue.<br />

One fi nal word of caution about using query hints: Unlike locking hints (also referred to in BOL<br />

as table hints), which <strong>SQL</strong> Server attempts to satisfy, query hints are stronger, so if <strong>SQL</strong> Server is<br />

unable to satisfy a query hint it will raise error 8622 and not create any plan.<br />

Query hints are specifi ed using the OPTION clause, which is always added at the end of the T-<strong>SQL</strong><br />

statement — unlike locking or join hints, which are added within the T-<strong>SQL</strong> statement after the<br />

tables they are to affect.<br />

NOTE Refer to the Transact <strong>SQL</strong> Reference, Data Manipulation Language,<br />

Hints(Transact-<strong>SQL</strong>) section, or search for query hints in <strong>SQL</strong> Server 2012<br />

Books Online for a complete list of query hints.<br />

The following sections describe a few of the more interesting query hints.<br />

FAST number_rows<br />

Use this query hint when you want to retrieve only the fi rst n rows out of a relatively large result set.<br />

A typical example of this is a website that uses paging to display large sets of rows, whereby the fi rst<br />

page shows only the fi rst web page worth of rows, and a page might contain only 20, 30, or maybe<br />

40 rows. If the query returns thousands of rows, then <strong>SQL</strong> Server would possibly optimize this<br />

query using hash joins. Hash joins work well with large datasets but have a higher setup time than<br />

perhaps a nested loop join. Nested loop joins have a very low setup cost and can return the fi rst<br />

set of rows more quickly but takes considerably longer to return all the rows. Using the<br />

FAST query hint causes the Query Optimizer to use nested loop joins and other<br />

techniques, rather than hashed joins, to get the fi rst n rows faster.<br />

Typically, once the fi rst n rows are returned, if the remaining rows are retrieved, the query performs<br />

slower than if this hint were not used.<br />

{Loop | Merge | Hash } JOIN<br />

The JOIN query hint applies to all joins within the query. While this is similar to the join hint that<br />

can be specifi ed for an individual join between a pair of tables within a large more complex query,<br />

the query hint applies to all joins within the query, whereas the join hint applies only to the pair of<br />

tables in the join with which it is associated.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!