Oracle In-Database Archiving演示

See here for a demo in Oracle LiveSQL .

This article uses the example of Oracle By Examples .

overview

In-database archiving is a 12c feature.

This tutorial shows how to use in-database archiving to store all rows in the database, but hide specific rows from the application. Experiment for about 15 minutes.

In-database archiving enables you to archive rows in a table by marking them as invisible. These invisible rows are in the database and optimized using compression ( is it automatic? ), but are not visible to the application. The data in these rows can be used for compliance purposes by setting session parameters if desired.

With in-database archiving, you can store more data in a single database for longer periods of time without impacting application performance. Archive data can be compressed to help improve backup performance, and updates to archive data can be deferred during application upgrades to improve upgrade performance.

To manage in-database archiving for a table, you must enable ROW ARCHIVAL for the table, manipulate the table's ORA_ARCHIVE_STATE hidden column, and specify ACTIVE or ALL for the ROW ARCHIVAL VISIBILITY session parameter.

Create table and enable row archiving

connect hr/****@orclpdb1;

create table emp_arch
as select employee_id, first_name from employees where rownum <= 4;

alter table emp_arch row archival;

-- 不显示隐藏列
desc emp_arch;
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPLOYEE_ID                                        NUMBER(6)
 FIRST_NAME                                         VARCHAR2(20)

-- 显示隐藏列
col ORA_ARCHIVE_STATE for a10
select employee_id, first_name, ora_archive_state from emp_arch;

EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0
        102 Lex                  0
        103 Alexander            0

select * from emp_arch;
EMPLOYEE_ID FIRST_NAME
----------- --------------------
        100 Steven
        101 Neena
        102 Lex
        103 Alexander

set archive status for some lines

-- 修改归档状态,直接修改为'1'也可以
update emp_arch
set ora_archive_state=dbms_ilm.archivestatename(1)
where employee_id in (102, 103);

commit;

-- 看不到已归档的行
select employee_id, first_name, ora_archive_state from emp_arch;
EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0

Archived line showing session settings

-- 看到所有行
alter session set row archival visibility = all;

select employee_id, first_name, ora_archive_state from emp_arch;
EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0
        102 Lex                  1
        103 Alexander            1

-- 看到未归档的行
alter session set row archival visibility = active;

select employee_id, first_name, ora_archive_state from emp_arch;

EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0

Determining that copying archived rows for tables does not copy archive status

Note that although the archive status virtual column is not copied, session settings affect the number of rows copied.

-- all会拷贝所有行,而active只会拷贝可见(非归档)的行
alter session set row archival visibility = all;
create table emp_arch_copy as select * from emp_arch;

select employee_id, first_name, ora_archive_state from emp_arch_copy;
select employee_id, first_name, ora_archive_state from emp_arch_copy
                                *
ERROR at line 1:
ORA-00904: "ORA_ARCHIVE_STATE": invalid identifier

select employee_id, first_name from emp_arch_copy;
EMPLOYEE_ID FIRST_NAME
----------- --------------------
        100 Steven
        101 Neena
        102 Lex
        103 Alexander


alter table emp_arch_copy row archival;
select employee_id, first_name, ora_archive_state from emp_arch_copy;
EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0
        102 Lex                  0
        103 Alexander            0

update emp_arch_copy
set ora_archive_state=dbms_ilm.archivestatename(1)
where employee_id in (102, 103);

commit;

select employee_id, first_name, ora_archive_state from emp_arch_copy;

EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0
        102 Lex                  1
        103 Alexander            1

select employee_id, first_name, ora_archive_state from emp_arch;

EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0
        102 Lex                  1
        103 Alexander            1

insert into emp_arch_copy select employee_id, first_name || '_New' from emp_arch;

commit;

select employee_id, first_name, ora_archive_state from emp_arch_copy;

EMPLOYEE_ID FIRST_NAME           ORA_ARCHIV
----------- -------------------- ----------
        100 Steven               0
        101 Neena                0
        102 Lex                  1
        103 Alexander            1
        100 Steven_New           0
        101 Neena_New            0
        102 Lex_New              0
        103 Alexander_New        0

As can be seen from the last command, the archive status has not been copied.

reset environment

drop table emp_arch purge;

drop table emp_arch_copy purge;

reference

  • https://oracle-base.com/articles/12c/in-database-archiving-12cr1
  • https://www.youtube.com/watch?v=fvGkKJeQFTs

Guess you like

Origin blog.csdn.net/stevensxiao/article/details/128598075