Oracle对表做全表扫描的时候 ,会扫描完HWM以下的数据块。如果某个表delete(delete操作不会降低高水位)了大量数据,那么这时对表做全表扫描就会做很多无用功,扫描了一大堆数据块,最后发现块里面居然没有数据。
通常,在对表做了大批量delete操作之后,就应该马上降低表的高水位,可以使用shrink 命令或者alter table table_name move降低表的高水位。在降低表的高水位之后,表上面的索引会失效,因为表的rowid更改了,这个时候需要rebuild索引。
如何求出段的高水位?其实很简单,首先对表收集统计信息,然后查询DBA_TABLES的blocks,以及empty_blocks字段,blocks表示已经用了多少个blocks,empty_blocks表示从来没有使用过的blocks。那么blocks就表示段的高水位。
可以使用下面的语句查看表到底用了多少个blocks
select count( distinct dbms_rowid.rowid_block_number(rowid)) from table_name;
然后再对比dba_tables表中的blocks列,如果求出的blocks数与dba_tables相差在10左右,那么表示这个表不需要shrink,用上面的脚本求出的blocks数没计算段头,位图管理块。如果相差很大,那么表示这个表需要shrink了,不过这样做比较麻烦,不是吗?
其实还可以监控表的DML操作,根据监控的结果,我们就可以判断哪些表需要降低高水位。在Oracle10g中DBA_TAB_MODIFICATIONS这个视图记录了自上次收集统计信息以来表的DML操作信息。当我们再次对表收集统计信息,该视图的记录就会被清空。
对于非分区表,可以使用下面脚本初步检测哪些表需要降低高水位
exec DBMS_STATS.FLUSH_DATABASE_MONITORING_INFO ;
col table format a35
select a.owner || '.' || a.table_name "Table",a.num_rows,a.avg_row_len,b.inserts,b.deletes,a.num_rows+nvl(b.inserts,0)-nvl(b.deletes,0) total_rows,
round(a.avg_row_len*(a.num_rows+nvl(b.inserts,0)-nvl(b.deletes,0))/1024/1024,4) "Calculate_Sizle(Mb)",c.bytes/1024/1024 "Segment_Size(Mb)"
from dba_tables a left join all_tab_modifications b
on a.owner=b.table_owner and a.table_name=b.table_name inner join dba_segments c on a.owner=c.owner and a.table_name=c.segment_name
where a.last_analyzed is not null and a.partitioned='NO' and b.deletes>100 and a.owner not like ‘%SYS%' and c.bytes/1024/1024>100
and (a.avg_row_len*(a.num_rows+nvl(b.inserts,0)-nvl(b.deletes,0)))/c.bytes0,段大小超过200M,比值小于0.3等等。利用上面脚本查询出可疑的表之后,可以最开始讲的方法检查是否要shrink表,你也可以用Segment Advisor来检查是否需要shrink这个表。Segment Advisor使用方法:
SQL> variable task_id number;
SQL> begin
2 declare
3 object_id number;
4 name varchar2(100);
5 task_desc varchar2(100);
6 begin
7 name := 'test';
8 task_desc := 'Segment Advisor TEST';
9 dbms_advisor.create_task(advisor_name => 'Segment Advisor',
10 task_id => :task_id,
11 task_name => name,
12 task_desc => task_desc
13 );
14 dbms_advisor.create_object(task_name => name,
15 object_type => 'TABLE',
16 attr1 => 'ROBINSON',
17 attr2 => 'TEST',
18 attr3 => NULL,
19 attr4 => NULL,
20 attr5 => NULL,
21 object_id =>object_id
22 );
23 dbms_advisor.set_task_parameter(task_name => name,
24 parameter => 'recommend_all',
25 value => 'TRUE'
26 );
27 dbms_advisor.execute_task(task_name => name);
28 end;
29 end;
30 /
PL/SQL procedure successfully completed
task_id
---------
560
SQL> col task_name format a8
SQL> col segname format a8
SQL> col partition format a8
SQL> col type format a8
SQL> col message format a100
SQL> select af.task_name, ao.attr2 segname, ao.attr3 partition, ao.type, af.message
2 from dba_advisor_findings af, dba_advisor_objects ao
3 where ao.task_id = af.task_id
4 and ao.object_id = af.object_id
5 and ao.owner = 'ROBINSON';
TASK_NAM SEGNAME PARTITIO TYPE MESSAGE
-------- -------- -------- -------- ----------------------------------------------------------------------------------------------------
test TEST TABLE Enable row movement of the table ROBINSON.TEST and perform shrink, estimated savings is 28451785 bytes.
下面是一个生产环境数据库,可以看到有很多表都需要shrink,我就不贴出具体的处理步骤了。
SQL> select a.owner || '.' || a.table_name "Table",a.num_rows,a.avg_row_len,b.inserts,b.deletes,a.num_rows+nvl(b.inserts,0)-nvl(b.deletes,0) total_rows,
2 round(a.avg_row_len*(a.num_rows+nvl(b.inserts,0)-nvl(b.deletes,0))/1024/1024,4) "Calculate_Sizle(Mb)",c.bytes/1024/1024 "Segment_Size(Mb)"
3 from dba_tables a left join all_tab_modifications b
4 on a.owner=b.table_owner and a.table_name=b.table_name inner join dba_segments c on a.owner=c.owner and a.table_name=c.segment_name
5 where a.last_analyzed is not null and a.partitioned='NO' and b.deletes>100 and c.bytes/1024/1024>100 and a.owner not like '%SYS%'
6 and (a.avg_row_len*(a.num_rows+nvl(b.inserts,0)-nvl(b.deletes,0)))/c.bytes