Documente Academic
Documente Profesional
Documente Cultură
When using rule-based optimization, you can determine the performance of a rule-based query
by understanding which table is the driving table and how many rows each part returns. Find out
how with this hands-on solution.
When coding a SQL statement with tables in master-detail relationships, it's common to
have to decide whether to write the query using the WHERE EXISTS (. . .) clause or the
WHERE value IN (. . .) clause. You may resist using WHERE EXISTS because it has the
awkward syntax of returning a value, which you always ignore.
However, there's a difference when using rule-based optimization. You can determine the
performance of a rule-based query by understanding which table is the driving table and how
many rows each part returns.
When you write a query using the IN clause, you're telling the rule-based optimizer that you
want the inner query to drive the outer query (think: IN = inside to outside). For example, to
query the 14-row EMP table for the direct reports to the employee KING, you could write the
following:
select ename from emp e
where mgr in (select empno from emp where ename = 'KING');
You can write the same query using EXISTS by moving the outer query column to a
subquery condition, like this:
select ename from emp e
where exists (select 0 from emp where e.mgr = empno and ename = 'KING');
When you write EXISTS in a where clause, you're telling the optimizer that you want the
outer query to be run first, using each value to fetch a value from the inner query (think:
EXISTS = outside to inside).
The EXPLAIN PLAN result for the query is:
OBJECT OPERATION
---------- ---------------------------------------SELECT STATEMENT()
FILTER()
EMP
TABLE ACCESS(FULL)
EMP
TABLE ACCESS(BY INDEX ROWID)
PK_EMP
INDEX(UNIQUE SCAN)