Question: I want to replace a
nested loops join with a hash join, and I need the syntax
for a hash join hint. How do I add a hash join hint to
my SQL?
Answer:
The use_hash hint is used to force a hash join in
Oracle SQL and you add the use_hash hint as follows:
select
/*+ use_hash */
col1 . . . ;
Please note that you must have enough PGA RAM to enable
hash joins. If yur database does not have enough RAM
to allow Oracle to perform a hash join, then the hash join
hint will be ignored because Oracle does not have the
computing resources.
The
use_hash hint requests a hash join against the
specified tables. Essentially, a hash join is a technique
whereby Oracle loads the rows from the driving table (the
smallest table, first after the where clause) into a
RAM area defined by the hash_area_size session
parameter, but the PGA areas regions are specific to your
release. In 12c the parameter is memory_target
if you are using automatic memory management.
Oracle
then uses a hashing technique to locate the rows in the
larger second table. As I mention in Chapter 10, a hash join
is often combined with parallel query in cases where both
tables are very large.
The following query is an example of
a query that has been hinted to force a hash join with
parallel query:
select /*+
use_hash(e,b) parallel(e, 4) parallel(b, 4) */
e.ename,
hiredate,
b.comm
from
emp e,
bonus b
where
e.ename = b.ename
;
Here is the execution plan for the hash join.
Note that both tables in this join are using parallel query
to obtain their rows:
OPERATION
----------------------------------------------------------------------
OPTIONS OBJECT_NAME POSITION
------------------------------ ----------------------------
----------
SELECT STATEMENT
3
HASH JOIN
1
PARALLEL_TO_SERIAL
TABLE ACCESS
FULL EMP 1
PARALLEL_TO_PARALLEL
TABLE ACCESS
FULL BONUS 2
Hash joins are often faster than
nested loops joins, especially in cases where the driving
table is filtered into a small number of rows in the query's
where clause.
Enabling Your Database to
Accept the use_hash Hint
The use_hash hint is very
finicky, and there are many conditions that must be
satisfied. It is not uncommon to find that a use_hash
hint is ignored and here are some common causes of this
problem.
-
Check initialization parameters
Make sure that you have the proper settings for
optimizer_index_cost_adj, hash_multiblock_io_count,
optimizer_max_permutations, and
hash_area_size. You can see Chapter 16 for details
on setting these parameters.
-
Verify driving table Make
sure that the smaller table is the driving table (the
first table in the from clause). This is because
a hash join builds the memory array using the driving
table.
-
Analyze CBO statistics Check
that tables and/or columns of the join tables are
appropriately analyzed.
-
Check for skewed columns
Histograms are recommended only for non-uniform column
distributions. If necessary, you can override the join
order chosen by the cost-based optimizer using the
ordered hint.
-
Check RAM region Ensure that
hash_area_size is large enough to hold the
smaller table in memory. Otherwise, Oracle must write to
the TEMP tablespace, slowing down the hash join.
|
|
|
Oracle Training from Don Burleson
The best on site
"Oracle
training classes" are just a phone call away! You can get personalized Oracle training by Donald Burleson, right at your shop!

|
|
|
|
|
Burleson is the American Team

Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|