Les 11 Fb Queries

37
11 Copyright © 2007, Oracle. All rights reserved. Using Flashback Technology

description

Oracle DBA Interview Questions and Answers, CBT, Oracle E-books, Certifications http://oracledba-interview.blogspot.com

Transcript of Les 11 Fb Queries

Page 1: Les 11 Fb Queries

11Copyright © 2007, Oracle. All rights reserved.

Using Flashback Technology

Page 2: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 2

Objectives

After completing this lesson, you should be able to:

• Query the recycle bin

• Restore dropped tables from the recycle bin

• Perform Flashback Query

• Use Flashback Version Query

• Use Flashback Transaction Query

• Use Flashback Transaction

Page 3: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 3

Flashback Technology

Object Level

Scenario Examples FlashbackTechnology

Depends On

AffectsData

Database Truncate table; Undesired multitable changes made

Database Flashback logs

TRUE

Table Drop table Drop Recycle bin TRUE

Update with the wrong WHERE clause

Table Undo data TRUE

Compare current data with data from the past

Query Undo data FALSE

Compare versions of a row

Version Undo data FALSE

Keep historical transaction data

Data Archive Undo data TRUE

Transaction Investigate and back out suspect transactions

Transaction Undo data TRUE

Page 4: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 4

Transactions and Undo

Undo “old” data

in undo tablespace

DML operations

Original data in

buffer cache

Page 5: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 5

Guaranteeing Undo Retention

A transaction that generatesmore undo than what there

is space for will fail.

SELECT statementsrunning 15 minutes or less

are always satisfied.

Undo data in undo

tablespace

Retention guarantee:15 minutes

Page 6: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 6

Preparing Your Database for Flashback

• Creating an undo tablespace

• Enabling Automatic Undo Management

• Specifying versus guaranteeing undo retention

Default database initialization parameters:— UNDO_MANAGEMENT='AUTO'— UNDO_TABLESPACE='UNDOTBS1'— UNDO_RETENTION=900

Page 7: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 8

Flashback Drop and the Recycle Bin

DROP TABLE employees;

FLASHBACK TABLEemployeesTO BEFORE DROP; Mistake was

made.

RECYCLEBIN=ON

Page 8: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 9

3

21

Recycle Bin

DROP TABLE employees;

BIN$zbjra9wy==$0EMPLOYEES_PK

EMPLOYEES

Recyclebin

DBA_FREE_SPACE

BIN$zbjrBdpw==$0

USER_OBJECTS

BIN$zbjrBdpw==$0 EMPLOYEESBIN$zbjra9wy==$0 EMPLOYEES_PK

4

Objects are:

– Renamed

– Not moved

Page 9: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 11

Restoring Tables from the Recycle Bin

• Restore dropped tables and dependent objects.

• If multiple recycle bin entries have the same original name:– Use unique, system-generated names to restore a particular

version– When using original names, the restored table is last in, first

out (LIFO)

• Rename the original name if that name is currently used.

FLASHBACK TABLE <table_name> TO BEFORE DROP [RENAME TO <new_name>];

Page 10: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 12

Recycle Bin: Automatic Space Reclamation

BIN$zbjrBdpw==$0 BIN$zbjra9wy==$0

BIN$zbjra9wy==$0

BIN$zbjrBdpw==$0

1

2

3

Recycle bin

DBA_FREE_SPACE - RECYCLEBIN

Autoextend

Page 11: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 13

Recycle Bin: Manual Space Reclamation

PURGE {TABLE <table_name>|INDEX <index_name>}

PURGE TABLESPACE <ts_name> [USER <user_name>]

PURGE [USER_|DBA_]RECYCLEBIN

Page 12: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 14

Bypassing the Recycle Bin

DROP TABLESPACE <ts_name> [INCLUDING CONTENTS] ;

DROP USER <user_name> [CASCADE] ;

DROP TABLE <table_name> [PURGE] ;

Page 13: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 15

Querying the Recycle Bin

SELECT owner, original_name, object_name, type, ts_name, droptime, related, spaceFROM dba_recyclebinWHERE can_undrop = 'YES';

SQL> SELECT original_name, object_name, ts_name, droptime FROM user_recyclebin WHERE can_undrop = 'YES'; 2

ORIGINAL_NAME OBJECT_NAME TS_NAM DROPTIME------------- ----------------------- ------ -------------------EMPLOYEES2 BIN$NE4Rk64w...gbpQ==$0 USERS 2007-07-02:15:45:13

SQL> SHOW RECYCLEBIN

Page 14: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 16

Querying Data fromDropped Tables

SELECT ... FROM "BIN$zbjrBdpw==$0" [AS OF ...]WHERE ...

Recyclebin

DBA_INDEXES

YES

INDEX_NAMEDROPPED

NO SALES_PK

DBA_TABLESTABLE_NAMEDROPPED

YES

NO SALES

BIN$zbjrBdpw==$0 EMPLOYEES

BIN$zbjra9wy==$0 EMPLOYEES_PK

Page 15: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 17

Using Flashback Technology to Query Data

• Flashback Query– Query all data at a specified point in time.

• Flashback Version Query– See all versions of a row between two times.– See the transactions that changed the row.

• Flashback Transaction Query– See all changes made

by a transaction.

Tx3

Tx1

Tx2

Time

Flashback

Page 16: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 18

Flashback Query

T1 T2

SELECT employee_id, salary FROM employees

AS OF TIMESTAMP <T1>

WHERE employee_id = 200

employees employees Unwantedupdates

Use to query all data at a specified point in time.

Page 17: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 19

Flashback Query: Example

11:00 11:10

UPDATE employees SET salary = (SELECT salary FROM employees

AS OF TIMESTAMP TO_TIMESTAMP ('2005-05-04 11:00:00', 'yyyy-mm-dd hh24:mi:ss') WHERE employee_id = 200)

WHERE employee_id = 200

employees employees

salary = 4,400

employees

salary = 4,400salary = 4,840

Page 18: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 20

Flashback Version Query

t1 t2

Tx1 Tx2

SELECT versions_xid, salary FROM employees

VERSIONS BETWEEN TIMESTAMP <t1> and <t2>

WHERE employee_id = 200;

Tx0

Tx0 Tx1 Tx2

employees employees employees

200

Page 19: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 21

Using Enterprise Manager to PerformFlashback Version Query

Page 20: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 22

Flashback Version Query:Considerations

• The VERSIONS clause cannot be used to query:– External tables– Temporary tables– Fixed tables– Views

• The VERSIONS clause cannot span DDL commands.

• Segment shrink operations are filtered out.

Page 21: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 23

Flashback Transaction Query

UndoSQL

FLASHBACK_TRANSACTION_QUERY

DBA

User

ErroneousDML

Page 22: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 24

Using Enterprise Manager to PerformFlashback Transaction Query

Page 23: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 25

Flashback Transaction Query:Considerations

• DDL commands are seen as dictionary updates.

• Flashback Transaction Query on a transaction underlying a DDL command displays the data dictionary changes.

• Dropped objects appear as object numbers.

• Dropped users appear as user identifiers.

Page 24: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 26

Flashback Transaction

• Setting up Flashback Transaction prerequisites

• Stepping through a possible workflow

• Using the Flashback Transaction Wizard

• Querying transactions with and without dependencies

• Choosing back-out options and flashing back transactions

• Reviewing the results

Page 25: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 27

Prerequisites

Page 26: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 28

Flashing Back a Transaction

• You can flash back a transaction with Enterprise Manager or from the command line.

• EM uses the Flashback Transaction Wizard, which calls the DBMS_FLASHBACK.TRANSACTION_BACKOUT procedure with the NOCASCADE option.

• If the PL/SQL call finishes successfully, it means that the transaction does not have any dependencies and a single transaction is backed out successfully.

Page 27: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 29

Possible Workflow

1. Viewing data in a table

2. Discovering a logical problem

3. Using Flashback Transaction1. Performing a query

2. Selecting a transaction

3. Flashing back a transaction (with no conflicts)

4. Choosing other back-out options (if conflicts exists)

4. Reviewing Flashback Transaction results

Page 28: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 30

Viewing Data

Page 29: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 31

Flashback Transaction Wizard

Page 30: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 32

Flashback Transaction Wizard

Page 31: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 33

Flashback Transaction Wizard

2

1

Page 32: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 34

Flashback Transaction Wizard

Page 33: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 35

Choosing Other Back-out Options

Page 34: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 36

Choosing Other Back-out Options

Page 35: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 37

Final Steps Without EM

After choosing your back-out option, the dependency report is generated in the DBA_FLASHBACK_TXN_STATE and DBA_FLASHBACK_TXN_REPORT tables.

• Review the dependency report that shows all transactions which were backed out.

• Commit the changes to make them permanent.

• Roll back to discard the changes.

Page 36: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 38

Summary

In this lesson, you should have learned how to:

• Query the recycle bin

• Restore dropped tables from the recycle bin

• Perform Flashback Query

• Use Flashback Version Query

• Use Flashback Transaction Query

• Use Flashback Transaction

Page 37: Les 11 Fb Queries

Copyright © 2007, Oracle. All rights reserved.11 - 39

Practice 11 Overview: Performing Flashback Database

This practice covers the following topics:

• Recycle bin activities

• Flashback Query

• Flashback Transaction