Oracle脚本检查对象空间使用情况

TOM有个非常好的查看对象空间使用情况的脚本show_space.sql,下面将介绍。


这里介绍的是相对而言比较简单的,下面演示中其实是针对表的空间信息,至于对象的,我将在下面改写。


下面例子演示的是delete和truncate对高水位的影响,其中用到了脚本space_usage.sql。


[oracle@maa3 ~]$ cat space_usage.sql

-- space_usage.sql


declare

       l_tabname varchar2(30) := '&1';

       l_fs1_bytes number;

       l_fs2_bytes number;

       l_fs3_bytes number;

       l_fs4_bytes number;

       l_fs1_blocks number;

       l_fs2_blocks number;

       l_fs3_blocks number;

       l_fs4_blocks number;

       l_full_bytes number;

       l_full_blocks number;

       l_unformatted_bytes number;

       l_unformatted_blocks number;

begin

       dbms_space.space_usage(

                       segment_owner => user,

                       segment_name => l_tabname,

                       segment_type => 'TABLE',

                       fs1_bytes => l_fs1_bytes,

                       fs1_blocks => l_fs1_blocks,

                       fs2_bytes => l_fs2_bytes,

                       fs2_blocks => l_fs2_blocks,

                       fs3_bytes => l_fs3_bytes,

                       fs3_blocks => l_fs3_blocks,

                       fs4_bytes => l_fs4_bytes,

                       fs4_blocks => l_fs4_blocks,

                       full_bytes => l_full_bytes,

                       full_blocks => l_full_blocks,

                       unformatted_blocks => l_unformatted_blocks,

                       unformatted_bytes => l_unformatted_bytes

                       );

       dbms_output.put_line('0-25% Free = '||l_fs1_blocks||' Bytes = '||l_fs1_bytes);

       dbms_output.put_line('25-50% Free = '||l_fs2_blocks||' Bytes = '||l_fs2_bytes);

       dbms_output.put_line('50-75% Free = '||l_fs3_blocks||' Bytes = '||l_fs3_bytes);

       dbms_output.put_line('75-100% Free = '||l_fs4_blocks||' Bytes = '||l_fs4_bytes);

       dbms_output.put_line('Full Blocks = '||l_full_blocks||' Bytes = '||l_full_bytes);

end;

/


查看给T1表分配了多少数据块儿

luocs@MAA> select blocks from user_segments where segment_name='T1';


# Blocks

---------------

           384


列出包含数据的数据块儿数

luocs@MAA> select count(distinct(dbms_rowid.rowid_block_number(rowid))) block_count from t1;


BLOCK_COUNT

---------------

           306


查询lowest block number和highest block number

luocs@MAA> select min(dbms_rowid.rowid_block_number(rowid)) min_block, max(dbms_rowid.rowid_block_number(rowid)) max_block from t1;


MIN_BLOCK       MAX_BLOCK

--------------- ---------------

           131             447


luocs@MAA> @space_usage T1

0-25% Free = 0 Bytes = 0

25-50% Free = 0 Bytes = 0

50-75% Free = 0 Bytes = 0

75-100% Free = 0 Bytes = 0

Full Blocks = 306 Bytes = 2506752


大家在使用过程中,可能会遇到ORA-00942: table or view does not exist、ORA-06512: at "SYS.DBMS_SPACE", line 214的错误,这是因为大家表名使用了小写,比如:

luocs@MAA> @space_usage t1

declare

*

ERROR at line 1:

ORA-00942: table or view does not exist

ORA-06512: at "SYS.DBMS_SPACE", line 214

ORA-06512: at line 16


为更加方便,我稍微修改了下脚本,此脚本可在dba权限用户下使用,需要提供模式和表名


sys@MAA> get space

 1  declare

 2          l_user varchar2(30) := upper('&1');

 3     l_tabname varchar2(30) := upper('&2');

 4     l_fs1_bytes number;

 5     l_fs2_bytes number;

 6     l_fs3_bytes number;

 7     l_fs4_bytes number;

 8     l_fs1_blocks number;

 9     l_fs2_blocks number;

10     l_fs3_blocks number;

11     l_fs4_blocks number;

12     l_full_bytes number;

13     l_full_blocks number;

14     l_unformatted_bytes number;

15     l_unformatted_blocks number;

16  begin

17     dbms_space.space_usage(

18                     segment_owner => l_user,

19                     segment_name => l_tabname,

20                     segment_type => 'TABLE',

21                     fs1_bytes => l_fs1_bytes,

22                     fs1_blocks => l_fs1_blocks,

23                     fs2_bytes => l_fs2_bytes,

24                     fs2_blocks => l_fs2_blocks,

25                     fs3_bytes => l_fs3_bytes,

26                     fs3_blocks => l_fs3_blocks,

27                     fs4_bytes => l_fs4_bytes,

28                     fs4_blocks => l_fs4_blocks,

29                     full_bytes => l_full_bytes,

30                     full_blocks => l_full_blocks,

31                     unformatted_blocks => l_unformatted_blocks,

32                     unformatted_bytes => l_unformatted_bytes

33                     );

34     dbms_output.put_line('0-25% Free = '||l_fs1_blocks||' Bytes = '||l_fs1_bytes);

35     dbms_output.put_line('25-50% Free = '||l_fs2_blocks||' Bytes = '||l_fs2_bytes);

36     dbms_output.put_line('50-75% Free = '||l_fs3_blocks||' Bytes = '||l_fs3_bytes);

37     dbms_output.put_line('75-100% Free = '||l_fs4_blocks||' Bytes = '||l_fs4_bytes);

38     dbms_output.put_line('Full Blocks = '||l_full_blocks||' Bytes = '||l_full_bytes);

39* end;


sys@MAA> @space luocs t1

old   2:         l_user varchar2(30) := upper('&1');

new   2:         l_user varchar2(30) := upper('luocs');

old   3:        l_tabname varchar2(30) := upper('&2');

new   3:        l_tabname varchar2(30) := upper('t1');

0-25% Free = 0 Bytes = 0

25-50% Free = 0 Bytes = 0

50-75% Free = 0 Bytes = 0

75-100% Free = 0 Bytes = 0

Full Blocks = 306 Bytes = 2506752


PL/SQL procedure successfully completed.


先用delete删除t1表所有数据

sys@MAA> delete from luocs.t1;


22671 rows deleted.


sys@MAA> commit;


Commit complete.


sys@MAA> select blocks from dba_segments where owner='LUOCS' and segment_name='T1';


BLOCKS

----------

      384


sys@MAA> select count(distinct(dbms_rowid.rowid_block_number(rowid))) block_count from luocs.t1;


BLOCK_COUNT

-----------

         0


sys@MAA> @space luocs t1

old   2:         l_user varchar2(30) := upper('&1');

new   2:         l_user varchar2(30) := upper('luocs');

old   3:        l_tabname varchar2(30) := upper('&2');

new   3:        l_tabname varchar2(30) := upper('t1');

0-25% Free = 0 Bytes = 0

25-50% Free = 0 Bytes = 0

50-75% Free = 0 Bytes = 0

75-100% Free = 306 Bytes = 2506752

Full Blocks = 0 Bytes = 0


PL/SQL procedure successfully completed.


但因为DELETE并不降低HWM,所以做FTS 扫描的I/O块儿依然多

sys@MAA> set autot traceonly exp stat

sys@MAA> select  * from luocs.t1;


no rows selected


Execution Plan

----------------------------------------------------------

Plan hash value: 3617692013


--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      | 22671 |  2036K|    88   (0)| 00:00:02 |

|   1 |  TABLE ACCESS FULL| T1   | 22671 |  2036K|    88   (0)| 00:00:02 |

--------------------------------------------------------------------------


Statistics

----------------------------------------------------------

         0  recursive calls

         0  db block gets

       309  consistent gets

         0  physical reads

         0  redo size

      1343  bytes sent via SQL*Net to client

       512  bytes received via SQL*Net from client

         1  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         0  rows processed


sys@MAA> set autot off


然后truncate这张表

sys@MAA> truncate table luocs.t1;


Table truncated.


sys@MAA> @space luocs t1

old   2:         l_user varchar2(30) := upper('&1');

new   2:         l_user varchar2(30) := upper('luocs');

old   3:        l_tabname varchar2(30) := upper('&2');

new   3:        l_tabname varchar2(30) := upper('t1');

0-25% Free = 0 Bytes = 0

25-50% Free = 0 Bytes = 0

50-75% Free = 0 Bytes = 0

75-100% Free = 0 Bytes = 0

Full Blocks = 0 Bytes = 0


PL/SQL procedure successfully completed.


sys@MAA> select blocks from dba_segments where owner='LUOCS' and segment_name='T1';


BLOCKS

----------

        8


再进行FTS,逻辑读降低

sys@MAA> set autot traceonly exp stat

sys@MAA> select  * from luocs.t1;


no rows selected


Execution Plan

----------------------------------------------------------

Plan hash value: 3617692013


--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      | 22671 |  2036K|    88   (0)| 00:00:02 |

|   1 |  TABLE ACCESS FULL| T1   | 22671 |  2036K|    88   (0)| 00:00:02 |

--------------------------------------------------------------------------


Statistics

----------------------------------------------------------

         1  recursive calls

         0  db block gets

         1  consistent gets

         0  physical reads

         0  redo size

      1343  bytes sent via SQL*Net to client

       512  bytes received via SQL*Net from client

         1  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         0  rows processed


至于ROWS依然显示22671是因为没有进行分析

sys@MAA> exec dbms_stats.gather_table_stats('LUOCS','T1',cascade=>true);


PL/SQL procedure successfully completed.


sys@MAA> select  * from luocs.t1;


no rows selected


Execution Plan

----------------------------------------------------------

Plan hash value: 3617692013


--------------------------------------------------------------------------

| Id  | Operation         | Name | Rows  | Bytes | Cost (%CPU)| Time     |

--------------------------------------------------------------------------

|   0 | SELECT STATEMENT  |      |     1 |   207 |     2   (0)| 00:00:01 |

|   1 |  TABLE ACCESS FULL| T1   |     1 |   207 |     2   (0)| 00:00:01 |

--------------------------------------------------------------------------


Statistics

----------------------------------------------------------

         0  recursive calls

         0  db block gets

         1  consistent gets

         0  physical reads

         0  redo size

      1343  bytes sent via SQL*Net to client

       512  bytes received via SQL*Net from client

         1  SQL*Net roundtrips to/from client

         0  sorts (memory)

         0  sorts (disk)

         0  rows processed


sys@MAA> select count(*) from luocs.t1;


COUNT(*)

----------

    45594


sys@MAA> exec dbms_stats.gather_table_stats('LUOCS','T1',cascade=>true);


PL/SQL procedure successfully completed.


sys@MAA> exec dbms_stats.gather_index_stats('LUOCS','INX_ID');


PL/SQL procedure successfully completed.


oracle视频教程请关注:http://u.youku.com/user_video/id_UMzAzMjkxMjE2.html


你可能感兴趣的:(oracle,Oracle对象空间,Oracle脚本)