15.01.2013 Views

Oracle Database 11g The Complete Reference (Osborne ORACLE ...

Oracle Database 11g The Complete Reference (Osborne ORACLE ...

Oracle Database 11g The Complete Reference (Osborne ORACLE ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 46: <strong>The</strong> Hitchhiker’s Guide to Tuning Applications and SQL 805<br />

hint to force a NESTED LOOPS join to be used, then you may also use an INDEX hint to specify<br />

which index should be used during the NESTED LOOPS join and which table should be accessed<br />

via a full table scan.<br />

Hints About Goals<br />

You can specify a hint that directs the optimizer to execute a query with a specific goal in mind.<br />

<strong>The</strong> available goals related to joins are the following:<br />

■<br />

■<br />

ALL_ROWS Execute the query so that all of the rows are returned as quickly as possible.<br />

FIRST_ROWS Execute the query so that the first row will be returned as quickly as<br />

possible.<br />

By default, the optimizer will execute a query using an execution path that is selected to<br />

minimize the total time needed to resolve the query. Thus, the default is to use ALL_ROWS as the<br />

goal. If the optimizer is only concerned about the total time needed to return all rows for the<br />

query, then set-based operations such as sorts and MERGE JOIN can be used. However, the ALL_<br />

ROWS goal may not always be appropriate. For example, online users tend to judge the<br />

performance of a system based on the time it takes for a query to return the first row of data. <strong>The</strong><br />

users thus have FIRST_ROWS as their primary goal, with the time it takes to return all of the rows<br />

as a secondary goal.<br />

<strong>The</strong> available hints mimic the goals: the ALL_ROWS hint allows the optimizer to choose from<br />

all available operations to minimize the overall processing time for the query, and the FIRST_<br />

ROWS hint tells the optimizer to select an execution path that minimizes the time required to<br />

return the first row to the user.<br />

If you use the FIRST_ROWS hint, the optimizer will be less likely to use MERGE JOIN and<br />

more likely to use NESTED LOOPS.<br />

Hints About Methods<br />

In addition to specifying the goals the optimizer should use when evaluating join method<br />

alternatives, you can list the specific operations to use and the tables to use them on. If a query<br />

involves only two tables, you do not need to specify the tables to join when providing a hint<br />

for a join method to use.<br />

<strong>The</strong> USE_NL hint tells the optimizer to use a NESTED LOOPS operation to join tables. In the<br />

following example, the USE_NL hint is specified for the join query example. Within the hint, the<br />

BOOKSHELF table is listed as the inner table for the join.<br />

select /*+ USE_NL(bookshelf) */<br />

BOOKSHELF.Publisher,<br />

BOOKSHELF_AUTHOR.AuthorName<br />

from BOOKSHELF, BOOKSHELF_AUTHOR<br />

where BOOKSHELF.Title = BOOKSHELF_AUTHOR.Title;<br />

If you want all of the joins in a many-table query to use NESTED LOOPS operations, you<br />

could just specify the USE_NL hint with no table references. In general, you should specify table<br />

names whenever you use a hint to specify a join method, because you do not know how the<br />

query may be used in the future. You may not even know how the database objects are currently<br />

set up—for example, one of the objects in your from clause may be a view that has been tuned to<br />

use MERGE JOIN operations.

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

Saved successfully!

Ooh no, something went wrong!