Академический Документы
Профессиональный Документы
Культура Документы
ss
Tamilselvan G
Be
In ac
Co f o t o n
rp ech
or
at
io
n
a
Cl
-2
2007-12-25
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
Semi Join
Anti Join
2007-12-25
2007-12-25
Pseudo code
For x in (select EMPID, BONUS from NEW_BONUS) loop
Look up the index EMP_UQ using x.EMPID,
if a matching KEY is found in EMP_UQ index, get the ROWID of EMP
Get the BONUS column from EMP for the ROWID
output the result
end if
End loop
Advantages of NL Join
1.
2.
3.
4.
Disadvantages of NL Join
1. If the result set is very large, then performance is horrible.
2. Not suitable for DW system because of processing large data sets.
Beacon Infotech Corporation
www.oracleact.com
2007-12-25
HINT Usage
SELECT /*+ LEADING(B) USE_NL(A) */
A.ID, A.NAME, B.DOB
FROM BIG_TABLE A, SMALL_TABLE B
WHERE A.ID = B.ID
Rows Row Source Operation
------- --------------------------------------------------20000 NESTED LOOPS (cr=44109 pr=3121 pw=0 time=1725511 us)
20000 TABLE ACCESS FULL SMALL_TABLE (cr=2774 pr=1540 pw=0 time=244921 us)
20000 TABLE ACCESS BY INDEX ROWID BIG_TABLE (cr=41335 pr=1581 pw=0 time=1405038 us)
20000 INDEX UNIQUE SCAN BIG_TABLE_IDX (cr=21335 pr=42 pw=0 time=637526 us)(object id 66371)
********************************************************************************
2007-12-25
cpu elapsed
-------- ---------0.05
0.04
0.00
0.00
0.02
0.01
-------- ---------0.07
0.06
2.
disk
query current rows
---------- ---------- ---------- ---------0
0
0
0
0
0
0
0
0
8
0
45
---------- ---------- ---------- ---------0
8
0
45
2007-12-25
2.Sort-Merge Join
1.
2.
3.
4.
2007-12-25
2.Sort-Merge Join
USE_MERGE HINT
SELECT /*+ USE_MERGE(A, B) */
COUNT(*), COUNT(A.ID), COUNT(B.DOB) , COUNT(A.NAME)
FROM BIG_TABLE A ,
SMALL_TABLE B
WHERE A.ID = B.ID
Rows Row Source Operation
------- --------------------------------------------------1 SORT AGGREGATE (cr=3087 pr=3081 pw=0 time=819538 us)
20000 MERGE JOIN (cr=3087 pr=3081 pw=0 time=811605 us)
20000 SORT JOIN (cr=1544 pr=1541 pw=0 time=322576 us)
20000 TABLE ACCESS FULL BIG_TABLE (cr=1544 pr=1541 pw=0 time=83240 us)
20000 SORT JOIN (cr=1543 pr=1540 pw=0 time=436453 us)
20000 TABLE ACCESS FULL SMALL_TABLE (cr=1543 pr=1540 pw=0 time=81751 us)
********************************************************************************
Note: The opposite of USE_MERGE is NO_USE_MERGE.
Event 10032 details about Sort, and event 10033 about Sort IO.
Beacon Infotech Corporation
www.oracleact.com
2007-12-25
3. Hash Join
call
count
-----------Parse
1
Execute 1
Fetch
2
------- -----total
4
query
current
rows
---------------------------0
0
0
0
0
0
1,004,659
0
1
------------- ---------- ---------1,004,659
0
1
INDEX UNIQUE SCAN CUSTOMER_IDX (cr=1000002 pr=0 pw=0 time=22641908 us)(object id 66385)
2007-12-25
select count(*)
from sales a, customer b
where a.cust_id = b.cust_id
call
count cpu elapsed
disk
query current
rows
------- ------ -------- ---------- ---------- ---------- ---------- ---------Parse
1
0.04
0.04
0
0
0
0
Execute 1
0.00
0.00
0
0
0
0
Fetch
2
5.64
5.51
4655
4701
0
1
------- ------------- ---------- ---------- ---------- ---------- ---------total
4
5.68
5.55
4655
4701
0
1
Rows
Row Source Operation
--------------------------------------------------------1
SORT AGGREGATE (cr=4716 pr=4655 pw=0 time=5511398 us)
1000000 HASH JOIN (cr=4716 pr=4655 pw=0 time=7098567 us)
20000
1000000
INDEX FAST FULL SCAN CUSTOMER_IDX (cr=44 pr=0 pw=0 time=20331 us)(object id 66385)
10
2007-12-25
3. Hash Join
Comparison
Nested
Loops
Join
# of LIO
1,004,659
Run Time
30.61
in Seconds
Hash Join
Hash Join
Performance
Improvement
4,701
Consumed 213
times less LIO
5.55
11
2007-12-25
3. Hash Join
12
2007-12-25
3. Hash Join
No latch is needed because the hash table is in private memory, hence cache
buffer chain latch is not acquired.
2.
3.
When joining one big data sets with smaller data sets
13
2007-12-25
3. Hash Join
Monitoring
SQL> select * from v$sysstat where name like 'workarea exec%'
STATISTIC# NAME
CLASS VALUE
--------------------------------------------- -----294
workarea executions - optimal
64
7423
295
workarea executions - onepass
64
5
296
workarea executions - multipass 64
0
14
STAT_ID
---------3211650785
798730793
3804491469
2007-12-25
3. Hash Join .
V$SQL_WORKAREA in 10g
V$SQL_WORKAREA_ACTIVE
Name
Null? Type
--------------------------------- -----------------ADDRESS
RAW(8)
HASH_VALUE
NUMBER
SQL_ID
VARCHAR2(13)
CHILD_NUMBER
NUMBER
WORKAREA_ADDRESS
RAW(8)
OPERATION_TYPE
VARCHAR2(20)
OPERATION_ID
NUMBER
POLICY
VARCHAR2(10)
ESTIMATED_OPTIMAL_SIZE
NUMBER
ESTIMATED_ONEPASS_SIZE
NUMBER
LAST_MEMORY_USED
NUMBER
LAST_EXECUTION
VARCHAR2(10)
LAST_DEGREE
NUMBER
TOTAL_EXECUTIONS
NUMBER
OPTIMAL_EXECUTIONS
NUMBER
ONEPASS_EXECUTIONS
NUMBER
MULTIPASSES_EXECUTIONS
NUMBER
ACTIVE_TIME
NUMBER
MAX_TEMPSEG_SIZE
NUMBER
LAST_TEMPSEG_SIZE
NUMBER
Name
-------------------------SQL_HASH_VALUE
SQL_ID
WORKAREA_ADDRESS
OPERATION_TYPE
OPERATION_ID
POLICY
SID
QCINST_ID
QCSID
ACTIVE_TIME
WORK_AREA_SIZE
EXPECTED_SIZE
ACTUAL_MEM_USED
MAX_MEM_USED
NUMBER_PASSES
TEMPSEG_SIZE
TABLESPACE
SEGRFNO#
SEGBLK#
15
Null? Type
-------- -----------------NUMBER
VARCHAR2(13)
RAW(8)
VARCHAR2(20)
NUMBER
VARCHAR2(6)
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
NUMBER
VARCHAR2(31)
NUMBER
NUMBER
2007-12-25
ST
-CA
NC
CA
MA
ST NAME
-- -----------------------------GA Georgia
CA California
NY New York
AL Alabama
TX Texas
MA Massachusetts
SC South Carolina
NC North Carolina
AZ Arizona
9 rows selected.
16
2007-12-25
17
2007-12-25
SUPPLIER
ST STATE
------------------------- -- ----------------------------ABC Inc
CA California
Microsoft
NC North Carolina
Oracle Corp
CA California
IBM Corp
MA Massachusetts
AZ Arizona
NY New York
GA Georgia
TX Texas
SC South Carolina
AL Alabama
10 rows selected.
Steps involved:
1.
2.
3.
4.
18
2007-12-25
5. Cartesian Join
2007-12-25
5. Cartesian Join
ENAME
--------------Tamil
Tandon
Muthu
Ravi
Veera
SALARY DEPTNO
---------- ---------900
10
1900
10
3100
20
4100
20
2700
30
PCT
---------7.09
14.96
24.41
32.28
21.26
20
We can change
the Cartesian
Product SQL into
Analytical
Function.
2007-12-25
6.Outer Join
DEPTNO
---------10
10
20
20
21
DEPTNO DEPTNAME
---------- ------------------10 Sales
10 Sales
20 Marketing
20 Marketing
30 Accounts -----
2007-12-25
6.Outer Join..
Types of Outer Joins: 1 Right Outer Join, 2 Left Outer Join, 3 Full Outer Join
ANSI SQL
SQL> SELECT A.ENAME,
NVL(A.DEPTNO, B.DEPTNO) DEPTNO,
B.DEPTNAME
FROM EMP A RIGHT OUTER JOIN DEPT B
ON (A.DEPTNO = B.DEPTNO) ;
DEPTNO DEPTNAME
--------- -----------10 Sales
10 Sales
20 Marketing
20 Marketing
30 Accounts
ENAME
--------------Tamil
Tandon
Muthu
Ravi
22
DEPTNO DEPTNAME
---------- -----------10
Sales
10
Sales
20
Marketing
20
Marketing
30
Accounts
2007-12-25
6.Outer Join..
ENAME
DEPTNO DEPTNAME
---------------
---------- ------------
Tamil
10
Sales
Tandon
10
Sales
Muthu
20
Marketing
Ravi
20
Marketing
Veera
23
2007-12-25
6.Outer Join..
ANSI SQL
3
b.deptname
4 from emp a , dept b
5 where a.deptno = b.deptno(+)
6 union
7 select a.ename,
8
b.deptno deptno ,
9
b.deptname
10 from emp a, dept b
11* where a.deptno(+) = b.deptno
SQL> /
ENAME
DEPTNO DEPTNAME
--------------- ---------- -----------Muthu
20 Marketing
Ravi
Tamil
Tandon
Veera
20 Marketing
10 Sales
10 Sales
DEPTNO DEPTNAME
--------- -----------10 Sales
10 Sales
20 Marketing
20 Marketing
30 Accounts
30 Accounts
6 rows selected.
Beacon Infotech Corporation
www.oracleact.com
6 rows selected.
24
2007-12-25
6.Outer Join..
25
2007-12-25
6.Outer Join..
26
2007-12-25
7. Hierarchical Join
MGRID
----------
----------
----------
1001 Scott
10
10600
1002 Tom
10
2500
1001
--------
1003 Veer
10
700
1002
--------
1004 Ram
10
500
1002
1005 Ragu
10
3100
1001
1006 Bush
10
800
1005
1007 Huss
10
600
1005
1008 Gopi
10
400
1005
---------- CEO
8 rows selected. Note the CUMSALARY column. This column stores individual salary for those
employees who do not have subordinate, but stores cumulative salary for those employees who
have subordinate plus his salary.
Now our requirement is list all employees individual salary.
27
2007-12-25
7. Hierarchical Join
28
2007-12-25
Pseudo columns
1
2
3
CONNECT_BY_ISCYCLE
CONNECT_BY_ISLEAF
LEVEL
EMPID NAME
MGRID
---------- ----------
----------
1001 Scott
1002 Tom
1001
1003 Kumar
1002
1004 Veera
1001
1005 Selvan
1004
7. Hierarchical Join
IsLeaf
LEVEL Path
---------- ---------- -------------------0
1
/Scott
0
2
/Scott/Tom
1
3
/Scott/Tom/Kumar
0
2
/Scott/Veera
1
3
/Scott/Veera/Selvan
29
2007-12-25
8. Sub Query
SALARY DEPTNO
BONUS
---------- ---------- ---------900
10
1900
10
3100
20
4100
20
2700
DEPTNO
---------20
BONUS
----------
30
2007-12-25
8. Sub Query
31
2007-12-25
8. Sub Query
IN Sub Query
SELECT * FROM EMP A
WHERE EMPNO
IN
( SELECT EMPNO FROM EMP_TEMP B)
Rows
------5
5
5
32
2007-12-25
9. Semi Join
33
2007-12-25
9. Semi Join
54 (6)| 00:00:01 |
------------------------------------------------------------------------------------------------------------------------------------
34
2007-12-25
9. Anti Join
Anti Join - An anti join is a query that returns rows in one table that
do not match some set of rows from another table.
NOT IN Sub Query
SELECT NAME, DOJ FROM CUSTOMER C
WHERE (NAME, DOJ) NOT IN
( SELECT NAME, DOJ FROM SUPPLIER S) ;
PLAN_TABLE_OUTPUT in 10g
-----------------------------------------------------------------------------------------------------------------------Plan hash value: 3490453085
-----------------------------------------------------------------------------------------------------------------------| Id | Operation
| Name
| Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------------------------------| 0 | SELECT STATEMENT |
| 50 | 1300 | 27791 (8)| 00:03:29 |
|* 1 | FILTER
|
|
|
|
|
|
| 2 | TABLE ACCESS FULL| CUSTOMER | 1000 | 26000 | 213 (6) | 00:00:02 |
|* 3 | TABLE ACCESS FULL| SUPPLIER | 13 | 195 | 55 (8) | 00:00:01 |
-------------------------------------------------------------------------------------------------------------------------
35
2007-12-25
cpu elapsed
-------- ---------0.04
0.06
0.00
0.00
0.48
0.45
------- ---------0.52
0.52
disk
query current
rows
---------- ---------- ---------- ---------0
0
0
0
0
0
0
0
1945
2018
0
15000
---------- ---------- ---------- ---------1945
2018
0
15000
36
2007-12-25
rows
---------0
0
15000
---------15000
********************************************************************************
Beacon Infotech Corporation
www.oracleact.com
37
2007-12-25
ID
---------1
5
7
9
ID NAME
---------- ---------5 ABC
6 ABC
Tom
COUNT(*)
---------0
Because there is a NULL value in a row in
T2 table.
Beacon Infotech Corporation
www.oracleact.com
COUNT(*)
---------3
38
2007-12-25
39
There is NO index on H2
table, but still the
elapsed time is 0.28
seconds. ANTI JOIN
using Outer Join is one
of the fastest way to get
the result set.
2007-12-25
40
2007-12-25
Among the various tests, this is the BEST RUN TIME in 9i..
Beacon Infotech Corporation
www.oracleact.com
41
1.
2.
3.
4.
5.
ALWAYS_ANTI_JOIN
parameter must be set
to HASH or MERGE.
2007-12-25
Category
Characteristics
Correlated /
Noncorrelated
Simple/Complex
Simple sub query contain a single table. Complex sub queries contain
many tables.
Aggregate
If a simple sub query contains some aggregation, then there are some
restrictions on how the optimizer may be able to transform them.
Single-row
IN/EXISTS
NOT IN / NOT
EXISTS
NOT IN sub query is not same as NOT EXISTS, but can be transformed
into ANTI-joins.
42
2007-12-25
Name
8i
9i
10g
Description
_UNNEST_NOT_EXISTS_SQ
n/a
Singl
e
n/a
_UNNEST_SUBQUERY
false
True
true
_ORDERED_SEMI_JOIN
True
True
True
_COST_EQUALITY_SEMI_JOIN
n/a
True
True
_ALWAYS_ANTI_JOIN
NL
Choo
se
Choose
_ALWAYS_SEMI_JOIN
Stand
ard
Choo
se
Choose
43
2007-12-25
Name
8i
9i
10g
Description
_OPTIMIZER_CORRECT_SQ_SEL
ECTIVITY
n/a
n/a
True
_OPTIMIZER_SQU_BOTTOMUP
n/a
n/a
True
_DISTINCT_VIEW_UNNESTING
n/a
n/a
False
10
_RIGHT_OUTER_HASH_ENABLE
n/a
n/a
True
11
_REMOVE_AGGR_SUBQUERY
n/a
n/a
True
44
2007-12-25
45
2007-12-25