alter session set hash_join_enabled = false;
create table t1 as
with generator as (
select --+ materialize
rownum id
from all_objects
where rownum <= 3000
)
select rownum id,
rownum n1,
trunc((rownum - 1)/2) n2,
lpad(rownum,10,'0') small_vc,
rpad('x',100,'x') padding
from generator v1,
generator v2
where rownum <= 10000;
alter table t1 add constraint t1_pk primary key(id);
select count(distinct t1_vc || t2_vc)
from (select /*+ no_merge ordered use_merge(t2) */
t1.small_vc t1_vc
t2.small_vc t2_vc
from t1,
t2,
where
t1.n1 <= 1000 and t2.id = t1.id -- 1번
t1.n1 <= 1000 and t2.n2 = t1.id -- 2번
t1.n1 <= 1000 and t2.id between t1.id -1 and t1.id +1 -- 3번
t1.n1 <= 1000 and t2.n2 = t1.id -- 4번
t1.n1 <= 1000 and t2.id = t1.id -- 5번
);
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 26 | 3 (34)| 00:00:01 |
| 1 | SORT ORDER BY | | 1 | 26 | 3 (34)| 00:00:01 |
| 2 | TABLE ACCESS FULL| T1 | 1 | 26 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------
select col1, col2 from t1 order by col1, col2;
| 1 | SORT ORDER BY | | 1 | 26 | 3 (34)| 00:00:01 |
select distinct col1, col2 from t1;
| 1 | HASH UNIQUE | | 1 | 26 | 3 (34)| 00:00:01 | <- 책은 Sort (unique)
select col1, col2, count(*) from t1 group by col1, col2;
| 1 | SORT GROUP BY | | 1 | 26 | 3 (34)| 00:00:01 |
select col1, col2, count(*) from t1 group by col1, col2 order by col1, col2;
| 1 | SORT GROUP BY | | 1 | 26 | 3 (34)| 00:00:01 |
select max(col1), max(col2) from t1;
| 1 | SORT AGGREGATE | | 1 | 26 | | |
alter session set "_optimizer_cost_model"=io
drop table t1 purge;
create table t1 as
select rownum id, ao.*
from all_objects ao
where rownum <= 2500;
drop table t2 purge;
create table t2 as
select rownum id, ao.*
from all_objects ao
where rownum <= 2000;
- 각각 집합에 대한 유일성을 강제한 후 집합연산을 하는 경우의 쿼리
select distinct owner, object_type from t1
intersect
select distinct owner, object_type from t2;
- 집합연산자는 unique한 결과를 반환한다는 점을 반영한 쿼리
select owner, object_type from t1
intersect
select owner, object_type from t2;
--위 쿼리
-----------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost
-----------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 123K| 45
| 1 | INTERSECTION | | | |
| 2 | SORT UNIQUE | | 2500 | 70000 | 24
| 3 | TABLE ACCESS FULL| T1 | 2500 | 70000 | 5
| 4 | SORT UNIQUE | | 2000 | 56000 | 21
| 5 | TABLE ACCESS FULL| T2 | 2000 | 56000 | 4
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 123K| 20 (50)| 00:00:01 |
| 1 | INTERSECTION | | | | | |
| 2 | SORT UNIQUE | | 2500 | 70000 | 11 (10)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T1 | 2500 | 70000 | 10 (0)| 00:00:01 |
| 4 | SORT UNIQUE | | 2000 | 56000 | 9 (12)| 00:00:01 |
| 5 | TABLE ACCESS FULL| T2 | 2000 | 56000 | 8 (0)| 00:00:01 |
----------------------------------------------------------------------------
--아래 쿼리
------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost |
------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 123K| 45 |
| 1 | INTERSECTION | | | | |
| 2 | SORT UNIQUE | | 2500 | 70000 | 24 |
| 3 | TABLE ACCESS FULL| T1 | 2500 | 70000 | 5 |
| 4 | SORT UNIQUE | | 2000 | 56000 | 21 |
| 5 | TABLE ACCESS FULL| T2 | 2000 | 56000 | 4 |
------------------------------------------------------------
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2000 | 123K| 20 (50)| 00:00:01 |
| 1 | INTERSECTION | | | | | |
| 2 | SORT UNIQUE | | 2500 | 70000 | 11 (10)| 00:00:01 |
| 3 | TABLE ACCESS FULL| T1 | 2500 | 70000 | 10 (0)| 00:00:01 |
| 4 | SORT UNIQUE | | 2000 | 56000 | 9 (12)| 00:00:01 |
| 5 | TABLE ACCESS FULL| T2 | 2000 | 56000 | 8 (0)| 00:00:01 |
----------------------------------------------------------------------------
alter session set "_optimizer_cost_model"=cpu;
alter session set statistics_level = all;
alter session set optimizer_features_enable = '9.2.0';
explain plan for
create table t_intersect as
select distinct *
from ( select owner, object_type from t1
intersect
select owner, object_type from t2
);
select * from table(dbms_xplan.display());
----------------------------------------------
| Id | Operation | Name |
----------------------------------------------
| 0 | CREATE TABLE STATEMENT | |
| 1 | LOAD AS SELECT | T_INTERSECT |
| 2 | SORT UNIQUE | |
| 3 | VIEW | |
| 4 | INTERSECTION | |
| 5 | SORT UNIQUE | |
| 6 | TABLE ACCESS FULL| T1 |
| 7 | SORT UNIQUE | |
| 8 | TABLE ACCESS FULL| T2 |
----------------------------------------------
alter session set optimizer_features_enable = '10.2.0.1';
explain plan for
create table t_intersect as
select distinct *
from ( select owner, object_type from t1
intersect
select owner, object_type from t2
);
select * from table(dbms_xplan.display());
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | CREATE TABLE STATEMENT | | 2000 | 56000 | 30 (10)| 00:00:01 |
| 1 | LOAD AS SELECT | T_INTERSECT | | | | |
| 2 | HASH UNIQUE | | 2000 | 56000 | 27 (12)| 00:00:01 |
| 3 | VIEW | | 2000 | 56000 | 26 (8)| 00:00:01 |
| 4 | INTERSECTION | | | | | |
| 5 | SORT UNIQUE | | 2500 | 70000 | 11 (10)| 00:00:01 |
| 6 | TABLE ACCESS FULL| T1 | 2500 | 70000 | 10 (0)| 00:00:01 |
| 7 | SORT UNIQUE | | 2000 | 56000 | 9 (12)| 00:00:01 |
| 8 | TABLE ACCESS FULL| T2 | 2000 | 56000 | 8 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
- 강좌 URL : http://www.gurubee.net/lecture/3986
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.