Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the...

22
Flashback Query Total Recall and some of it’s uses By Clancy Bu*on Park Lane IT

description

 

Transcript of Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the...

Page 1: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Query  Total  Recall    

and  some  of  it’s  uses  By  

Clancy  Bu*on  Park  Lane  IT  

Page 2: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Introduc:on  •  Clancy  Bu*on  –  Oracle  DBA  at  Park  Lane  IT    •  7  years  experience  as  an  Oracle  Database  Administrator  working  for  clients  in  government  and  uDliDes  

•  Clancy.Bu*[email protected]  

Page 3: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Query  •  Available  since  version  9.2  

–  Implemented  on  Oracles  UNDO  based  read  consistency  

•  Select  statement  has  two  new  clauses  

•  AS  of      –  Returns  the  enDre  table  as  it  existed  at  a  point  in  Dme    

•  Versions  Between  –  returns  all  commiOed  versions  of  rows  that  existed  between  two  points  

in  Dme  

Page 4: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Query  Examples  •  select  *    

 from  scott.emp        as  of        timestamp  sysdate  -­‐  interval  '60'  minute;  

•  select  *      from  scott.emp        versions  between        timestamp  sysdate  -­‐  interval  '60'  minute                    and  sysdate  -­‐  interval  '5'  minute;  

Page 5: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Query    Versions  Pseudo  Columns  

•  Used  with  versions  between    

–  VERSIONS_STARTSCN  and  VERSIONS_STARTTIME  •   StarDng  System  Change  Number  (SCN)  or  TIMESTAMP  when  the  row  version  was  created.  This  pseudocolumn  

idenDfies  the  Dme  when  the  data  first  had  the  values  reflected  in  the  row  version.  Use  this  pseudocolumn  to  idenDfy  the  past  target  Dme  for  Oracle  Flashback  Table  or  Oracle  Flashback  Query.  If  this  pseudocolumn  is  NULL,  then  the  row  version  was  created  before  start.  

–  VERSIONS_ENDSCN  and  VERSIONS_ENDTIME  •   SCN  or  TIMESTAMP  when  the  row  version  expired.  If  the  pseudocolumn  is  NULL,  then  either  the  row  version  

was  current  at  the  Dme  of  the  query  or  the  row  corresponds  to  a  DELETE  operaDon.  

–  VERSIONS_XID  •  For  each  version  of  each  row,  returns  the  transacDon  ID  (a  RAW  number)  of  the  transacDon  that  created  the  

row  version.  

–  VERSIONS_OPERATION  •   OperaDon  performed  by  the  transacDon:  I  for  inserDon,  D  for  deleDon,  or  U  for  update.  The  version  is  that  of  

the  row  that  was  inserted,  deleted,  or  updated;  that  is,  the  row  a*er  an  INSERT  operaDon,  the  row  before  a  DELETE  operaDon,  or  the  row  affected  by  an  UPDATE  operaDon.  

Page 6: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Versions  Pseudo  Column    Query  Example  

•  Can  be  used  in  the  select  list,  the  where  clause  and  order  by  clause  

•  Select  versions_startscn        ,versions_endscn        ,versions_operation        ,emp.*      from  scott.emp  

         versions  between          timestamp  sysdate  -­‐  interval  '20'  hour    

   and  sysdate  -­‐  interval  '5'  minute  emp    where  versions_operation='U'    order  by  versions_startscn;  

Page 7: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Total  Recall  •  Total  Recall  is  new  in  version  11  

•  Introduces  Flashback  Archives  to  the  database  

•  Built  on  ParDDoning  and  Advanced  Compression  technology  

•  Introduces  a  new  background  process  FBDA  

Page 8: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Archive  •  New  privilege  FLASHBACK  ARCHIVE  ADMINISTER  

•  New  DDL  statement  –  create  flashback  archive  flba1    tablespace  flash_archives      retention  10  day;  

 

•  Creates  archive  tables  in  a  normal  tablespace  automaDcally  

•  Enabled  per  table  –  alter  table  scott.emp  flashback  archive  flba1;  

Page 9: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Archive  •  DicDonary  views  – dba_flashback_archive  

– dba_flashback_archive_tables  

OWNER_NAME  FLASHBACK_ARCHIVE_NAME  

FLASHBACK_ARCHIVE#   RETENTION_IN_DAYS   CREATE_TIME   LAST_PURGE_TIME   STATUS  

SYS   FLBA1   1   10  

28/JUL/11  02:57:57.000000000  PM  

28/JUL/11  02:57:57.000000000  PM  

TABLE_NAME   OWNER_NAME   FLASHBACK_ARCHIVE_NAME   ARCHIVE_TABLE_NAME   STATUS  

EMP   SCOTT   FLBA1   SYS_FBA_HIST_73257   ENABLED  

Page 10: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flashback  Archive  -­‐  Internal  Tables  •  Enabling  flashback  archive  on  a  table  automaDcally  creates  three  new  tables  in  the  

schema  

•  The  table  name  is  formed  by  SYS_FBA_<purpose>_<FBDA_object_iden3fier>  

•  SYS_FBA_DDL_COLMAP_<object_id>  –  Records  current  and  past  columns  that  existed  on  the  base  table  (supports  DDL  on  the  base  

table)  

•  SYS_FBA_HIST_<object_id>  –  Contains  the  actual  historical  values  

•  SYS_FBA_TCRV_<object_id>  –  Maps  start  and  end  SCN  to  rowids  in  the  base  table  to  idenDfy  the  current  version  of  the  row  

Page 11: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

SYS_FBA_DDL_COLMAP_73257  CREATE  TABLE  "SCOTT"."SYS_FBA_DDL_COLMAP_73257"      (          "STARTSCN"        NUMBER,          "ENDSCN"            NUMBER,          "XID"        RAW(8),          "OPERATION"                              VARCHAR2(1  BYTE),          "COLUMN_NAME"                          VARCHAR2(255  BYTE),          "TYPE"                                        VARCHAR2(255  BYTE),          "HISTORICAL_COLUMN_NAME"    VARCHAR2(255  BYTE)      )      SEGMENT  CREATION  IMMEDIATE  NOCOMPRESS  LOGGING  TABLESPACE  

"FLASH_ARCHIVES"  ;  

Page 12: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

SYS_FBA_TCRV_73257  CREATE  TABLE  "SCOTT"."SYS_FBA_TCRV_73257"      (          "RID"              VARCHAR2(4000  BYTE),          "STARTSCN"    NUMBER,          "ENDSCN"        NUMBER,          "XID"    RAW(8),          "OP"      VARCHAR2(1  BYTE)      )      SEGMENT  CREATION  IMMEDIATE  NOCOMPRESS  LOGGING  TABLESPACE  "FLASH_ARCHIVES"  ;      CREATE  INDEX  "SCOTT"."SYS_FBA_TCRV_IDX_73257"  ON  "SCOTT"."SYS_FBA_TCRV_73257"      (          "RID"      )      COMPUTE  STATISTICS  TABLESPACE  "FLASH_ARCHIVES"  ;  

Page 13: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

SYS_FBA_HIST_73257  CREATE  TABLE  "SCOTT"."SYS_FBA_HIST_73257"      (          "RID"                VARCHAR2(4000  BYTE),          "STARTSCN“      NUMBER,          "ENDSCN"          NUMBER,          "XID“      RAW(8),          "OPERATION“      VARCHAR2(1  BYTE),          "EMPNO"              NUMBER(4,0),          "ENAME"              VARCHAR2(10  BYTE),          "JOB"                VARCHAR2(9  BYTE),          "MGR"                NUMBER(4,0),          "HIREDATE“    DATE,          "SAL"            NUMBER(7,2),          "COMM"          NUMBER(7,2),          "DEPTNO"      NUMBER(2,0)      )      COMPRESS  FOR  OLTP        TABLESPACE  "FLASH_ARCHIVES"  PARTITION  BY  RANGE      (          "ENDSCN"      )      (          PARTITION  "HIGH_PART"  VALUES  LESS  THAN  (MAXVALUE)    TABLESPACE  "FLASH_ARCHIVES"  COMPRESS  FOR  OLTP      )  ;  

Page 14: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Historical  Indexes  •  Indexes  on  base  table  aren’t  mirrored  on  history  tables  

•  Indexes  can  be  created  on  history  tables  •  E.g.  local  prefixed  index  created  for  start  and  end  scn  

     CREATE  INDEX  "SCOTT"."EMP_SCN_INDEX"  ON  "SCOTT"."SYS_FBA_HIST_73257"  

     (        "ENDSCN",          "STARTSCN"        )  local;    

Page 15: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Par::oning  and  Compression  •  PARTITION  BY  RANGE  clause  on  SCN  column  of  SYS_FBA_HIST  tables  – Oracle  ParDDoning  opDon  

 •  COMPRESS  FOR  OLTP  clause  on  SYS_FBA_HIST  tables  – Oracle  Advanced  Compression  opDon    

Page 16: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flash  Back  Data  Archiver  (FBDA)  •  Is  a  new  background  process  

•  Maintains  the  Flashback  archives    •  FBDA  archives  the  historical  rows  of  tracked  tables  into  

flashback  data  archives.    

•  FBDA  is  also  responsible  for  automaDcally  managing  the  flashback  data  archive  for  space,  organizaDon,  and  retenDon  and  keeps  track  of  how  far  the  archiving  of  tracked  transacDons  has  occurred.  

Page 17: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Flash  Back  Data  Archiver  (FBDA)  •  Dynamic  based  on  DML  workload  

•  AutomaDcally  spawns  parallel  slaves  

•  Run  asynchronously  by  default  every  5  minutes  

•  Runs  more  frequently  depending  on  workload  

•  Reads  UNDO  buffers  from  cache,  or  from  disk  if  they  have  aged  out.  

Page 18: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

DDL  Support  •  All  DDL  is  supported  in  11.2  

•  For  complex  schema  changes  dbms_flashback  

•  disassociate_sa  and  reassociate_sa  

•  Manual  changes  can  be  made  a*er  disassociaDon  to  base  table  and  history  table  

•  ReassociaDon  can  only  occur  if  base  table  and  history  table  schema  is  the  same  

Page 19: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

•  begin      dbms_flashback_archive.disassociate_sa('SCOTT','EMP');    end;    /  

•  begin      dbms_flashback_archive.reassociate_sa('SCOTT','EMP');    end;    /  

DBMS_FLASHBACK_ARCHIVE  

Page 20: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

DML  Support  •  FBDA  supports  parallel  DML    •  DML  cannot  be  performed  on  history  table  by  a  user  

•  Except  when  it  is  disassociated  from  the  base  table  

Page 21: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Uses  for  Total  Recall  •  AudiDng  –  Provides  a  tamper  proof  historical  record  of  all  changes  

•  Row  based  recovery  –  Can  be  used  to  recover  individual  rows  by  updaDng  back  to  a  previous  value  

•  Change  data  capture  –  Fine  grained  change  capture  for  data  warehouse  extracts  

Page 22: Database & Technology 1 _ Clancy Bufton _ Flashback Query - oracle total recall and some of the uses.pdf

Cau:on  •  Seung  a  very  long  UNDO_RETENTION  in  place  of  using  flashback  archives    •  Asynchronous  FBDA  

–   changes  may  not  be  visible  to  a  flashback  archive  query  for  several  minutes  a*er  commit  

•  Global  indexes  on  history  tables  –  When  FBDA  automaDcally  maintains  the  parDDons  the  enDre  index  will  be  invalidated  and  

need  to  be  rebuilt.    

•  Don’t  use  in  11.1  –  Versions  between  semi  funcDonal  –  No  DDL  support  –  Many  bugs  –  FBDA  no  parallel  DML