create table test (id int); SQL> begin  ..."/>
您好,登錄后才能下訂單哦!
1,創(chuàng)建一個測試表,test,并且插入10000行數(shù)據(jù);
SQL> create table test (id int);
SQL> begin
2 for i in 1..10000 loop
3 insert into test values(i)
4 end loop;
5 end;
6 /
SQL> commit;
2,創(chuàng)建一個存儲過程SHOW_SPACE:
|
|
create or replace procedure show_space
( p_segname in varchar2,
p_owner in varchar2 default user,
p_type in varchar2 default 'TABLE',
p_partition in varchar2 default NULL )
as
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
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( '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;
/
3,檢查表test的空間使用情況:
SQL> exec show_space('TEST');
Total Blocks............................24
Total Bytes.............................196608
Unused Blocks...........................3
Unused Bytes............................24576
Last Used Ext FileId....................1
Last Used Ext BlockId...................62177
Last Used Block.........................5
由上可知,該表test共占用了24個數(shù)據(jù)塊,196608字節(jié),文件ID為1
4, 獲得表test在數(shù)據(jù)塊中的分布情況:
SQL> select f,b from (
2 select dbms_rowid.rowid_relative_fno(rowid) f,
3 dbms_rowid.rowid_block_number(rowid) b
4 from test) group by f,b order by b;
F B
---------- ----------
1 62162
1 62163
1 62164
1 62165
1 62166
1 62167
1 62168
1 62169
1 62170
1 62171
1 62172
1 62173
1 62174
1 62175
1 62176
1 62177
16 rows selected.
由此可見,表test中的數(shù)據(jù)共占用了16個數(shù)據(jù)塊,但是前面第三步中,發(fā)現(xiàn)該表占用了24個數(shù)據(jù)塊。這是正常的,因為oracle本身會使用8個數(shù)據(jù)塊來記錄段頭、位圖塊等額外的信息。我們現(xiàn)在只需要了解到,表test共占用了24個數(shù)據(jù)塊,其中16個是數(shù)據(jù),8個是表信息。
5,檢查x$bh和v$bh的更新:
SQL> select file#,dbablk,tch from x$bh where bj=
2 (select data_object_id from dba_objects
3 where wner='SYS' and object_name='TEST')
4 order by dbablk;
FILE# DBABLK TCH
---------- ---------- ----------
1 62161 6
1 62162 3
1 62163 3
1 62164 3
1 62165 3
1 62166 3
1 62167 3
1 62168 3
1 62169 3
1 62170 3
1 62171 3
1 62172 3
1 62173 3
1 62174 3
1 62175 3
1 62176 3
1 62177 3
1 62178 3
1 62179 3
1 62180 3
1 62181 3
21 rows selected.
SQL> select file#,block#,status from v$bh where bjd=
2 (select data_object_id from dba_objects
3 where wner='SYS' and object_name='TEST')
4 order by block#;
FILE# BLOCK# STATUS
---------- ---------- -------
1 62161 xcur
1 62162 xcur
1 62163 xcur
1 62164 xcur
1 62165 xcur
1 62166 xcur
1 62167 xcur
1 62168 xcur
1 62169 xcur
1 62170 xcur
1 62171 xcur
1 62172 xcur
1 62173 xcur
1 62174 xcur
1 62175 xcur
1 62176 xcur
1 62177 xcur
1 62178 xcur
1 62179 xcur
1 62180 xcur
1 62181 xcur
21 rows selected.
這里可以看到,在v$bh和x$bh中得到的數(shù)據(jù)塊,是從62161~62181的21條記錄,但是在第四步中,我們知道數(shù)據(jù)是占用了62162~62177的16個數(shù)據(jù)庫,這里,62161數(shù)據(jù)塊里面存放的是段頭信息,可以通過如下命令進(jìn)行驗證:
SQL> select header_file,header_block from dba_segments
2 where wner='SYS' and segment_name='TEST';
HEADER_FILE HEADER_BLOCK
----------- ------------
1 62161
在v$bh視圖中,我們可以看到這21個數(shù)據(jù)塊都是xcur狀態(tài),表示這些數(shù)據(jù)塊都是排斥狀態(tài),正在被使用,該字段還有其他的類型,請參見數(shù)據(jù)塊的狀態(tài)類型。
(
oracle緩沖塊(data block)狀態(tài)類型
|
)
6,清空數(shù)據(jù)緩存:
SQL> alter system flush buffer_cache;
(在Oracle9i里,Oracle提供了一個內(nèi)部事件,用以強制刷新Buffer Cache,其語法為:
alter session set events 'immediate trace name flush_cache level 1';
或者:
alter session set events = 'immediate trace name flush_cache';
類似的也可以使用alter system系統(tǒng)級設(shè)置:
alter system set events = 'immediate trace name flush_cache';
在Oracle10g中,Oracle提供一個新的特性,可以通過如下命令刷新Buffer Cache:
alter system flush buffer_cache;
)
7,重新檢查v$bh和x$bh的內(nèi)容:
SQL> select file#,dbablk,tch from x$bh where bj=
2 (select data_object_id from dba_objects
3 where wner='SYS' and object_name='TEST')
4 order by dbablk;
FILE# DBABLK TCH
---------- ---------- ----------
1 62161 0
1 62162 0
1 62163 0
1 62164 0
1 62165 0
1 62166 0
1 62167 0
1 62168 0
1 62169 0
1 62170 0
1 62171 0
1 62172 0
1 62173 0
1 62174 0
1 62175 0
1 62176 0
1 62177 0
1 62178 0
1 62179 0
1 62180 0
1 62181 0
21 rows selected.
SQL> select file#,block#,status from v$bh where bjd=
2 (select data_object_id from dba_objects
3 where wner='SYS' and object_name='TEST')
4 order by block#;
FILE# BLOCK# STATUS
---------- ---------- -------
1 62161 free
1 62162 free
1 62163 free
1 62164 free
1 62165 free
1 62166 free
1 62167 free
1 62168 free
1 62169 free
1 62170 free
1 62171 free
1 62172 free
1 62173 free
1 62174 free
1 62175 free
1 62176 free
1 62177 free
1 62178 free
1 62179 free
1 62180 free
1 62181 free
21 rows selected.
這時候我們可以看到,x$bh中的tch字段,已經(jīng)由原來的3變成了0,同時v$bh視圖的數(shù)據(jù)塊狀態(tài)也變成了free,但是記錄的數(shù)據(jù)塊并沒有發(fā)生變化,還是在62161~62181這些數(shù)據(jù)塊中,這就是說,雖然數(shù)據(jù)已經(jīng)被寫到了磁盤中,但是數(shù)據(jù)庫記錄的指針并沒有清空,僅僅是其狀態(tài)發(fā)生了改變。<SPAN
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。