Note_Tech

All technological notes.


Project maintained by simonangel-fong Hosted on GitHub Pages — Theme by mattgraham

Flashback - Flashback Data Archives

Back


Flashback Data Archives

Archive 不使用 undo, 而是使用 temporal table/tbsp

diagram_archive


Creating a Temporal History and Enabling Archiving

-- creat a flashback archive named fda1
-- point the fda1 to the tbsp fda_tbs1
-- define a retention as one year
-- use optimize data to enable efficient storage
CREATE FLASHBACK ARCHIVE fda1 TABLESPACE fda_tbs1
OPTIMIZE DATA QUOTA 10M RETENTION 1 YEAR;
  1. Enable the flashback archiving (or disable it) for a (whole) table.
    • This task requires the FLASHBACK ARCHIVE object privilege.
    • By default, flashback archiving is off for any table.
    • Although flashback archiving is enabled for a table, some DDL statements are not allowed on that table.
-- turn on flashback archive for the table to fda1
ALTER TABLE HR.EMPLOYEES FLASHBACK ARCHIVE fda1;

How the Flashback Data Archive Works

diagram_fda



Partitions

process and slaves:


Collecting User Context in Temporal History

EXEC DBMS_FLASHBACK_ARCHIVE.SET_ CONTEXT LEVEL ('TYPICAL')
/
SELECT
    DBMS_FLASHBACK_ARCHIVE.GET_SYS_CONTEXT
    (VERSIONS_XID, 'USERENV', 'SESSION USER'),
    VERSIONS_XID,
    VERSIONS_STARTTIME,
    VERSIONS_ENDTIME,
    employee_id,
    salary
FROM hr.employees VERSIONS BETWEEN SCN MINVALUE
AND MAXVALUE ;

Transparent Schema Evolution

diagram_schema01


Full Schema Evolution

diagram_schema

  1. If you have the FLASHBACK ARCHIVE ADMINISTER privilege, you can disassociate the archive from the base table with the DTSASSOCTATE_FBA procedure.
  2. Make the necessary changes to the base table.
  3. Make the necessary changes to the corresponding archive.
  4. Then associate the table with the archive within the same schema with the RESASSOCIATE_FBA procedure.
  • Temporal History validates that the schemas are the same upon association.

Temporal Validity and History

diagram_temp_valid

  • employee 400 was hired on March 22
  • the row was entered in the HR.EMP table on March 23.
  • 22-MAR-12 is the valid time temporal date
  • 23-MAR-12 is the transaction time temporal date

Using the PERIOD FOR Clause

-- Explicitly define the two date-time columns
CREATE TABLE emp
(
  empno number, salary number, deptid number,
  name VARCHAR2 (100),
  user_time_start DATE, user_time_end DATE,   -- Explicitly define
  PERIOD FOR user_time (user_time_start, user_time_end)
);

-- automatic valid-time columns
CREATE TABLE emp2
(
  empno number, salary number, deptid number,
  name VARCHAR2 (100),
  PERIOD FOR user_time    -- automatic define
  -- name of the valid-time dimension is user_time
  -- USER_TIME is used as the prefix for the two date columns automatically created: USER_TIME_START and USER_TIME_END.
);
-- insert
INSERT INTO emp2
(empno, salary, deptid, name, user_time_start, user_time_end)
VALUES (1,1000,20, 'John', SYSDATE, NULL);

-- query
select EMPNO, user_time_start, user_time_end from emp2;
-- EMPNO  USER_TIME_START                       USER_TIME_END
-- 1      17-AUG-12 09.58.03.000000 AM +00:00

Filtering on Valid-Time Columns


Example:


select * from hr.emp as of PERIOD FOR user_time
to_date('01-DEC-1992', 'dd-mon-yyyy') ;

即使用 period for 自动筛选在某个时间点上时间有效的记录.

example_valid


select * from hr.emp VERSIONS PERIOD FOR user_time
BETWEEN to_date('31-DEC-2011', 'dd-mon-yyyy')
AND to_date('31-DEC-2012', 'dd-mon-yyyy');

The query displays all employees whose VALID_TIME_START is less than or equal to ‘31-DEC-2011’and VALID_TIME_END greater than or equal to ‘31-DEC-2012”.

example_valid


select * from hr.emp
as of period for user time to_date('31-DEC-1992', 'dd-mon-yyyy"')
as of timestamp to_date ('30-mar-2012', 'dd-mon-yyyy');

Using DBMS_FLASHBACK_ARCHIVE

-- Set the visibility to data to the full table level, the default temporal table visibility.
DBMS_FLASHBACK_ARCHIVE.ENABLE_AT_VALID_TIME ('ALL')

-- Set the visibility to data valid as of the given time.
-- showing only rows overlapping the given date.
DBMS_FLASHBACK_ARCHIVE.ENABLE_AT_VALID_TIME(
  'ASOF',
  (to_timestamp ('29-SEP-10 05.44.01 PM'))
);

-- Set the visibility to data currently valid within the valid time period at the session level.
DBMS_FLASHBACK_ARCHIVE.ENABLE_AT_VALID_TIME ('CURRENT')

TOP