-- create object
DROP TABLE T1 PURGE;
CREATE TABLE T1(C1 VARCHAR2(1), C2 INT);
-- create data
INSERT INTO T1
SELECT 'A', 1
FROM DUAL
CONNECT BY LEVEL <= 1000
UNION ALL
SELECT 'B', 2
FROM DUAL
CONNECT BY LEVEL <= 100
UNION ALL
SELECT 'C', 2
FROM DUAL
CONNECT BY LEVEL <= 100
;
COMMIT;
-- gather stats "with" histogram
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'T1', METHOD_OPT => 'FOR ALL COLUMNS SIZE SKEWONLY', NO_INVALIDATE => FALSE);
-- Table
SELECT TABLE_NAME,
NUM_ROWS,
BLOCKS,
SAMPLE_SIZE,
TO_CHAR(LAST_ANALYZED, 'YYYY/MM/DD HH24:MI:SS') AS LAST_ANAL
FROM USER_TAB_STATISTICS
WHERE TABLE_NAME = UPPER('T1')
;
TABLE_NAME | NUM_ROWS | BLOCKS | SAMPLE_SIZE | LAST_ANAL |
---|---|---|---|---|
T1 | 1200 | 13 | 1200 | 2009-03-01 2:39:21 PM |
-- Column
SELECT S.TABLE_NAME,
S.COLUMN_NAME,
S.NUM_DISTINCT,
S.NUM_NULLS,
S.DENSITY,
S.LOW_VALUE,
S.HIGH_VALUE,
S.HISTOGRAM
FROM USER_TAB_COLS S
WHERE S.TABLE_NAME = UPPER('T1')
;
TABLE_NAME | COLUMN_NAME | NUM_DISTINCT | NUM_NULLS | DENSITY | LOW_VALUE | HIGH_VALUE | HISTOGRAM |
---|---|---|---|---|---|---|---|
T1 | C2 | 2 | 0 | 0.000416666666666667 | C102 | C103 | FREQUENCY |
T1 | C1 | 3 | 0 | 0.000416666666666667 | 41 | 43 | FREQUENCY |
-- Histogram
SELECT TABLE_NAME,
COLUMN_NAME,
ENDPOINT_NUMBER,
ENDPOINT_VALUE || '(' || ENDPOINT_ACTUAL_VALUE || ')' AS ENDPOINT_VALUE
FROM USER_TAB_HISTOGRAMS
WHERE TABLE_NAME = UPPER('T1')
ORDER BY COLUMN_NAME,
ENDPOINT_NUMBER
;
TABLE_NAME | COLUMN_NAME | ENDPOINT_NUMBER | ENDPOINT_VALUE |
---|---|---|---|
T1 | C1 | 1000 | 337499295804764000000000000000000000() |
T1 | C1 | 1100 | 342691592663299000000000000000000000() |
T1 | C1 | 1200 | 347883889521833000000000000000000000() |
T1 | C2 | 1000 | 1() |
T1 | C2 | 1200 | 2() |
SELECT /*+ GATHER_PLAN_STATISTICS */
COUNT(*)
FROM T1
WHERE C1 = 'A'
;
select * from table(dbms_xplan.display_cursor(null, null, 'iostats cost last'));
--------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | Cost (%CPU)| A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | | 1 |00:00:00.01 | 15 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 1000 | 5 (0)| 1000 |00:00:00.01 | 15 |
--------------------------------------------------------------------------------------------------
;
SELECT /*+ GATHER_PLAN_STATISTICS */
COUNT(*)
FROM T1
WHERE C1 = 'A'
AND C2 = 1
;
select * from table(dbms_xplan.display_cursor(null, null, 'iostats cost last'));
--------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | Cost (%CPU)| A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | | 1 |00:00:00.01 | 15 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 833 | 5 (0)| 1000 |00:00:00.01 | 15 |
--------------------------------------------------------------------------------------------------
;
SELECT /*+ GATHER_PLAN_STATISTICS */
COUNT(*)
FROM T1
WHERE C1 = 'A'
AND C2 = 2
;
select * from table(dbms_xplan.display_cursor(null, null, 'iostats cost last'));
--------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | Cost (%CPU)| A-Rows | A-Time | Buffers |
--------------------------------------------------------------------------------------------------
| 1 | SORT AGGREGATE | | 1 | 1 | | 1 |00:00:00.01 | 15 |
|* 2 | TABLE ACCESS FULL| T1 | 1 | 166 | 5 (0)| 0 |00:00:00.01 | 15 |
--------------------------------------------------------------------------------------------------
;
EXEC DBMS_STATS.GATHER_TABLE_STATS(USER, 'T1', METHOD_OPT => 'FOR COLUMNS (C1, C2) SIZE SKEWONLY', NO_INVALIDATE => FALSE);
- 강좌 URL : http://www.gurubee.net/lecture/4425
- 구루비 강좌는 개인의 학습용으로만 사용 할 수 있으며, 다른 웹 페이지에 게재할 경우에는 출처를 꼭 밝혀 주시면 고맙겠습니다.~^^
- 구루비 강좌는 서비스 제공을 위한 목적이나, 학원 홍보, 수익을 얻기 위한 용도로 사용 할 수 없습니다.