SELECT LPAD(상품번호, 30) || LPAD(상품명, 30) || LPAD(고객id, 10)
|| LPAD(고객명, 20) || TO_CHAR(주문일시, 'yyyymmdd hh24:mi:ss')
FROM 주문상품
WHERE 주문일시 BETWEEN :start AND :end
ORDER BY 상품번호;
SELECT LPAD(상품번호, 30) || LPAD(상품명, 30) || LPAD(고객ID, 10)
|| LPAD(고객명, 20) || TO_CHAR(주문일시, 'YYYYMMDD HH24:MI:SS')
FROM (
SELECT 상품번호, 상품명, 고객ID, 고객명, 주문일시
FROM 주문상품
WHERE 주문일시 BETWEEN :START AND :END
ORDER BY 상품번호
);
SELECT TOP 10 거래일시, 채결건수, 체수량, 거래대금
FROM 시간대별종목거래
WHERE 종목코드 = 'KR123456'
AND 거래일시 >= '20080304';
SELECT 거래일시, 채결건수, 체수량, 거래대금
FROM 시간대별종목거래
WHERE 종목코드 = 'KR123456'
AND 거래일시 >= '20080304'
ORDER 거래일시
FETCH FIRST 10 ROWS ONLY;
SELECT *
FROM (
SELECT 거래일시, 채결건수, 체수량, 거래대금
WHERE 종목코드 = 'KR123456'
AND 거래일시 >= '20080304'
ORDER 거래일시
)
WHERE ROWNUM <= 10;
Execution Plan
------------------------------------------------------------------------------
0 SELECT STATEMENT Optimizer=ALL_ROWS
1 0 COUNT (STOPKEY)
2 1 VIEW
3 2 TABLE ACCESS (BY INDEX ROWID) OF '시간별종목거래' (TABLE)
4 3 INDEX (RANGE SCAN) OF '시간별종목거래_PK' (INDEX (UNIQUE))
SQL> create table t as select * from all_objects;
Table created.
SQL> alter session set workarea_size_policy = manual;
Session altered.
SQL> alter session set sort_area_size = 524288;
Session altered.
SQL> set autotrace traceonly statistics
SQL> select count(*) from t;
Statistics
----------------------------------------------------------
28 recursive calls
0 db block gets
773 consistent gets
687 physical reads
0 redo size
413 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SET AUTOTRACE ON
select *
from (
select * from t
order by object_name
)
where rownum <= 10 ;
Execution Plan
----------------------------------------------------------------------------------------
Plan hash value: 3299198703
----------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
----------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 1280 | | 2234 (1)| 00:00:22 |
|* 1 | COUNT STOPKEY | | | | | | |
| 2 | VIEW | | 59455 | 7431K| | 2234 (1)| 00:00:22 |
|* 3 | SORT ORDER BY STOPKEY| | 59455 | 7431K| 18M| 2234 (1)| 00:00:22 |
| 4 | TABLE ACCESS FULL | T | 59455 | 7431K| | 159 (2)| 00:00:02 |
----------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter(ROWNUM<=10)
3 - filter(ROWNUM<=10)
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
692 consistent gets
0 physical reads
0 redo size
1683 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
10 rows processed
SQL> select *
from (
select a.*, rownum no
from (
select * from t order by object_name
) a
)
where no <= 10 ;
10 rows selected.
Execution Plan
--------------------------------------------------------------------------------------
Plan hash value: 3902787780
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 59455 | 8186K| | 2234 (1)| 00:00:22 |
|* 1 | VIEW | | 59455 | 8186K| | 2234 (1)| 00:00:22 |
| 2 | COUNT | | | | | | |
| 3 | VIEW | | 59455 | 7431K| | 2234 (1)| 00:00:22 |
| 4 | SORT ORDER BY | | 59455 | 7431K| 18M| 2234 (1)| 00:00:22 |
| 5 | TABLE ACCESS FULL| T | 59455 | 7431K| | 159 (2)| 00:00:02 |
--------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("NO"<=10)
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
13 recursive calls
13 db block gets
850 consistent gets
699 physical reads
0 redo size
1750 bytes sent via SQL*Net to client
385 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
1 sorts (disk)
10 rows processed
SQL> create table tt
as
select 1 id, rownum seq, owner, object_name, object_type, created, status
from all_objects ;
Table created.
begin
for i in 1..9
loop
insert into tt
select i+1 id, rownum seq
, owner, object_name, object_type, created, status
from tt
where id = 1;
commit;
end loop;
end;
/
PL/SQL procedure successfully completed.
SQL> alter session set workarea_size_policy = manual;
SQL> alter session set sort_area_size = 1048576;
SQL> alter session set sql_trace = true;
SQL> alter session set tracefile_identifier='eun01';
SQL> select id, seq, owner, object_name, object_type, created, status
from (select id, seq
, max(seq) over (partition by id) last_seq
, owner, object_name, object_type, created, status
from tt)
where seq = last_seq;
$ TKPROF eunora_ora_18913_eun01.trc eun01.log SORT=(EXECPU) EXPLAIN = eun/loveora
==================================== eun01.log ==========================================
select id, seq, owner, object_name, object_type, created, status
from (select id, seq
, max(seq) over (partition by id) last_seq
, owner, object_name, object_type, created, status
from tt)
where seq = last_seq
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.01 0.01 0 1 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 3.85 6.44 13487 4536 9 10
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 3.86 6.46 13487 4537 9 10
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 68 (EUN)
Rows Row Source Operation
------- ---------------------------------------------------
10 VIEW (cr=4536 pr=13487 pw=8980 time=3669305 us)
500200 WINDOW SORT (cr=4536 pr=13487 pw=8980 time=23961167 us)
500200 TABLE ACCESS FULL TT (cr=4536 pr=0 pw=0 time=21008541 us)
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
10 VIEW
500200 WINDOW (SORT)
500200 TABLE ACCESS (FULL) OF 'TT' (TABLE)
SQL> alter session set workarea_size_policy = manual;
Session altered.
SQL> alter session set sort_area_size = 1048576;
Session altered.
SQL> alter session set sql_trace = true;
Session altered.
SQL> alter session set tracefile_identifier='eun02';
Session altered.
SQL> select id, seq, owner, object_name, object_type, created, status
from (select id, seq
, rank() over (partition by id order by seq desc) rnum
, owner, object_name, object_type, created, status
from tt)
where rnum = 1;
$ TKPROF eunora_ora_19087_eun02.trc eun02.log SORT=(EXECPU) EXPLAIN = eun/loveora
==================================== eun02.log ==========================================
select id, seq, owner, object_name, object_type, created, status
from (select id, seq
, rank() over (partition by id order by seq desc) rnum
, owner, object_name, object_type, created, status
from tt)
where rnum = 1
call count cpu elapsed disk query current rows
------- ------ -------- ---------- ---------- ---------- ---------- ----------
Parse 1 0.02 0.03 0 72 0 0
Execute 1 0.00 0.00 0 0 0 0
Fetch 2 2.36 2.63 37 4536 38 10
------- ------ -------- ---------- ---------- ---------- ---------- ----------
total 4 2.39 2.66 37 4608 38 10
Misses in library cache during parse: 1
Optimizer mode: ALL_ROWS
Parsing user id: 68 (EUN)
Rows Row Source Operation
------- ---------------------------------------------------------------
10 VIEW (cr=4536 pr=37 pw=37 time=2576319 us)
118 WINDOW SORT PUSHED RANK (cr=4536 pr=37 pw=37 time=2575430 us)
500200 TABLE ACCESS FULL TT (cr=4536 pr=0 pw=0 time=19507973 us)
Rows Execution Plan
------- ---------------------------------------------------
0 SELECT STATEMENT MODE: ALL_ROWS
10 VIEW
118 WINDOW (SORT PUSHED RANK)
500200 TABLE ACCESS (FULL) OF 'TT' (TABLE)
- 강좌 URL : http://www.gurubee.net/lecture/3302
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.