프린트 하기

OS 환경 : Oracle Linux 8.4 (64bit)

 

DB 환경 : Oracle Database 19.3.0.0

 

방법 : DBMS_SPACE.UNUSED_SPACE 한번에 여러 세그먼트 조회

DBMS_SPACE.UNUSED_SPACE 조회

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
SQL>
set lines 200 pages 1000
set serveroutput on
DECLARE
    v_total_blocks       NUMBER;
    v_total_bytes        NUMBER;
    v_unused_blocks      NUMBER;
    v_unused_bytes       NUMBER;
    v_last_used_extent_file_id  NUMBER;
    v_last_used_extent_block_id NUMBER;
    v_last_used_block  NUMBER;
BEGIN
    -- 테이블 헤더 출력
    DBMS_OUTPUT.PUT_LINE('OWNER           SEGMENT_NAME    SEGMENT_TYPE        TOTAL_BLOCKS      TOTAL_MB  UNUSED_BLOCKS      UNUSED_MB');
    DBMS_OUTPUT.PUT_LINE('--------------- --------------- ------------------ ------------- ------------- -------------- --------------');
    
    -- DBA_SEGMENTS에서 테이블 및 인덱스 세그먼트 가져오기
    FOR rec IN (SELECT owner, segment_name, segment_type
                  FROM dba_segments
                 WHERE owner = 'IMSI'
                   AND segment_name LIKE 'SAMPLE_T%')
    LOOP
        BEGIN
            -- DBMS_SPACE.UNUSED_SPACE를 호출하여 세그먼트의 사용 공간 정보 가져오기
            DBMS_SPACE.UNUSED_SPACE(
                segment_owner       => rec.owner,
                segment_name        => rec.segment_name,
                segment_type        => rec.segment_type,
                total_blocks        => v_total_blocks,
                total_bytes         => v_total_bytes,
                unused_blocks       => v_unused_blocks,
                unused_bytes        => v_unused_bytes,
                last_used_extent_file_id  => v_last_used_extent_file_id,
                last_used_extent_block_id => v_last_used_extent_block_id,
                last_used_block     => v_last_used_block
            );
            
            -- 각 세그먼트에 대한 정보를 출력
            DBMS_OUTPUT.PUT_LINE(RPAD(rec.owner, 15|| ' ' ||
                                 RPAD(rec.segment_name, 15|| ' ' ||
                                 RPAD(rec.segment_type, 18|| ' ' ||
                                 LPAD(v_total_blocks, 13|| ' ' ||
                                 LPAD(TO_CHAR(ROUND(v_total_bytes / 1024 / 10242), '99990.00'), 13|| ' ' ||
                                 LPAD(v_unused_blocks, 14|| ' ' ||
                                 LPAD(TO_CHAR(ROUND(v_unused_bytes / 1024 / 10242), '99990.00'), 14));
        EXCEPTION
            -- 에러 발생 시 예외 처리 (예: 세그먼트가 분석 불가할 경우)
            WHEN OTHERS THEN
                DBMS_OUTPUT.PUT_LINE('Error processing ' || rec.segment_name || ' (' || rec.segment_type || ')');
        END;
    END LOOP;
END;
/
 
OWNER           SEGMENT_NAME    SEGMENT_TYPE        TOTAL_BLOCKS      TOTAL_MB  UNUSED_BLOCKS      UNUSED_MB
--------------- --------------- ------------------ ------------- ------------- -------------- --------------
IMSI            SAMPLE_T1       TABLE                     118784        928.00              0           0.00
IMSI            SAMPLE_T2       TABLE                     118784        928.00            719           5.62
IMSI            SAMPLE_T3       TABLE                     118784        928.00            719           5.62
IMSI            SAMPLE_T4       TABLE                      26624        208.00            333           2.60
IMSI            SAMPLE_T4_IX1   INDEX                      35840        280.00            863           6.74
IMSI            SAMPLE_T5       TABLE                      26624        208.00            333           2.60
IMSI            SAMPLE_T5_IX1   INDEX                      35840        280.00            863           6.74
IMSI            SAMPLE_T6       TABLE                      26624        208.00            333           2.60
IMSI            SAMPLE_T6_IX1   INDEX                      35840        280.00            863           6.74
 
PL/SQL procedure successfully completed.

 

 

참조 :