********************************************************************************
  5. 碎片整理方法
  ********************************************************************************
  ------------------------------------------------*
  5.1表空间碎片整理
  ------------------------------------------------*
  alter tablespace users coalesce;
  ------------------------------------------------*
  5.2表碎片整理
  ------------------------------------------------*
  ---方法1:exo/imp或data pump数据泵技术
  ---方法2:CTAS
  create table newtable as select * from oldtable;
  drop table oldtable;
  rename table newtable to oldtable;
  ----方法3:move tablespace技术
  alter table <table_name> move tablespace <newtablespace_name>;
  ----方法4:shrink
  alter table <table_name> enable row movement; 
  alter table <table_name> shrink space cascade;  --压缩表以及相关数据段并下调HWM
  alter table <table_name> shrink space compact;  --只压缩数据不下调HWM,不影响DML操作
  alter table <table_name> shrink   space;  --下调HWM,影响DML操作
  ----方法5:online redefinition
  --online redefinition具有的应用场景:
  1).Online table redefinition enables you to:
  2).Modify the storage parameters of a table or cluster
  3).Move a table or cluster to a different tablespace
  4).Add or drop partitioning support (non-clustered tables only)
  5).Change partition structure
  6).Change physical properties of a single table partition, including moving it to a different tablespace in the same schema
  7).Change physical properties of a materialized view log or an Oracle Streams Advanced Queueing queue table
  8).Add support for parallel queries
  9).Re-create a table or cluster to reduce fragmentation
  10).Convert a relational table into a table with object columns, or do the reverse.
  11).Convert an object table into a relational table or a table with object columns, or do the reverse.
  ---整理步骤
  --步骤1:检测表是否具有按主键进行ONLINE REDIFINITION能力
  BEGIN
  DBMS_REDEFINITION.CAN_REDEF_TABLE('scott','t1',
  DBMS_REDEFINITION.CONS_USE_PK);
  END;
  /
  --步骤2:新建一张同结构的临时表
  create table scott.tp1 tablespace ocpyang
  as
  select * from scott.t1 where 1=2;
  --步骤3:启动ONLINE REDIFINITION
  BEGIN
  DBMS_REDEFINITION.START_REDEF_TABLE('scott', 't1','tp1',
  '',
  dbms_redefinition.cons_use_pk);
  END;
  /
  --步骤4:Copy dependent objects. (Automatically create any triggers, indexes, materialized view logs,
  grants, and constraints on scott.tblorders.)
  DECLARE
  num_errors PLS_INTEGER;
  BEGIN
  DBMS_REDEFINITION.COPY_TABLE_DEPENDENTS('scott', 't1','tp1',
  DBMS_REDEFINITION.CONS_ORIG_PARAMS, TRUE, TRUE, TRUE, TRUE, num_errors);
  END;
  /
  --步骤5:检查是否除primary、constraint之外的错误
  select object_name, base_table_name, ddl_txt from
  DBA_REDEFINITION_ERRORS;
  --步骤6:Optionally, synchronize the interim table hr.int_admin_emp.
  BEGIN
  DBMS_REDEFINITION.SYNC_INTERIM_TABLE('scott', 't1', 'tp1');
  END;
  /
  --步骤7:Complete the redefinition.
  BEGIN
  DBMS_REDEFINITION.FINISH_REDEF_TABLE('scott', 't1', 'tp1');
  END;
  /
  NOTE:
  The table scott.tblorders is locked in the exclusive mode only for a small window toward the end of this step.
  After this call the table scott.tblorders is redefined such that it has all the attributes of the scott.tptblorders table.
  ------------------------------------------------*
  5.3 索引碎片整理
  ------------------------------------------------*
  alter index <index_name> rebuild online parallel 4 nologging;
  alter table <index_name> coalesce;
  由于rebuild index可以在线、并行、不产生日志方式进行.推荐使用rebuild index.
  ********************************************************************************
  6.佳实践
  ********************************************************************************
  1.针对表的碎片化优先考虑shrink技术;针对索引的碎片优先考虑rebuild index技术;
  2.如果shrink不理想则采用online redefinition技术
  3.如果空间不够导致rebuild index无法实施则考虑coalesce技术
  4.虽然shrink和rebuild index都不影响在线应用但保险起见尽量避免在业务高峰执行
  5.shrink技术考虑先压缩数据不下调HWM,然后找业务低谷时间再下调HWM并释放空间
  6.建议rebuild index以非ONLINE方式执行虽然支持online.