技术开发 频道

Oracle性能优化之提升block的效率

  6、Migration和Chaining

  1)如果一行的数据太大以至一个单独的block容纳不下,会产生两种现象:

  A、Chaining:行数据太大以至一个空block容纳不下,oracle会将这一行的数据存放在一个或多个block 组成的block chain中,insert、update都可能导致这个问题,在某些情况下row chaining是不能避免的。

  B、Migration:一次update操作可能导致行数据增大,以至它所在的block容纳不下,oracle server会去寻找一个有足够自由空间容纳整行数据的block,如果这样的block存在,oracle server把整行移到新的block,在原位置保存一个指向新存放位置的镜像行,镜像行的rowid和原来的rowid一致。

  Chaining、Migration的弊端:insert、update的性能降低,索引查询增加了IO次数。

  2)检测migration和chaining:

        Analyize table table_name compute statistics;

  Select num_rows,chain_cnt from dba_tables where table_name=’...’;

  查询镜像行:

  Analyize table table_name list chained rows;

  Select owner_name,table_name,head_rowid from chained_rows where table_name=’...’;

  产生Migration的原因可能是由于PCTFREE设置的太低以至没有保留足够的空间用于更新。

  可以通过增加PCTFREE的值避免行镜像产生。

  3)消除镜像行的步骤:

  运行analyize table ... list chained rows;

  复制镜像行到另一个表tmp;

  从源表中删除这些行;

  从tmp中将这些行插回到源表中。

  脚本:

  /* Get the name of the table with migrated rows */

  accept table_name prompt ’Enter the name of the table with migrated rows: ’

  /* Clean up from last execution */

  set echo off

  drop table migrated_rows;

  drop table chained_rows;

  /* Create the CHAINED_ROWS table */

  @?/rdbms/admin/utlchain

  set echo on

  spool fix_mig

  /* List the chained & migrated rows */

  analyze table &table_name list chained rows;

  /* Copy the chained/migrated rows to another table */

  create table migrated_rows as

  select orig.* from &table_name orig, chained_rows cr

  where orig.rowid = cr.head_rowid

  and cr.table_name = upper(’&table_name’);

  /* Delete the chained/migrated rows from the original table */

  delete from &table_name

  where rowid in ( select head_rowid from chained_rows );

  /* Copy the chained/migrated rows back into the original table */

  insert into &table_name select * from migrated_rows;

  spool off

  使用这个脚本时,必须将涉及到的外键约束去掉。

  7、索引重组

  在一个不稳定的表上建索引会影响性能,一个索引block只有完全空时才能进入free list,即使一个索引block里只含有一个条目,它也必须被维护,因此索引需要进行阶段性的重建。

  1)检查索引是否需要重组

  A、收集一个index的使用统计

  ANALYZE INDEX acct_no_idx VALIDATE STRUCTURE;

  B、查看收集的统计数据

  SELECT NAME,(DEL_LF_ROWS_LEN/LF_ROWS_LEN) * 100 AS index_usage FROM index_stats;
 

  C、如果浪费超过20%则索引需要重建

  ALTER INDEX acct_no_idx REBUILD;

  D、或者对索引进行整理

  Alter index acct_no_idx coalesce;

  2)标记未使用的索引

  A、 开始监测索引的使用

  Alter index hr.emp_name_ix monitoring usage;

  B、 停止监测索引的使用

  Alter index hr.emp_name_ix nomonitoring usage;

  C、 查询索引的使用情况

  Select index_name,used from v$object_usage;

  删除未使用过的索引,可以降低DML操作的成本,从而提升系统性能。

  为了尽可能经济的利用block,应对存在较多空block、镜像行的表进行重建,对建立不稳定表上的索引应有规律的进行重建,并尽可能创建本地管理的表空间。

 

0
相关文章