1、 show_space 源代码
CREATE OR REPLACE PROCEDURE show_space (
p_segname_1 IN VARCHAR2,
p_type_1 IN VARCHAR2 DEFAULT 'TABLE',
p_space IN VARCHAR2 DEFAULT 'MANUAL',
p_analyzed IN VARCHAR2 DEFAULT 'N',
p_partition_1 IN VARCHAR2 DEFAULT NULL,
p_owner_1 IN VARCHAR2 DEFAULT USER)
AUTHID CURRENT_USER
AS
p_segname VARCHAR2 (100);
p_type VARCHAR2 (30);
p_owner VARCHAR2 (30);
p_partition VARCHAR2 (50);
l_unformatted_blocks NUMBER;
l_unformatted_bytes NUMBER;
l_fs1_blocks NUMBER;
l_fs1_bytes NUMBER;
l_fs2_blocks NUMBER;
l_fs2_bytes NUMBER;
l_fs3_blocks NUMBER;
l_fs3_bytes NUMBER;
l_fs4_blocks NUMBER;
l_fs4_bytes NUMBER;
l_full_blocks NUMBER;
l_full_bytes NUMBER;
l_free_blks NUMBER;
l_total_blocks NUMBER;
l_total_bytes NUMBER;
l_unused_blocks NUMBER;
l_unused_bytes NUMBER;
l_LastUsedExtFileId NUMBER;
l_LastUsedExtBlockId NUMBER;
l_LAST_USED_BLOCK NUMBER;
PROCEDURE p (p_label IN VARCHAR2,p_num IN NUMBER)
IS
BEGIN
DBMS_OUTPUT.put_line (RPAD(p_label, 40, '.') || p_num);
END;
BEGIN
p_segname := UPPER (p_segname_1);
p_owner := UPPER (p_owner_1);
p_type := p_type_1;
p_partition := UPPER(p_partition_1);
IF (p_type_1 = 'i' OR p_type_1 ='I')
THEN
p_type := 'INDEX';
END IF;
IF (p_type_1 = 't' OR p_type_1 ='T')
THEN
p_type := 'TABLE';
END IF;
IF (p_type_1 = 'tp' OR p_type_1 ='TP')
THEN
p_type := 'TABLE PARTITION';
END IF;
IF (p_type_1 = 'ip' OR p_type_1 = 'IP')
THEN
p_type := 'INDEX PARTITION';
END IF;
IF (p_type_1 = 'c' OR p_type_1 ='C')
THEN
p_type := 'CLUSTER';
END IF;
DBMS_SPACE.UNUSED_SPACE (
segment_owner => p_owner,
segment_name => p_segname,
segment_type => p_type,
partition_name => p_partition,
total_blocks => l_total_blocks,
total_bytes => l_total_bytes,
unused_blocks => l_unused_blocks,
unused_bytes => l_unused_bytes,
LAST_USED_EXTENT_FILE_ID => l_LastUsedExtFileId,
LAST_USED_EXTENT_BLOCK_ID => l_LastUsedExtBlockId,
LAST_USED_BLOCK => l_LAST_USED_BLOCK);
IF p_space = 'MANUAL' OR (p_space<> 'auto' AND p_space <> 'AUTO')
THEN
DBMS_SPACE.FREE_BLOCKS (segment_owner => p_owner,
segment_name =>p_segname,
segment_type => p_type,
partition_name =>p_partition,
freelist_group_id => 0,
free_blks =>l_free_blks);
p ('Free Blocks', l_free_blks);
END IF;
p ('Total Blocks',l_total_blocks);
p ('Total Bytes', l_total_bytes);
p ('Unused Blocks',l_unused_blocks);
p ('Unused Bytes',l_unused_bytes);
p ('Last Used Ext FileId',l_LastUsedExtFileId);
p ('Last Used Ext BlockId', l_LastUsedExtBlockId);
p ('Last Used Block',l_LAST_USED_BLOCK);
/*IF the segment is analyzed */
IF p_analyzed = 'Y'
THEN
DBMS_SPACE.SPACE_USAGE(segment_owner => p_owner,
segment_name => p_segname,
segment_type => p_type,
partition_name =>p_partition,
unformatted_blocks => l_unformatted_blocks,
unformatted_bytes =>l_unformatted_bytes,
fs1_blocks =>l_fs1_blocks,
fs1_bytes =>l_fs1_bytes,
fs2_blocks =>l_fs2_blocks,
fs2_bytes => l_fs2_bytes,
fs3_blocks =>l_fs3_blocks,
fs3_bytes =>l_fs3_bytes,
fs4_blocks =>l_fs4_blocks,
fs4_bytes => l_fs4_bytes,
full_blocks =>l_full_blocks,
full_bytes =>l_full_bytes);
DBMS_OUTPUT.put_line (RPAD ('', 50, '*'));
DBMS_OUTPUT.put_line ('Thesegment is analyzed');
p ('0% -- 25% free spaceblocks', l_fs1_blocks);
p ('0% -- 25% free spacebytes', l_fs1_bytes);
p ('25% -- 50% free spaceblocks', l_fs2_blocks);
p ('25% -- 50% free spacebytes', l_fs2_bytes);
p ('50% -- 75% free spaceblocks', l_fs3_blocks);
p ('50% -- 75% free spacebytes', l_fs3_bytes);
p ('75% -- 100% free spaceblocks', l_fs4_blocks);
p ('75% -- 100% free spacebytes', l_fs4_bytes);
p ('Unused Blocks', l_unformatted_blocks);
p ('Unused Bytes',l_unformatted_bytes);
p ('Total Blocks',l_full_blocks);
p ('Total bytes',l_full_bytes);
END IF;
END;
DBMS_SPACE.SPACE_USAGE过程只适合于 assm管理的表空间
DBMS_SPACE.FREE_BLOCKS 过程(只用MSSM管理的表空间)
mssm 管理表空间
SQL> create tablespace test_m datafile 'D:\APP\ADMINISTRATOR\ORADATA\ORA11G\test_m.DBF' size 20M extent management local segment space management manual;
表空间已创建。
SQL> exec show_space('T_M','T','MANUAL')
Free Blocks.............................0 --由DBMS_SPACE.FREE_BLOCKS输出
Total Blocks............................1152 --以下由DBMS_SPACE.UNUSED_SPACE输出
Total Bytes.............................9437184
Unused Blocks...........................125
Unused Bytes............................1024000
Last Used Ext FileId....................6
Last Used Ext BlockId...................1152
Last Used Block.........................3
PL/SQL 过程已成功完成。
SQL> exec show_space('ind_M','I','MANUAL');
Free Blocks.............................0
Total Blocks............................256
Total Bytes.............................2097152
Unused Blocks...........................94
Unused Bytes............................770048
Last Used Ext FileId....................6
Last Used Ext BlockId...................1408
Last Used Block.........................34
PL/SQL 过程已成功完成。
ASSM 管理表空间
SQL> create tablespace test_a datafile 'D:\APP\ADMINISTRATOR\ORADATA\ORA11G\test_a.DBF' size 20M extent management local segment space management auto;
表空间已创建。
SQL> create table t_a tablespace test_a as select *From dba_objects;
表已创建。
SQL> create index ind_a on t_a(object_id) tablespace test_a;
索引已创建。
SQL> exec show_space('t_a','T','AUTO');
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................99
Unused Bytes............................811008
Last Used Ext FileId....................5
Last Used Ext BlockId...................1152
Last Used Block.........................29
PL/SQL 过程已成功完成。
SQL> exec show_space('ind_a','I','AUTO');
Total Blocks............................256
Total Bytes.............................2097152
Unused Blocks...........................83
Unused Bytes............................679936
Last Used Ext FileId....................5
Last Used Ext BlockId...................1408
Last Used Block.........................45
PL/SQL 过程已成功完成。
在DBMS_SPACE.SPACE_USAGE收集信息之前,做了一个ANALYZED的判断,因为Analyze 会收集以下信息,而gather_table_stats 则不会收集:
SQL> exec show_space('T_M','T','MANUAL','Y')
Free Blocks.............................0
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................125
Unused Bytes............................1024000
Last Used Ext FileId....................6
Last Used Ext BlockId...................1152
Last Used Block.........................3
BEGIN show_space('T_M','T','MANUAL','Y'); END;
*
第 1 行出现错误:
ORA-10614: Operation not allowed on this segment
ORA-06512: 在 "SYS.DBMS_SPACE", line 190
ORA-06512: 在 "SYS.SHOW_SPACE", line 217
ORA-06512: 在 line 1
DBMS_SPACE.SPACE_USAGE 这个包只适合于assm
SQL> exec show_space('T_A','T','AUTO','Y',NULL)
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................99
Unused Bytes............................811008
Last Used Ext FileId....................5
Last Used Ext BlockId...................1152
Last Used Block.........................29
Thesegment is analyzed
0% -- 25% free spaceblocks..............0
0% -- 25% free spacebytes...............0
25% -- 50% free spaceblocks.............0
25% -- 50% free spacebytes..............0
50% -- 75% free spaceblocks.............0
50% -- 75% free spacebytes..............0
75% -- 100% free spaceblocks............0
75% -- 100% free spacebytes.............0
Unused Blocks...........................0
Unused Bytes............................0
Total Blocks............................1027
Total bytes.............................8413184
PL/SQL 过程已成功完成。
使用 dbms_stats.gather_table_stats
SQL> exec dbms_stats.gather_table_stats(ownname =>'sys',tabname=>'t_a',estimate_percent => 100 ,method_opt =>'for all columns size 1',degree=>2,cascade => true);
PL/SQL 过程已成功完成。
SQL>
SQL> exec show_space('T_A','T','AUTO','Y',NULL)
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................99
Unused Bytes............................811008
Last Used Ext FileId....................5
Last Used Ext BlockId...................1152
Last Used Block.........................29
Thesegment is analyzed
0% -- 25% free spaceblocks..............0
0% -- 25% free spacebytes...............0
25% -- 50% free spaceblocks.............0
25% -- 50% free spacebytes..............0
50% -- 75% free spaceblocks.............0
50% -- 75% free spacebytes..............0
75% -- 100% free spaceblocks............0
75% -- 100% free spacebytes.............0
Unused Blocks...........................0
Unused Bytes............................0
Total Blocks............................1027
Total bytes.............................8413184
PL/SQL 过程已成功完成。
SQL> analyze table t_a compute statistics;
表已分析。
SQL> exec show_space('T_A','T','AUTO','Y',NULL)
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................99
Unused Bytes............................811008
Last Used Ext FileId....................5
Last Used Ext BlockId...................1152
Last Used Block.........................29
Thesegment is analyzed
0% -- 25% free spaceblocks..............0
0% -- 25% free spacebytes...............0
25% -- 50% free spaceblocks.............0
25% -- 50% free spacebytes..............0
50% -- 75% free spaceblocks.............0
50% -- 75% free spacebytes..............0
75% -- 100% free spaceblocks............0
75% -- 100% free spacebytes.............0
Unused Blocks...........................0
Unused Bytes............................0
Total Blocks............................1027
Total bytes.............................8413184
SQL> delete t_a;
已删除72130行。
SQL> commit;
提交完成。
SQL> exec dbms_stats.gather_table_stats(ownname =>'sys',tabname=>'t_a',estimate_percent => 100 ,method_opt =>'for all colu
PL/SQL 过程已成功完成。
SQL> exec show_space('T_A','T','AUTO','Y',NULL)
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................99
Unused Bytes............................811008
Last Used Ext FileId....................5
Last Used Ext BlockId...................1152
Last Used Block.........................29
Thesegment is analyzed
0% -- 25% free spaceblocks..............0
0% -- 25% free spacebytes...............0
25% -- 50% free spaceblocks.............0
25% -- 50% free spacebytes..............0
50% -- 75% free spaceblocks.............0
50% -- 75% free spacebytes..............0
75% -- 100% free spaceblocks............1027
75% -- 100% free spacebytes.............8413184
Unused Blocks...........................0
Unused Bytes............................0
Total Blocks............................0
Total bytes.............................0
PL/SQL 过程已成功完成。
SQL> alter table t_a compute statistics
2
SQL> analyze table t_a compute statistics;
表已分析。
SQL> exec show_space('T_A','T','AUTO','Y',NULL)
Total Blocks............................1152
Total Bytes.............................9437184
Unused Blocks...........................99
Unused Bytes............................811008
Last Used Ext FileId....................5
Last Used Ext BlockId...................1152
Last Used Block.........................29
Thesegment is analyzed
0% -- 25% free spaceblocks..............0
0% -- 25% free spacebytes...............0
25% -- 50% free spaceblocks.............0
25% -- 50% free spacebytes..............0
50% -- 75% free spaceblocks.............0
50% -- 75% free spacebytes..............0
75% -- 100% free spaceblocks............1027
75% -- 100% free spacebytes.............8413184
Unused Blocks...........................0
Unused Bytes............................0
Total Blocks............................0
Total bytes.............................0
PL/SQL 过程已成功完成。
两者值相同,但实际上,Analyze 会收集如下信息:EMPTY_BLOCKS,AVG_SPACE,CHAIN_CNT,而gather_table_stats则不会收集。
参考:
http://blog.csdn.net/tianlesoftware/article/details/8151129