October 2005
Intermediate to advanced
454 pages
14h 44m
English
Table functions can also be used to help queries perform better by adding application knowledge to a query. The classic example is the multiple “OR EXISTS” type of query shown in this example.
SELECT *
FROM main_table mt
WHERE col1 = 1
AND ( EXISTS ( SELECT 1
FROM or_table_one
WHERE col11 = mt.col1 )
OR EXISTS ( SELECT 1
FROM or_table_two
WHERE col21 = mt.col1 )
OR EXISTS ( SELECT 1
FROM or_table_three
WHERE col31 = mt.col1 ) );It will return a record if a corresponding record is found in any of three other tables. The AUTOTRACE output for the query shows that the Oracle optimizer looked at each table involved in order to get the single resultant record.
Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=ALL_ROWS (Cost=1 Card=1 Bytes=4) 1 0 FILTER 2 1 TABLE ACCESS (BY INDEX ROWID) OF 'MAIN_TABLE' (TABLE) (Cost=1 Card=1 Bytes=4) 3 2 INDEX (UNIQUE SCAN) OF 'SYS_C003477' (INDEX (UNIQUE)) (Cost=0 Card=1) 4 1 INDEX (UNIQUE SCAN) OF 'SYS_C003479' (INDEX (UNIQUE)) (Cost=0 Card=1 Bytes=2) 5 1 INDEX (UNIQUE SCAN) OF 'SYS_C003481' (INDEX (UNIQUE)) (Cost=1 Card=1 Bytes=3) 6 1 INDEX (UNIQUE SCAN) OF 'SYS_C003483' (INDEX (UNIQUE)) (Cost=1 Card=1 Bytes=3) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 13 consistent gets 0 physical reads 0 redo size 446 bytes sent via SQL*Net to client 511 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client ...