Exploring optimizer hints
In this recipe, we will see how to suggest (to the optimizer) the strategy to adopt for choosing the best execution plan, using optimizer hints.
Note
The use of optimizer hints is a trick and should be considered only when no solution seems to work. The query optimizer is designed to choose the best execution plan, based on many different considerations, so it's very important to keep updated statistics to help the query optimizer in doing its work.
How to do it...
The following steps will explore optimizer hints:
Connect the database to the
SH
schema:CONNECT sh@TESTDB/sh
Set the auto-trace functionality in SQL*Plus to see only the execution plan without executing the queries:
SET AUTOT TRACE EXP
Select some records from the
CUSTOMERS
table (say, all customers born in 1949):SELECT C.CUST_FIRST_NAME, C.CUST_LAST_NAME FROM sh.CUSTOMERS C WHERE C.CUST_YEAR_OF_BIRTH = 1949;
Execute the same query suggesting that it's better to full-scan the
CUSTOMERS
table in order to...