DavidYahalom.com is an IT knowledgebase dedicated to the world of databses and RDBMS systems by David Yahalom. Here you'll find articles, views, news, tips and in-depth analysis about Oracle, DB2 LUW, Sql Server and MySql. I hope you'll enjoy your stay.
28th
FEB
Using DBMS_FLASHBACK in Oracle to set your entire session back in time
Posted by David Yahalom under Oracle
Last week I’ve talked to you about using Flashback query, Flashback table and FlashBack database in Oracle 10g to help you improve productivity and perhaps save you time in doing complete or partial database recovery.
Today I want to talk to you about a less known feature of Flashback, the DBMS_FLASHBACK package available since Oracle 9iR1.
The DBMS_FLASHBACK package provides almost the same functionality as FlashBack query but with one big difference. Instead of being able to “go back in time” one query at a time, the DBMS_FLASHBACK package acts as a time machine, or a time tunnel, allowing you to “turn back the clock” and carry out normal SQL queries or PL/SQL code, without change, but as if you were running them from a time in the past.
Flashback query in Oracle 9iR2 (using the as of clause in your SQL statement) is actually based and internally using the capabilities of DBMS_FLASHBACK package.
To use DBMS_FLASHBACK you simply call DBMS_FLASHBACK.ENABLE_AT_TIME (and specify a specific timestamp in the database life cycle) or DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER (and specify an SCN number in the database life cycle) to “set back the clock” of your current session as a whole.
Following that you can run you queries and PL/SQL code normally without any modifications but instead of getting the results you normally would (which represent to current state of the data in your database) the results you receive represent the state of the database as it was at the timestamp you provided with the DBMS_FLASH.ENABLE_AT_TIME command.
To return to the “present” simply cancel the “time tunnel” using the DFBMS_FLASHBACK.DISABLE command.
Let’s write an example that will show us just how easy it is to use the DBMS_FLASHBACK package.
SQL> conn admin/*********@daviddb
Connected.
SQL> create table employees (ename char(20), sal number(6));
Table created.
SQL> insert into employees values ('Linus Torvalds', 5000);
1 row created.
SQL> insert into employees values ('Tom Kyte', 4000);
1 row created.
SQL> insert into employees values ('Steve Ballmer', 9000);
1 row created.
SQL> commit;
Commit complete.
SQL> select * from employees;
ENAME SAL
-------------------- ----------
Linus Torvalds 5000
Tom Kyte 4000
Steve Ballmer 9000
This is our base table of employees. At this time the table holds "good" data.
SQL> var sysscn number
SQL> EXEC :sysscn:=DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER
PL/SQL procedure successfully completed.
I’m creating a sysscn variable that I will use to store the current SCN of the system. In this example, using a system SCN number as the reference to which we will later Flashback to is easier to demonstrate than using a timestamp.
SQL> update employees set sal = 9999; 3 rows updated. SQL> commit; Commit complete. SQL> select * From employees; ENAME SAL -------------------- ---------- Linus Torvalds 9999 Tom Kyte 9999 Steve Ballmer 9999
An employee from the HR department has accidentally updated all employee salaries in the company to 9999. When he notices this mistake he notifies the DBA immediately that something is wrong with employee salaries. Normally, this would require us to recover that table from a backup. We can avoid this by using the DBMS_FLASHBACK package.
SQL> EXEC DBMS_FLASHBACK.ENABLE_AT_SYSTEM_CHANGE_NUMBER(:sysscn); PL/SQL procedure successfully completed. SQL> select * from employees; ENAME SAL -------------------- ---------- Linus Torvalds 5000 Tom Kyte 4000 Steve Ballmer 9000 SQL> exec dbms_flashback.disable PL/SQL procedure successfully completed.
Remember that while you are in a “time tunnel” you cannot use DML or DDL statements and that you cannot open a “time tunnel” from within a “time tunnel”. That is, you must use
Also keep in mind that you must have the execute privilege on the DBMS_FLASHBACK package in order to use this wonderful feature.
Using DBMS_FLASHBACK will sometimes make your life easier than using a regular Flashback query. Because it sets your entire session “back in time” you can run multiple database-wide queries or execute PL/SQL code freely from your session without restriction or the need to re-write existing code with the as of clause.
18th
FEB
Oracle 10g FlashBack Database
Posted by David Yahalom under Oracle
A few days ago I ran into a problem with one of our development DB2 servers. A programmer in my team accidentally activated a Java Hibernate class with an option that truncates tables before loading them into memory. This resulted in most of our tables becoming empty (and I’m taking about something like 300 tables here).
I had to do a tape restore in order for us to get our database running again. This wasn’t hard, but was a hassle nonetheless since I had to mount the tape, restore the image with the backup software and then do a restore and rollforward of the database. This is a manual process that must be done by the DBA.
During my late night restore session I wished it was an Oracle server I was working on. I remember a few months ago I had a smiler situation with an Oracle 10g server we had but that time I was able to use the “Flashback database” feature to restore my db to a working state in less time with allot fewer hassle.
Not many DBAs utilize Oracle’s 10g new “Flashback Database” feature which is a shame. Maybe its because we were all brought up on Oracle during simpler days when Flashbacking an entire database was only a dream.
Since Oracle 9i we have a “flashback query” option which allows us to restore the state of tables to an X minutes ago timestamp. This ability is based on the UNDO Tablespace data retention mechanism. When you modify a table, given a large enough UNDO tablespace, Oracle keeps copies of the old table blocks in its UNDO tablespace.
Since Oracle 10g we also have something new called “Flashback Database”. This allows us to rollback the database, either in full or in parts, back to a specific point in time. This is most useful when you run into a problem and want a quick way to restore your database to a previous “snapshot” without the hassle and time of doing a tape restore.
This feature utilizes 10Gs new “Flashback Area” which stores changes to the database in files dedicated solely for this purpose.
Doing a database flashback is actually simpler than most DBAs believe.
Let me give you a quick example:
1) First, lets put the database in Flashback mode:
SQL> shutdown immediate; Database closed. Database dismounted. Oracle Instance shut down. SQL> startup mount; ORACLE instance started. SQL> alter database archivelog; Database altered. SQL> alter system set DB_FLASHBACK_RETENTION_TARGET=9600; System altered. SQL> alter system set DB_RECOVERY_FILE_DEST_SIZE=1073741824; System altered. SQL> alter system set DB_RECOVERY_FILE_DEST='/storage/flashback/'; System altered. SQL> alter database open; Database altered.
Note that this will only allow you to flashback your database to a point-in-time which is from now and forward.
To test flashbacking the db, let’s create a small test table:
SQL> create table hello_world (emp_id number(4), emp_name varchar2(30)); Table created. SQL> insert into hello_world values (1, 'Bill Gates'); 1 row created. SQL> insert into hello_world values (2, 'Steve Jobs'); 1 row created. SQL> insert into hello_world values (3, 'Chris Eaton'); 1 row created. SQL> commit; Commit complete;
Now lets get to the interesting stuff. Lets DROP THE TABLE and restore it from the flashback area:
SQL> drop table hello_world; Table dropped. SQL> flashback table "HELLO_WORLD" to before drop; Flashback complete;
Pretty cool, right?
But this isn’t everything… Lets try to push the “Flashback Database” feature to its limits and try restoring the ENTIRE DATABASE to an earlier point in time.
SQL> shutdown immediate;
Database closed.
Database dismounted.
ORACLE instance shut down.
SQL> startup mount exclusive;
ORACLE instance started.
Database mounted.
SQL> FLASHBACK DATABASE to timestamp to_timestamp('01-01-2007 10:00', 'DD-MM-YYYY HH24:MI:SS');
Flashback complete.
SQL> alter database open resetlogs;
13th
FEB
Great, simple and free db2 monitor software
Posted by David Yahalom under DB2 LUW
Today I have stumbled upon this great little free tool called “DB2 Monitor”.
This is a fast windows GUI application for performance monitoring. Shows active connections with lock information, SQLs in db cache, database snapshots, database hit ratios, database objects information, list history. Can help resolve locking problems and identify worst performing queries.
Its fast, has a very simple user interface and just works every time! (something I can’t really say about most of IBMs JAVA monitoring applications for DB2).
Recommended.
You can get DB2 Monitor from here.
12th
FEB
Update all database statistics with a single command
Posted by David Yahalom under DB2 LUW
As with all RDBMS implementation, DB2 UDB uses the statistics information in the catalog table to derive the best execution plan. We, as DBAs, should regularly run the RUNSTATS command to keep our database statistics updated for optimal query performance.
RUNSTATS ON TABLE SCHEMA_NAME.TABLE_NAME
Runstats works as advertised but what happen when you need to quickly update statistics for a large group of tables at once? Allot of DBAs (including myself) would write a script that creates another script, a “RUNSTATS ON TABLE_NAME” script, with the table name concated. This will, of course, work, but there is a simpler and better way to do this in DB2 using the reorgchk command.
To update stats for the entire database use:
REORGCHK UPDATE STATISTICS on TABLE ALL
To update all the tables of a particular schema use:
REORGCHK UPDATE STATISTICS on SCHEMA schema_name
You should note, however, that the RUNSTATS command generated by the REORGCHK UPDATES STATISTICS command collects statistic on the table only without distribution. To have distributed statistics on your tables you should use the RUNSTATS command instead.
Category Cloud
ASM DB2 LUW ETL General IT Grid Control Hardware ITIL Linux Monitoring MySQL Oracle RAC Security Solaris SQL Server Storage Uncategorized Unix Windows
Recent Posts
- What will happen to ASM when the disk path changes in Linux?
- The relation between an Oracle instance and memory in Windows
- Blog has been renamed!
- Cloud Oracle Storage: how to make ASM even better, the NAS way.
- ORA-600: Oracle process has no purpose in life!
Active polls
Categories
- ASM (2)
- DB2 LUW (11)
- ETL (2)
- General IT (7)
- Grid Control (1)
- Hardware (4)
- ITIL (1)
- Linux (3)
- Monitoring (1)
- MySQL (1)
- Oracle (32)
- RAC (5)
- Security (3)
- Solaris (3)
- SQL Server (1)
- Storage (1)
- Uncategorized (1)
- Unix (2)
- Windows (2)
Archives
- September 2009
- February 2009
- January 2009
- November 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- October 2007
- September 2007
- July 2007
- June 2007
- April 2007
- March 2007
- February 2007
- January 2007
- March 2006
Blogroll
DavidYahalom.com - Oracle, Databases, SQL, news, views, articles and in-depth analysis is powered by Wordpress. Designed by Free WordPress Themes.
