您现在的位置是:网站首页> 编程资料编程资料
ORACLE分区表转换在线重定义DBMS_REDEFINITION_oracle_
2023-05-27
576人已围观
简介 ORACLE分区表转换在线重定义DBMS_REDEFINITION_oracle_
一、DBMS_REDEFINITION(在线重定义)
参考MOS文档:_How To Partition Existing Table Using DBMS_REDEFINITION (Doc ID 472449.1) _
支持的数据库版本:Oracle Database - Enterprise Edition - Version 9.2.0.4 and later
在线重定义是通过 物化视图 实现的。
使用在线重定义的一些限制条件:
- 1、必须有足够的表空间来容纳表的两倍数据量。
- 2、主键列不能被修改。
- 3、表必须有主键。
- 4、必须在同一个用户下进行在线重定义。
- 5、SYS和SYSTEM用户下的表无法进行在线重定义。
- 6、在线重定义无法采用nologging。
- 7、如果中间表有新增列,则不能有NOT NULL约束
DBMS_REDEFINITION包:
- ABSORT_REDEF_TABLE:清理重定义的错误和中止重定义;
- CAN_REDEF_TABLE:检查表是否可以进行重定义,存储过程执行成功代表可以进行重定义;
- COPY_TABLE_DEPENDENTS:同步索引和依赖的对象(包括索引、约束、触发器、权限等);
- FINISH_REDEF_TABLE:完成在线重定义;
- REGISTER_DEPENDENTS_OBJECTS:注册依赖的对象,如索引、约束、触发器等;
- START_REDEF_TABLE:开始在线重定义;
- SYNC_INTERIM_TABLE:增量同步数据;
- UNREGISTER_DEPENDENT_OBJECT:不注册依赖的对象,如索引、约束、触发器等;
CREATE OR REPLACE PACKAGE SYS.dbms_redefinition AUTHID CURRENT_USER IS ------------ -- OVERVIEW -- -- This package provides the API to perform an online, out-of-place -- redefinition of a table --- ========= --- CONSTANTS --- ========= -- Constants for the options_flag parameter of start_redef_table cons_use_pk CONSTANT PLS_INTEGER := 1; cons_use_rowid CONSTANT PLS_INTEGER := 2; -- Constants used for the object types in the register_dependent_object cons_index CONSTANT PLS_INTEGER := 2; cons_constraint CONSTANT PLS_INTEGER := 3; cons_trigger CONSTANT PLS_INTEGER := 4; cons_mvlog CONSTANT PLS_INTEGER := 10; -- constants used to specify the method of copying indexes cons_orig_params CONSTANT PLS_INTEGER := 1; PRAGMA SUPPLEMENTAL_LOG_DATA(default, AUTO_WITH_COMMIT); -- NAME: can_redef_table - check if given table can be re-defined -- INPUTS: uname - table owner name -- tname - table name -- options_flag - flag indicating user options to use -- part_name - partition name PROCEDURE can_redef_table(uname IN VARCHAR2, tname IN VARCHAR2, options_flag IN PLS_INTEGER := 1, part_name IN VARCHAR2 := NULL); PRAGMA SUPPLEMENTAL_LOG_DATA(can_redef_table, NONE); -- NAME: start_redef_table - start the online re-organization -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- col_mapping - select list col mapping -- options_flag - flag indicating user options to use -- orderby_cols - comma separated list of order by columns -- followed by the optional ascending/descending -- keyword -- part_name - name of the partition to be redefined PROCEDURE start_redef_table(uname IN VARCHAR2, orig_table IN VARCHAR2, int_table IN VARCHAR2, col_mapping IN VARCHAR2 := NULL, options_flag IN BINARY_INTEGER := 1, orderby_cols IN VARCHAR2 := NULL, part_name IN VARCHAR2 := NULL); -- NAME: finish_redef_table - complete the online re-organization -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- part_name - name of the partition being redefined PROCEDURE finish_redef_table(uname IN VARCHAR2, orig_table IN VARCHAR2, int_table IN VARCHAR2, part_name IN VARCHAR2 := NULL); -- NAME: abort_redef_table - clean up after errors or abort the -- online re-organization -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- part_name - name of the partition being redefined PROCEDURE abort_redef_table(uname IN VARCHAR2, orig_table IN VARCHAR2, int_table IN VARCHAR2, part_name IN VARCHAR2 := NULL); -- NAME: sync_interim_table - synchronize interim table with the original -- table -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- part_name - name of the partition being redefined PROCEDURE sync_interim_table(uname IN VARCHAR2, orig_table IN VARCHAR2, int_table IN VARCHAR2, part_name IN VARCHAR2 := NULL); -- NAME: register_dependent_object - register dependent object -- -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- dep_type - type of the dependent object -- dep_owner - name of the dependent object owner -- dep_orig_name- name of the dependent object defined on table -- being re-organized -- dep_int_name - name of the corressponding dependent object on -- the interim table PROCEDURE register_dependent_object(uname IN VARCHAR2, orig_table IN VARCHAR2, int_table IN VARCHAR2, dep_type IN PLS_INTEGER, dep_owner IN VARCHAR2, dep_orig_name IN VARCHAR2, dep_int_name IN VARCHAR2); -- NAME: unregister_dependent_object - unregister dependent object -- -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- dep_type - type of the dependent object -- dep_owner - name of the dependent object owner -- dep_orig_name- name of the dependent object defined on table -- being re-organized -- dep_int_name - name of the corressponding dependent object on -- the interim table PROCEDURE unregister_dependent_object(uname IN VARCHAR2, orig_table IN VARCHAR2, int_table IN VARCHAR2, dep_type IN PLS_INTEGER, dep_owner IN VARCHAR2, dep_orig_name IN VARCHAR2, dep_int_name IN VARCHAR2); -- NAME: copy_table_dependents -- -- INPUTS: uname - schema name -- orig_table - name of table to be re-organized -- int_table - name of interim table -- copy_indexes - integer value indicating whether to -- copy indexes -- 0 - don't copy -- 1 - copy using storage params/tablespace -- of original index -- copy_triggers - TRUE implies copy triggers, FALSE otherwise -- copy_constraints - TRUE implies copy constraints, FALSE -- otherwise -- copy_privileges - TRUE implies copy privileges, FALSE -- otherwise -- ignore errors - TRUE implies continue after errors, FALSE -- otherwise -- num_errors - number of errors that occurred while -- cloning ddl -- copy_statistics - TRUE implies copy table statistics, FALSE -- otherwise. -- If copy_indexes is 1, copy index -- related statistics, 0 otherwise. -- copy_mvlog - TRUE implies copy table's MV log, FALSE -- otherwise. PROCEDURE 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); END;
二、在线重定义表的步骤
1.创建未分区的表
创建未分区的表,如果存在,就不需要操作。
--前置准备:创建用户,表空间,授权用户。 SQL> create tablespace PARTITION; SQL> create user par identified by par; SQL> grant dba to par; --创建表,索引,授权,同义词 SQL> conn par/par Connected. -- Create table create table student( s_id number(8) PRIMARY KEY, s_name varchar2(20) not null, s_sex varchar2(8), s_birdate date, constraint u_1 unique(s_name), constraint c_1 check (s_sex in ('MALE','FEMALE'))) tablespace PARTITION; -- Add comments to the table comment on table STUDENT is '学生表'; -- Add comments to the columns comment on column STUDENT.s_name is '姓名'; comment on column STUDENT.s_sex is '性别'; comment on column STUDENT.s_birdate is '出生日期'; -- Create/Recreate indexes create index S_NAME_IDX on STUDENT (S_NAME, S_SEX) tablespace PARTITION; -- Create SYNONYM CREATE SYNONYM stu FOR student; -- Grant/Revoke object privileges grant select, insert, delete on STUDENT to SCOTT; --查看表结构 SQL> desc stu Name Null? Type ----------------------------------------- -------- ---------------------------- S_ID NOT NULL NUMBER(8) S_NAME NOT NULL VARCHAR2(20) S_SEX VARCHAR2(8) S_BIRDATE DATE --插入数据 begin for i in 0 .. 24 loop insert into student values (i, 'student_' || i, decode(mod(i, 2), 0, 'MALE', 'FEMALE'), add_months(to_date('2019-1-1', 'yyyy-mm-dd'), i)); end loop; commit; end; / 2.确认表是否存在主键,表空间是否足够,收集表统计信息。
--查看表主键 SQL> select cu.* from user_cons_columns cu, user_constraints au where cu.constraint_name = au.constraint_name and au.constraint_type = 'P' and au.table_name = 'STUDENT'; --查看表大小和表空间 --查看表空间 SQL> select tablespace_name from dba_segments where segment_type= 'TABLE' and segment_name='STUDENT' and owner='PAR'; --查看表大小 SQL> select sum(bytes/1024/1024) from dba_segments where segment_type= 'TABLE' and segment_name='STUDENT' and owner='PAR'; --查看表空间 select tbs_used_info.tablespace_name, tbs_used_info.alloc_mb, tbs_used_info.used_mb, tbs_used_info.max_mb, tbs_used_info.free_of_max_mb, tbs_used_info.used_of_max || '%' used_of_max_pct from (select a.tablespace_name, round(a.bytes_alloc / 1024 / 1024) alloc_mb, round((a.bytes_alloc - nvl(b.bytes_free, 0)) / 1024 / 1024) used_mb, round((a.bytes_alloc - nvl(b.bytes_free, 0)) * 100 / a.maxbytes) used_of_max, round((a.maxbytes - a.bytes_alloc + nvl(b.bytes_free, 0)) / 104
相关内容
- Oracle控制文件丢失恢复归档模式方法_oracle_
- 详解Oracle控制文件及日志文件的管理问题_oracle_
- Oracle中日期的使用方法实例_oracle_
- Oracle触发器和程序包的基本介绍_oracle_
- centos编译安装mariadb的详细过程_mariadb_
- MariaDB的安装与配置教程_mariadb_
- MariaDB Spider 数据库分库分表实践记录_mariadb_
- MariaDB表表达式之公用表表达式(CTE)_mariadb_
- debian10 mariadb安装过程详解_mariadb_
- Linux安装MariaDB数据库的实例详解_mariadb_
点击排行
本栏推荐
