四, Show_space 统计SEGMENT的空间使用具体情况。

来源:互联网 发布:认知 知乎 编辑:程序博客网 时间:2024/05/02 00:16

ORACLE深入 第一章ORACLE DBA常用语句和脚本(2)

空间管理 绑定变量 读一致性 flashback

日月明王BLOGhttp://sunmoonking.spaces.live.com

, Show_space 统计SEGMENT的空间使用具体情况。
create or replace procedure show_space
( p_segname in varchar2,                    --SGEMENTNAME
p_owner in varchar2 default user,                      --默任是当前用户
p_type in varchar2 default 'TABLE',           --SEGMENT TYPE
p_partition in varchar2 default NULL )         --PARTITIONNAME
-- this procedure uses authid current user so it can query DBA_*
-- views using privileges from a ROLE, and so it can be installed
-- once per database, instead of once per user who wanted to use it
authid current_user
as
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;
l_segment_space_mgmt varchar2(255);
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;
-- inline procedure to print out numbers nicely formatted
-- with a simple label
procedure p( p_label in varchar2, p_num in number )
is
begin
dbms_output.put_line( rpad(p_label,40,'.') ||
to_char(p_num,'999,999,999,999') );
end;
begin
-- this query is executed dynamically in order to allow this procedure
-- to be created by a user who has access to DBA_SEGMENTS/TABLESPACES
-- via a role as is customary.
-- NOTE: at runtime, the invoker MUST have access to these two
-- views!
-- this query determines if the object is an ASSM object or not
begin
execute immediate
'select ts.segment_space_management
from dba_segments seg, dba_tablespaces ts
where seg.segment_name = :p_segname
and (:p_partition is null or
seg.partition_name = :p_partition)
and seg.owner = :p_owner
and seg.tablespace_name = ts.tablespace_name'
into l_segment_space_mgmt
using p_segname, p_partition, p_partition, p_owner;
exception
when too_many_rows then
dbms_output.put_line
( 'This must be a partitioned table, use p_partition => ');
return;
end;
-- if the object is in an ASSM tablespace, we must use this API
-- call to get space information, otherwise we use the FREE_BLOCKS
-- API for the user-managed segments
if l_segment_space_mgmt = 'AUTO'
then
dbms_space.space_usage
( p_owner, p_segname, p_type, l_unformatted_blocks,
l_unformatted_bytes, l_fs1_blocks, l_fs1_bytes,
l_fs2_blocks, l_fs2_bytes, l_fs3_blocks, l_fs3_bytes,
l_fs4_blocks, l_fs4_bytes, l_full_blocks, l_full_bytes, p_partition);
p( 'Unformatted Blocks ', l_unformatted_blocks );
p( 'FS1 Blocks (0-25) ', l_fs1_blocks );
p( 'FS2 Blocks (25-50) ', l_fs2_blocks );
p( 'FS3 Blocks (50-75) ', l_fs3_blocks );
p( 'FS4 Blocks (75-100)', l_fs4_blocks );
p( 'Full Blocks ', l_full_blocks );
else
dbms_space.free_blocks(
segment_owner => p_owner,
segment_name => p_segname,
segment_type => p_type,
freelist_group_id => 0,
free_blks => l_free_blks);
p( 'Free Blocks', l_free_blks );
end if;
-- and then the unused space API call to get the rest of the
-- information
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 );
p( 'Total Blocks', l_total_blocks );
p( 'Total Bytes', l_total_bytes );
p( 'Total MBytes', trunc(l_total_bytes/1024/1024) );
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 );
end;
 
SQL> desc show_space
PROCEDURE show_space
参数名称                       类型                    输入/输出默认值?
------------------------------ ----------------------- ------ --------
 P_SEGNAME                      VARCHAR2                IN
 P_OWNER                        VARCHAR2                IN     DEFAULT
 P_TYPE                         VARCHAR2                IN     DEFAULT
 P_PARTITION                    VARCHAR2                IN     DEFAULT
ASSMuser space managed tablespaces得到的结果不同,以下用user space managed tablespaces来说明
SQL> exec show_spacE('WWM');
Free Blocks.............................               2
Total Blocks............................           5,120
Total Bytes.............................      41,943,040
Total MBytes............................              40
Unused Blocks...........................              13
Unused Bytes............................         106,496
Last Used Ext FileId....................               1
Last Used Ext BlockId...................          46,473
Last Used Block.........................             115
FULL BLOCKS: full表示在没有变化之前不会再被分配出去。
Total Blocks, Total Bytes, Total MBytes: 整个分配给此SEGMENT的空间。
Unused Blocks, Unused Bytes:分配给SEGMENT,但是还没有使用,也就是说在HWM之上的空闲块。
Last Used Ext FileId:  最后一个extent所在的FILE ID.
Last Used Ext BlockId: 最后一个EXTENT BEGIN BLOCK ID.
FS1 BlocksFS4 Blocks: 例如,0-25,表示在0-25%之间的空块.
做一个大表,以供测试使用
create table big_table
as
select rownum id, a.*
from all_objects a
where 1=0
/
alter table big_table nologging;
insert /*+ append */
into big_table
select rownum, a.*
from all_objects a;
已创建29645行。
  1  insert /*+ append */
  2  into big_table
  3  select rownum+29646, a.*
  4* from all_objects a
SQL> /
已创建29645行。
alter table big_table add constraint
big_table_pk primary key(id)
  1  begin
  2  dbms_stats.gather_table_stats
  3  ( ownname => 'SYSTEM',
  4  tabname => 'BIG_TABLE',
  5  method_opt => 'for all indexed columns',
  6  cascade => TRUE );
  7* end;
, 绑定变量与非绑定变量
阅读本章可以参考我的  BIND VARIABLE
http://sunmoonking.spaces.live.com/blog/cns!E3BD9CBED01777CA!175.entry
测试表
create table t ( x int );
绑定变量
create or replace procedure proc1
 as
begin
for i in 1 .. 10000
loop
execute immediate
'insert into t values ( :x )' using i;
end loop;
 end;
/
非绑定变量
create or replace procedure proc2
 as
 begin
 for i in 1 .. 10000
 loop
 execute immediate
 'insert into t values ( '||i||')';
 end loop;
 end;
/
SQL> exec runstats_pkg.rs_start;
PL/SQL 过程已成功完成。
SQL> exec proc1
PL/SQL 过程已成功完成。
SQL> exec runstats_pkg.rs_middle;
PL/SQL 过程已成功完成。
SQL> exec proc2
PL/SQL 过程已成功完成。
SQL> exec runstats_pkg.rs_stop(1000);    --差别大于1000的才输出
Run1 ran in 1622 hsecs
Run2 ran in 4378 hsecs
run 1 ran in 37.05% of the time
 
Name                                Run1      Run2      Diff
STAT...bytes received via SQL*       387     1,690     1,303
STAT...bytes sent via SQL*Net        276     1,798     1,522
STAT...parse count (hard)              5    10,009    10,004
…………………………………………………………………………….
Run1 latches total versus runs -- difference and pct
Run1      Run2      Diff     Pct
313,680 1,625,995 1,312,315  19.29%
PL/SQL 过程已成功完成。
可以看到PRO2基本上是PRO1的三倍,也就是说解析时间占完整运行完一条语句所耗时间的2/3,会多占用LATCH.可见,尽量减少硬解析能明显提高性能.
,read-consistent
 
SQL> create table wwm as select * from all_users;
表已创建。
SQL> variable x refcursor
SQL> begin
  2  open :x for select * from wwm;
  3  end;
  4  /
PL/SQL 过程已成功完成。
SQL> delete from wwm;
已删除27行。
SQL> commit;
提交完成。
SQL> print x
USERNAME                                                        USER_ID
------------------------------------------------------------ ----------
CREATED
--------------
BI                                                                   60
07-6 -06
。。。。。。。。。。。。。。。
注意,这是在一个SESSION里做的实验。现建立一个表WWM,再OPEN一个CURSOR,再DELETEWWM,甚至COMMIT之后还是可以PRINT CURSOR的原来的内容。
但是,需要明白的是,在OPEN一个CURSOR的时候并不取得数据或把数据放到某个地方;而是在DELETE的时候才将你需要的数据给你保留到一个地方(UNDO SEGMENT或叫ROLLBACK SEGMENT)。
 
, flashback query.
SCN ORACLE内部时钟,
SQL> variable SCN number
SQL> exec :scn:=dbms_flashback.get_system_change_number
PL/SQL 过程已成功完成。
SQL> print scn
       SCN
----------
   1170636
SQL> select count(*) from wwm;
  COUNT(*)
----------
         0
SQL> desc wwm
 名称                                      是否为空? 类型
 ----------------------------------------- -------- ----------------------------
 USERNAME                                  NOT NULL VARCHAR2(30)
 USER_ID                                   NOT NULL NUMBER
 CREATED                                   NOT NULL DATE
SQL> insert into wwm select * from all_users;
已创建27行。
SQL> select count(*) from wwm;
  COUNT(*)
----------
        27
SQL> select count(*) from wwm as of scn :scn;
  COUNT(*)
----------
         0
SQL> commit;
提交完成。
SQL>  select count(*) from wwm as of scn :scn;
  COUNT(*)
----------
         0
甚至,我们可以用一个SQL得到两个SCN点的值
SQL> select * from (select count(*) from wwm),
  2   (select count(*) from wwm as of scn :scn);
 
  COUNT(*)   COUNT(*)
---------- ----------
27                0
 
原创粉丝点击