rebuild table online

The main steps of table redefinition are:
1, Verify the source table is able to be online redefine:
BEGIN Dbms_Redefinition.Can_Redef_Table('USER','SOURCE TABLE NAME'); END;
2, Create your empty interim table on new tablespace with the same structure as the source table:
3, Start the redefinition of the source table:
BEGIN Dbms_Redefinition.start_redef_table('USER','SOURCE TABLE NAME','INTERIM TABLE NAME‘); END;
4, Create the constraints, indexes and triggers on the interim table as they are defined on the source table.
5, Sync interim table with source table data:
BEGIN Dbms_Redefinition.SYNC_INTERIM_TABLE('USER','SOURCE TABLE NAME','INTERIM TABLE NAME‘); END;
6, Finish the redefinition of the source table:
BEGIN Dbms_Redefinition.FINISH_REDEF_TABLE('USER','SOURCE TABLE NAME','INTERIM TABLE NAME‘); END;


附:DBMS_REDEFINITION ONLINE REORGANIZATION OF TABLES
PURPOSE
-------

To provide information on the DBMS_REDEFINITION package.
 
SCOPE & APPLICATION
-------------------

Skill Level - Advanced

-----------------------------
 
DBMS_REDEFINITION AND ONLINE REORGANIZATION OF TABLES


DBMS_REDEFINITION
==================

With DBMS_REDEFINITION, you can perform an online reorganization of tables. To
achieve this online reorganization, incrementally maintainable local
materialized views are used. Snapshot logs need to be defined on the master
tables to support incrementally maintainable materialized views. These logs
keep track of the changes to the master tables and are used by the materialized
views during refresh synchronization. To keep table indexes and privileges you must use the
copy_table_dependents procedure.


Restrictions
=============

Tables with the following characteristics cannot be redefined online:
  - [9.0.1]Tables with no primary keys
  - Tables that have materialized view logs defined on them
  - [9i] Tables that are materialized view container tables and AQ tables
  - [10g] Tables that are replicated in an n-way master configuration can
    be redefined, but horizontal subsetting (subset of rows in the table),
    vertical subsetting (subset of columns in the table), and column
    transformations are not allowed.
  - The overflow table of an IOT table
  - Tables with fine-grained access control (row-level security)
  - Tables with BFILE columns
  - Tables with LONG columns can be redefined online, but those columns must be
    converted to CLOBS. Also, LONG RAW columns must be converted to BLOBS.
    Tables with LOB columns are acceptable.
  - Tables in the SYS and SYSTEM schema
  - Temporary tables

 
Other restrictions:
  - A subset of rows in the table
  - Only simple deterministic expressions, sequences, and SYSDATE can be used
    when mapping the columns in the interim table to those of the original table.
    For example, subqueries are not allowed.
  - If new columns are being added with no column mappings, then they must not
    be declared NOT NULL until the redefinition is complete.
  - There cannot be any referential constraints between the table being redefined
    and the interim table.
  - Table redefinition cannot be done NOLOGGING.
  - [10g] For materialized view logs and queue tables, online redefinition is
    restricted to changes in physical properties.
  - You cannot convert a nested table to a VARRAY.


Privileges Required
====================
Following privileges are needed to run this package:  
  - Execute privilege to DBMS_REDEFINITION
  - Create any table
  - Alter any table
  - Drop any table
  - Lock any table
  - Select any table


Summary of DBMS_REDEFINITION Subprograms:
=========================================

CAN_REDEF_TABLE Procedure:
--------------------------
This procedure determines if a given table can be reorganized online. This is
the first step of the online reorganization process. If the table is not a
candidate for online redefinition, an error message is raised.

SYNTAX

DBMS_REDEFINITION.can_redef_table (
uname IN VARCHAR2,
tname IN VARCHAR2);

CAN_REDEF_TABLE Procedure Parameters:

Parameter          Description
---------          ------------
uname              The schema name of the table.
tname              The name of the table to be reorganized.



START_REDEF_TABLE Procedure:
----------------------------
This procedure initiates the reorganization process. After verifying that the
table can be reorganized online, you create an empty interim table (in the same
schema as the table to be reorganized) with the desired attributes of the
post-reorganization table.

SYNTAX

DBMS_REDEFINITION.start_redef_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2,
col_mapping IN VARCHAR2 := NULL);

START_REDEF_TABLE Procedure Parameters:

Parameter          Description
----------         ------------
uname              The schema name of the tables.
orig_table         The name of the table to be reorganized.
int_table          The name of the interim table.
col_mapping        The mapping information from the columns in the interim
                   table to the columns in the original table. (This is similar
                   to the column list on the SELECT clause of a query.) If NULL,
                   all the columns in the original table are selected and have
                   the same name after reorganization.

FINISH_REDEF_TABLE Procedure:
----------------------------
This procedure completes the reorganization process.  Before this step you can
create new indexes, triggers, grants, and constraints on the interim table. The
referential constraints involving the interim table must be disabled.  After
completing this step, the original table is locked briefly during this
procedure.

 
SYNTAX

DBMS_REDEFINITION.finish_redef_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2);

FINISH_REDEF_TABLE Procedure Parameters:

Parameter           Description
---------           ------------
uname               The schema name of the tables.
orig_table          The name of the table to be reorganized.
int_table           The name of the interim table.



SYNC_INTERIM_TABLE Procedure:
----------------------------
This procedure keeps the interim table synchronized with the original table.
This step is useful in minimizing the amount of synchronization needed to be
done by finish_reorg_table before completing the online reorganization. This
procedure can be called between long running operations (such as create index)
on the interim table to sync it up with the data in the original table and
speed up subsequent operations.

SYNTAX

DBMS_REDEFINITION.sync_interim_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2);

SYNC_INTERIM_TABLE Procedure Parameters:

Parameters          Description
----------          ------------
uname               The schema name of the tables.
orig_table          The name of the table to be reorganized.
int_table           The name of the interim table.


ABORT_REDEF_TABLE Procedure:
----------------------------
This procedure cleans up errors that occur during the reorganization process.
This procedure can also be used to abort the reorganization process any time
after start_reorg_table has been called and before finish_reorg_table is called.

SYNTAX

DBMS_REDEFINITION.abort_redef_table (
uname IN VARCHAR2,
orig_table IN VARCHAR2,
int_table IN VARCHAR2);

ABORT_REDEF_TABLE Procedure Parameters:

Parameters          Description
----------          ------------
uname               The schema name of the table.
orig_table          The name of the table to be reorganized.
int_table           The name of the interim table.


If we have an encrypted column which is part of primary key then
follow the steps mentioned in the bug below.

Reference: Bug 6034260 DBMS_REDEFINITION.START_REDEF_TABLE FAILS ORA-32412 BECAUSE
OF ENCRYPTED COLUMN

COPY_TABLE_DEPENDENTS  (Procedure)
Copies the dependent objects of the original table to the interim table


COPY_TABLE_DEPENDENTS.dbms_redefinition.copy_table_dependents(
uname               IN  VARCHAR2,
orig_table           IN  VARCHAR2,
int_table             IN  VARCHAR2,
copy_indexes     IN  PLS_INTEGER := 1,
copy_triggers      IN  BOOLEAN := TRUE,
copy_constraints IN  BOOLEAN := TRUE,
copy_privileges   IN  BOOLEAN := TRUE,
ignore_errors      IN  BOOLEAN := FALSE,
num_errors       OUT PLS_INTEGER,
copy_statistics  IN  BOOLEAN := FALSE
copy_mvlog       IN  BOOLEAN := FALSE);

RELATED DOCUMENTS
-----------------

Oracle9i Database Administrator's Guide Part Number A90117-01
Oracle9i Supplied PL/SQL Packages and Types Reference Part Number A89852-02
一、重建索引的前提 1、表上频繁发生update,delete操作; 2、表上发生了alter table ..move操作(move操作导致了rowid变化)。 二、重建索引的标准 1、索引重建是否有必要,一般看索引是否倾斜的严重,是否浪费了空间, 那应该如何才可以判断索引是否倾斜的严重,是否浪费了空间, 对索引进行结构分析(如下): SQL>Analyze index index_name validate structure; 2、在执行步骤1的session中查询index_stats表,不要到别的session去查询。 SQL>select height,DEL_LF_ROWS/LF_ROWS from index_stats; 说明:当 查询出来的 height>=4 或者 DEL_LF_ROWS/LF_ROWS>0.2 的场合 , 该索引考虑重建 。 举例: (t_gl_assistbalance 26 万多条信息 ) SQL> select count(*) from t_gl_assistbalance ; 输出结果: COUNT(*) ---------- 265788 SQL> Analyze index IX_GL_ASSTBAL_1 validate structure; Index analyzed SQL> select height,DEL_LF_ROWS/LF_ROWS from index_stats; 输出结果: HEIGHT DEL_LF_ROWS/LF_ROWS ---------- ------------------- 4 1 三、重建索引的方式 1、drop 原来的索引,然后再创建索引; 举例: 删除索引:drop index IX_PM_USERGROUP; 创建索引:create index IX_PM_USERGROUP on T_PM_USER (fgroupid); 说明:此方式耗时间,无法在24*7环境中实现,不建议使用。 2 、直接重建: 举例: alter index indexname rebuild; 或alter index indexname rebuild online; 说明:此方式比较快,可以在24*7环境中实现,建议使用此方式。 四、alter index rebuild 内部过程和注意点 alter index rebuild 和alter index rebuil online的区别 1、扫描方式不同 1.1、Rebuild以index fast full scan(or table full scan) 方式读取原索引中的数据来构建一个新的索引,有排序的操作; 1.2、rebuild online 执行表扫描获取数据,有排序的操作; 说明:Rebuild 方式 (index fast full scan or table full scan 取决于统计信息的cost) 举例1 SQL> explain plan for alter index IX_GL_ASSTBAL_1 rebuild; Explained SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | --------------------------------------------------------------------- | 0 | ALTER INDEX STATEMENT | | 999K| 4882K| 3219 | | 1 | INDEX BUILD NON UNIQUE| IDX_POLICY_ID2 | | | | | 2 | SORT CREATE INDEX | | 999K| 4882K| | | 3 | INDEX FAST FULL SCAN | IDX_POLICY_ID2 | 999K| 4882K| | --------------------------------------------------------------------- 举例2 SQL> explain plan for alter index idx_policy_id rebuild; Explained SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | --------------------------------------------------------------------- | 0 | ALTER INDEX STATEMENT | | 2072K| 9M| 461 | | 1 | INDEX BUILD NON UNIQUE| IDX_POLICY_ID | | | | | 2 | SORT CREATE INDEX | | 2072K| 9M| | | 3 | TABLE ACCESS FULL | TEST_INDEX | 2072K| 9M| 461 | 举例3 ( 注意和 举例1 比较 ) Rebuil online 方式 : SQL> explain plan for alter index idx_policy_id2 rebuild online; Explained SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT --------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost | ---------------------------------------------------------------------| 0 | ALTER INDEX STATEMENT | | 999K| 4882K| 3219 | | 1 | INDEX BUILD NON UNIQUE| IDX_POLICY_ID2 | | | | | 2 | SORT CREATE INDEX | | 999K| 4882K| | | 3 | TABLE ACCESS FULL | TEST_INDEX2 | 999K| 4882K| 3219 | 2 、rebuild 会阻塞 dml 操作 ,rebuild online 不会阻塞 dml 操作 ; 3 、rebuild online 时系统会产生一个 SYS_JOURNAL_xxx 的 IOT 类型的系统临时日志表 , 所有 rebuild online 时索引的变化都记录在这个表中 , 当新的索引创建完成后 , 把这个表的记录维护到新的索引中去 , 然后 drop 掉旧的索引 ,rebuild online 就完成了。 注意点: 1、 执行rebuild操作时,需要检查表空间是否足够; 2、虽然说rebuild online操作允许dml操作,但是还是建议在业务不繁忙时间段进行; Rebuild操作会产生大量redo log ; 五、重建分区表上的分区索引 重建分区索引方法: Alter index indexname rebuild partition paritionname tablespace tablespacename; Alter index indexname rebuild subpartition partitioname tablespace tablespacename; Partition name 可以从user_ind_partitions查找 Tablepace 参数允许alter index操作更改索引的存储空间; 六、索引状态描述 在数据字典中查看索引状态,发现有三种: valid:当前索引有效 N/A :分区索引 有效 unusable:索引失效 七、术语 1、高基数:简单理解就是表中列的不同值多。 2、低基数:建单理解就是表中的列的不同值少。 3、以删除的叶节点数量:指得是数据行的delete操作从逻辑上删除的索引节点 的数量,要记住oracle在删除数据行后,将 “ 死 “ 节点保留在索引中,这样做可以加快sql删除操作的速度,因此oracle删除数据行后可以不必重新平衡索引。 4、索引高度:索引高度是指由于数据行的插入操作而产生的索引层数,当表中添加大量数据时,oracle将生成索引的新层次以适应加入的数据行,因此,oracle索引可能有4层,但是这只会出现在索引数中产生大量插入操作的区域。Oracle索引的三层结构可以支持数百万的项目,而具备4层或是更多层的需要重建。 5、每次索引访问的读取数:是指利用索引读取一数据行时所需要的逻辑I/O操作数,逻辑读取不必是物理读取,因为索引的许多内容已经保存在数据缓冲区,然而,任何数据大于10的索引都需要重建。 6、什么时候重建呢? 察看 dba_indexes 中的 blevel 。这列是说明索引从根块到叶快的级别,或是深度。如果级别大于等于4。则需要重建, 如下 :Select index_name,blevel from dba_indexes where blevel>=4. 另一个从重建中受益的指标显然是当该索引中的被删除项占总的项数的百分比。如果在20%以上时,也应当重建,如下 SQL>analyze index index_name validate structure SQL>select (del_lf_rows_len/lf_rows_len)*100 from index_stats where name= ’ index_name ’ 就能看到是否这个索引被删除的百分比。 7、什么样的重建方式更好? (1)、建索引的办法: 1.1、删除并从头开始建立索引。 1.2 、 使用 alter index index_name rebuild 命令重建索引。 1.3 、 使用 alter index index_name coalesce 命令重建索引。 (2)、下面讨论一下这三种方法的优缺点: 2.1、删除并从头开始建索引:方法是最慢的,最耗时的。一般不建议。 2.2、Alter index index_name rebuild 快速重建索引的一种有效的办法,因为使用现有索引项来重建新索引,如果客户操作时有其他用户在对这个表操作,尽量使用带online参数来最大限度的减少索引重建时将会出现的任何加锁问题,alter index index_name rebuild online。 但是,由于新旧索引在建立时同时存在,因此,使用这种技巧则需要有额外的磁盘空间可临时使用,当索引建完后把老索引删除,如果没有成功,也不会影响原来的索引。利用这种办法可以用来将一个索引移到新的表空间。 Alter index index_name rebuild tablespace tablespace_name 。 这个命令的执行步骤如下: 首先,逐一读取现有索引,以获取索引的关键字。 其次,按新的结构填写临时数据段。 最后,一旦操作成功,删除原有索引树,降临时数据段重命名为新的索引。 需要注意的是alter index index_name rebuild 命令中必须使用tablespace字句,以保证重建工作是在现有索引相同的表空间进行。 2.3、alter index index_name coalesce 使用带有coalesce参数时重建期间不需要额外空间,它只是在重建索引时将处于同一个索引分支内的叶块拼合起来,这最大限度的减少了与查询过程中相关的潜在的加锁问题,但是,coalesce选项不能用来将一个索引转移到其他表空间。 八、其他 1、truncate 分区操作和truncate 普通表的区别? 1.1、Truncate 分区操作会导致全局索引失效; truncate 普通表对索引没有影响; 1.2、Truncate 分区操作不会释放全局索引中的空间,而truncate 普通表会释放索引所占空间; 2、rename 表名操作对索引没有影响,因为rename操作只是更改了数据字典,表中数据行的rowid并没有发生变化 总结: 1、判断是否需要重建索引: SQL>analyze index index_name validate structure; SQL> select height,DEL_LF_ROWS/LF_ROWS from index_stats; ( 或 Select index_name,blevel from dba_indexes where blevel>=4 ); 说明 : 当查询出来的 height>=4 或者 DEL_LF_ROWS/LF_ROWS>0.2 的场合 , 该索引考虑重建 ; 2 、重建索引方法 : 方法一、 Alter index index_name rebuild tablespace tablespace_name; 优点:是快速重建索引的一种有效的办法,可以用来将一个索引移到新的表空间。 缺点:重建期间需要额外空间。 方法二、 alter index index_name coalesce; 优点:重建期间不需要额外空间。 缺点:coalesce选项不能用来将一个索引转移到其他表空间。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值