You are on page 1of 13

Oracle 8 semi-joins

 Optimizes queries using EXISTS where there is no


supporting index
select * No index on employees
from customers c
where exists
(select 1 from employees e
where e.surname=c.contact_surname
and e.firstname=c.contact_firstname
and e.date_of_birth=c.date_of_birth)
Oracle 8 semi-joins

 Without the semi-join or supporting index, queries like


the one on the preceding slide will perform very badly.
 Oracle will perform a tablescan of the inner table for
each row retrieved by the outer table
 If customers has 100,000 rows, and employees 800
rows then 80 MILLION rows will be processed!
 In Oracle7, you should create the index or use an IN-
based subquery
 In Oracle8, the semi-join facility allows the query to be
resolved by a sort-merge or hash join.
To Use semi-joins

 Set ALWAYS_SEMI_JOIN=HASH or MERGE in


INIT.ORA, OR
 Use a MERGE_SJ or HASH_SJ hint in the subquery
of the SQL statement
SELECT *
FROM customers c
WHERE exists
(select /*+merge_sj*/ 1
from employees e
where ….)
Oracle8 semi-joins

 The performance improvements are impressive (note


the logarithmic scale)
1,343.19
EXISTS no semi-join or indexes

31.01
EXISTS no semi-join but with index

6.83
EXISTS - merge semi-join

6.69
IN-based subquery

1 10 100 1,000 10,000

Elapsed time (logarithmic scale)


Star Join improvements

 A STAR join involves a large “FACT” table being


joined to a number of smaller “dimension” tables
Star Join improvements

 The Oracle7 Star join algorithm works well when there is a


concatenated index on all the FACT table columns
 But when there are a large number of dimensions, creating
concatenated indexes for all possible queries is impossible.
 Oracle8’s “Star transformation” involves re-wording the query
so that it can be supported by combinations of bitmap indexes.
 Since bitmap indexes can be efficiently combined, a single
bitmap index on each column can support all possible queries.
To enable the star transformation

 Create bitmap indexes on each of the FACT table


columns which are used in star queries
 Make sure that
STAR_TRANSFORMATION_ENABLED is TRUE,
either by changing init.ora or using an ALTER
SESSION statement.
 Use the STAR_TRANSFORMATION hint if necessary.
Drawback of Star transformation

 Bitmap indexes reduce concurrency (row-level locking


may break down).

 But remember that large number of distinct column


values may not matter
Star transformation performance

 When there is no suitable concatenated index, the


Star transformation results in a significant
improvement
0.35

No suitable concatenated index


9.94

Star_transformation
Star
0.24

Concatenated index
0.01

0 1 2 3 4 5 6 7 8 9 10

Elapsed time (s)


Hint #9: Use ARRAY processing
– Retrieve or insert rows in batches, rather than one at a time.
– Methods of doing this are language specific

60

50

40
Elapsed time

30

20

10

0
0 20 40 60 80 100 120 140 160 180 200 220 240 260 280 300
Array size
Hint #10: Consider PL/SQL for
“tricky” SQL
 With SQL you specify the data you want, not how to
get it. Sometime you need to specifically dictate your
retrieval algorithms.
 For example:
– Getting the second highest value
– Doing lookups on a low-high lookup table
– Correlated updates
– SQL with multiple complex correlated subqueries
– SQL that seems to hard to optimize unless it is broken into
multiple queries linked in PL/SQL
Oracle8i PL/SQL Improvements

– Array processing

– NOCOPY

– Temporary tables

– The profiler

– Dynamic SQL

You might also like