You are on page 1of 11

Hints in Oracle:

Definition: Hints is an instruction to the optimizer, which can


influence the
Optimizer to choose a different plan for sql
statement.
The optimizer uses these hints to choose an execution plan
for the statement.

Why we use Hints:


Hints let you make decision usually made by the
optimizer.
Suppose table, Index statistics, Histograms are not
gathered properly
Then the optimizer might choose a plan which is not
optimal.
In this case like this hints can be used to influenced the
optimizer to get the better plan
Note:
Hints Should be used only as last Resort. If statistics are
gathered properly and query is still following a suboptimal execution plan.
Category of Hints:
1. Hints for optimization approaches and goals.
2. Hints for enabling optimizer features.
3. Hints for access paths.
4. Hints for Join operation.
5. Hints for Join orders.
6. Hints for parallel execution.
7. Hints for Query transformation.
8. Hints for online application upgrade.
9. Additional Hints.

1. Hints for optimization approaches and goals:


Hints Name
All_Rows

Choose
First_row
Rule

Description
Used the cost based
approach for best
throughput. Its a default
in oracle 11g.
If statistics are available
will use cost else rule.
Use cost based approach
for best response time.
Use rules based
approach. This cancels
only if other hints
specified for the
statement. This hint is an
out dated hint.

First we enable the auto trace


Sql> set autotrace traceonly expl;

Writing standard for Hints:


/*+ <hintsname> */
Examples:
Sql> select /*+ all_rows */empid,ename from emp where
deptno=10;
Sql>Select /*+ Choose */empid,ename,job,deptno from emp
order by empid;

If statistics are not gathered from a long time for emp table
in choose hints display rule based otherwise it will display
cost based result.
How to gather table statistics:
Old method:
Sql> analyze table emp compute statistics;
New method:
Sql> exec DBMS_STATS.gather_table_stats(ownname => 'scott',tabname =>
'EMP');

2. Hints for access paths:

Hints Name
Cluster(table)

Full(table)

Hash(table)

Rowid(table)
Index

Description
Its tell to the optimizer to
do cluster scan to access
the table.
This tells to the optimizer
to do a full scan of the
specified table
Tells to oracle to explicitly
chose the hash access
method for the table.
Forces rowid scan of the
specified table
Forces a index scan of
the specified table using
the specified index(s). if
list of indexes are
specified , the optimizer
choose the one with the
lowest cost. If no index
specified then optimizer

Index_Asc

Index_Desc

Index_combine

Index_join

Index_ss

Index_ffs

No_index

choose the available


index for the table with
the lowest cost.
Same as index, only
performs an ascending
search of the index
chosen if the statement
uses an index range
scan, Then oracle DB
scans the index entries in
ascending order of their
indexed values.
Same as index except
performs a descending
search.if more than one
table is accessed then
this is ignored.
Combines the bitmap
indexes on the tables .if
the cost shows that to do
so would give better
performance.
Instruct to the optimizer
to use index join as an
access path.
Instruct to the optimizer
to perform an index skip
scan for the specified
table.
Performs a fast full index
scan rather than table
scan.
Not to use one or more
indexes for the specified
table.

e.g:
Sql> select /*+ index(e emp_department_ix)
*/empid,ename,deptid from emp e where
deptno<=40;
In this above example emp_department_ix is a index
in deptno column of emp table and e is a table alias
of emp table.
Sql> select /*+ index_combine(e
emp_stats_btm_flag_ix job_id_bmp_index) */* from
emp e where emp_status=Training or job_id=Irprogramer;
In the above example emp_stats_btm_flag_ix and
job_id_bmp_index are both bitmap index for the
columns emp_status and job_id respectively.
Sql> select /*+ index_join(e emp_manager_indx
emp_dept_indx) */empid,ename from emp e where
empid<145 and deptno<60;
Sql> select /*+ index_ss(e emp_name_indx)
*/empno,ename from emp e where ename=King;
Sql> select /*+ index_ffs(e emp_name_indx)
*/ename from emp;
3. Hints for join operation:

Hints Name
Use_hash

Description
Forces the optimizer to join
each specified table with
another row source using a
hash join.

Use_merge

Use_nl

Use_nl_with_index

No_use_nl
No_use_hash
No_use_merge

Its instruct to optimizer to joi


each table with another row
source using a sort merge joi
Its instruct to the optimizer to
join each specified table to
another row source with
nested loops join using
specified table as an inner
table.
Its instruct to the optimizer to
join the specified table to
another row source with a
nested loops join using the
specified table as the inner
table and will use index.
Instruct to the optimizer not t
use nested loop joins.
Instruct not to use hash join
Instruct not to use sort merge
join.

Examples:
Sql> select /*+ use_nl_with_index(e empl_dept_index) */*
from empl e,dept d where e.deptno=d.deptno and
e.deptno<=40;
Sql> select /*+ no_use_hash(e d) */empid,ename from emp
e,dept d where e.deptno=d.deptno;

4. Hints for join orders:


Hints Name
Leading

Description
This hints causes oracle to use

the specified table as the first


table in the join order . This hin
is more versatile then ordered
hints.
When we put ordered hint from
a sql statement requiring a
join,the optimizer choose the
order in which to join the table

Ordered

Examples:
Sql> select /*+ leading(e s) */ * from emp e,dept
d ,sal_grade s
Where e.deptno=d.deptno and e.sal=j.hsal;
5. Hints for parallel execution:
Hints Name
Parallel

Parallel(Auto)

Parallel(Manual)

Parallel(Integer)

Description
This statement always
will run parallel and the
database computes the
degree or parallelism
which can be 2 or more.
The DB computes the
degree of parallelism,
which can be 1 or more.
If the computed
parallelism is 1, then the
statement runs serially.
The optimizer is forced
to use the parallel
setting of the objects in
the statement.
The optimizer uses the
degree of parallelism

Paralle_index

PQ_distribute

No_parallel

No_parallel_index

specified by integer.
Its instruct to the
optimizer to use
specified no of
concurrent server to
parallelize index range
scan, full scan and fast
full scans for patronized
indexes.
This can control the
distribution of rows for
parallel sql statement to
direct how rows should
be distributed between
the producer (Query)
and the consumer (Load)
server. It can improve
the parallel join
operation performance
when using partition
tables.
It overrides parallel
parameter in DDL thats
create or alter the table.
Overrides a parallel
parameter in DDL that
created or altered the
index thus avoiding the
parallel index scan
operation.

Examples:
Sql> select /*+ parallel */ empid,ename from emp;

Sql> select /*+ parallel(Manual) */empid,ename from


empl;
How to set parallelism of a table:
Sql> alter table empl parallel 4;
In case of manual parallel its use the parallelism
define for the table.
Sql> Select /*+ parallel(e,3) */ empid,ename from
emp e;
Sql> select /*+ no_parallel(e) */* from empl e;
6. Other Hints:
Hints name
Append

Append_values

Noappend

Cache

Description
Its instruct the optimizer to us
direct pth insert.(table must b
alter with nologin mode)
It instruct the optimizer to use
direct path insert with the
values clause. If you do not
specify this hints then
conventional insert can used.
Its a new hints in oracle 11g R
and its very useful in forall
insert in pl sql block.
It use conventional insert by
disable parallel mode for the
duration of the insert
statement.
Conventional insert is default
serial mode. The direct path
insert is default in parallel
mode.
This hint instructs the optimize
to place the blocks received fo
the table at the most recently

Nocache

Result_cache

Examples:
Sql> set autotrace off;
Sql> sho parameter result_cache;

used end of the LRU listin the


buffer cache when a full table
scan is performed. (small table
fetch from datafiles to
memory).
It instruct to the optimizer to
place the blocks retrieve for th
table at the least recently
used(LRU)end of the LRU listin
the buffer cache. When a full
table scans is performed. This
the normal behaviors of the
blocks in the buffer cache.
It instructs the oracle DB to
cache the results of current
query or query fragment in
memory and then to use the
cache results in the further
execution of the query or quer
fragments. Resides in the
result_cache memory partition
of the shared pool.
Result_cache hint requests
oracle to check if result for yo
query already resides in the
result_cache if so, oracle by
skip the fetch step of executio
and get rows directly from the
cache instead of reading the
rows from the DB buffers.
This memory occupy in SGA.

If its manual then we can go for Result_cache hints.


Sql> select /*+ result_cache */ empno,ename,dname
from empl e,dept d,salgrade sl where
e.deptno=d.deptno and sl.hsal=e.sal;
Regards,
Bimal Kinkar Das

You might also like