在Oracle 10g 系统中,Oracle推荐使用DBMS_STATS包来收集优化统计信息,但是需要使用analyze命令来收集例如空白块、平均空间等信息。
analyze的又一个重要应用就是用来消除表的行迁移现象(Migrated Rows);可以使用analyze的list chained rows子句来查看表的出现行迁移和行链接的行记录;
样例实验:
数据库版本:
SYS@orcl11g> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
1.首先要在系统中生成一个系统表,用来存放表的行迁移记录信息;
SYS@orcl11g> @?/rdbms/admin/utlchain.sql
SYS@orcl11g> desc chained_rows;
Name Null? Type
----------------------------------------- -------- ----------------------------
OWNER_NAME VARCHAR2(30)
TABLE_NAME VARCHAR2(30)
CLUSTER_NAME VARCHAR2(30)
PARTITION_NAME VARCHAR2(30)
SUBPARTITION_NAME VARCHAR2(30)
HEAD_ROWID ROWID
ANALYZE_TIMESTAMP DATE
2.生成一个样例表,并设法使表中记录有行迁移现象出现;
SYS@orcl11g> create table test (id number,info varchar2(200));
//表中插入数据;
SYS@orcl11g> begin
2 for i in 1..10000 loop
3 insert into test values(i,'a');
4 end loop;
5 end;
6 /
PL/SQL procedure successfully completed.
//查看表占用的数据块,区的状态
SYS@orcl11g> select owner,segment_name,blocks,extents from dba_segments where segment_name='TEST';
OWNER SEGMENT_NAME BLOCKS EXTENTS
------------------------------ -------------------- ---------- ----------
SYS TEST 24 3
SYS@orcl11g> begin
2 for i in 1..100 loop
3 update test set info='abcdefghijklmnopqrstuvwxyz' where id=i;
4 end loop;
5 end;
6 /
PL/SQL procedure successfully completed.
3.使用analyze命令收集信息
SYS@orcl11g> analyze table test list chained rows;
4.查询chained_rows表
SYS@orcl11g> select count(*) from chained_rows;
COUNT(*)
----------
66
//说明有66行出现了行迁移的现象;
5.生成一个中间表,来保存产生行迁移的行记录;
SYS@orcl11g> create table test_tmp
2 as select * from test
3 where rowid in (
4 select head_rowid from chained_rows where table_name='TEST');
Table created.
6.删除原表中那些产生行迁移的行记录;
SYS@orcl11g> delete from test
2 where rowid in (
3 select head_rowid from chained_rows where table_name='TEST');
66 rows deleted.
7.将中间表中的数据重新插入到原表;
SYS@orcl11g> insert into test select * from test_tmp;
66 rows created.
8.删除中间表,和chained_rows表中的记录
SYS@orcl11g> drop table test_tmp purge;
Table dropped.
SYS@orcl11g> truncate table chained_rows;
Table truncated.
9.验证:
SYS@orcl11g> analyze table test list chained rows;
Table analyzed.
SYS@orcl11g> select count(*) from chained_rows;
COUNT(*)
----------
0
说明所有产生行迁移的行记录,已经没有了行迁移现象;
阅读(1029) | 评论(0) | 转发(0) |