Wednesday, June 13, 2018

How to do Flashback Database in oracle 12c active data guard environment

Here are the easy steps-


There was a requirement to run stress testing (provably it may run 24 hours) in production active data guard database environment (DB version - Enterprise Edition Release 12.1.0.2.0 – 64bit). 

Stress testing will create lots of testing data and after that they want to rollback all testing transactions.  Means they want to go back to the state where it was before stress testing start. 

So for that, I recommended to use flashback Database over point-in-time recovery. because it is very easy ,quick and with less impact on database availability. 

I suggested following two step by step Methods as per their environment.

you guys can use any of them. both worked fine for me.

Two methods are below-  

Method #1:   
May use if Data guard’s  protechtion mode is -  MAXIMUM PERFORMANCE.
else use Method #2 .

So in this case, we can plan to shutdown standby server before stress testing and start after flashback database on primary database.  Steps are below-

   1)      Confirming flashback enabled on both primary and standby databases.

SQL> select database_role,flashback_on from v$database;

   2)   Just before stress testing –  Cancel Managed recovery on standby server and shutdown    standby server.


     SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; 

          3)   Create a guaranteed restore point in primary.
           
              SQL> CREATE RESTORE POINT before_stress_test_pr GUARANTEE FLASHBACK DATABASE;
 


          4) After stress testing ,
 On primary –     
                      SQL>   SHUTDOWN IMMEDIATE  ;
                       SQL> STARTUP MOUNT ;
                         SQL>  FLASHBACK DATABASE TO RESTORE POINT before_stress_test_pr;
                         SQL> ALTER DATABASE OPEN RESETLOGS ;
 
         Checking SCN number in primary –(just for confirmation it is greater that standby by SCN number later) 
 
                 SQL>  SELECT CURRENT_SCN FROM V$DATABASE;
 
             5)    Start the standby server .
6)      Start Managed recovery on the standby –
 
                 SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;

             7)     Drop guaranteed restore point from primary.

             SQL> DROP RESTORE POINT before_stress_test_pr;

               
Method #2
This one can be used any situation.

1)      Confirming flashback enabled on both primary and standby databases.

SQL> select database_role,flashback_on from v$database;

2)      Create a guaranteed restore point in standby  ( just before stress testing start)

CREATE RESTORE POINT before_stress_test_st GUARANTEE FLASHBACK DATABASE;

3)      After few minutes, create a guaranteed restore point in primary (just before stress testing start)

CREATE RESTORE POINT before_stress_test_pr GUARANTEE FLASHBACK DATABASE;

 After stress testing ,
4)       On primary –     
           SQL>   SHUTDOWN IMMEDIATE  ;
           SQL> STARTUP MOUNT ;
                         SQL>  FLASHBACK DATABASE TO RESTORE POINT before_stress_test_pr;
                         SQL> ALTER DATABASE OPEN RESETLOGS ;
 
  Checking SCN number in primary –(just for confirmation it is greater that standby by SCN number later) 
 
             SQL>  SELECT CURRENT_SCN FROM V$DATABASE;
 
5)      On Standby – 
     SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE CANCEL; 
 
Checking SCN number in standby – for confirmation it is lesser than primary SCN number.
 
     SQL> SELECT CURRENT_SCN FROM V$DATABASE;
 
     SQL> FLASHBACK STANDBY DATABASE TO RESTORE POINT before_stress_test_st  ;
 
6)       Start Managed recovery on the standby –
 
SQL> ALTER DATABASE RECOVER MANAGED STANDBY DATABASE DISCONNECT FROM SESSION;

7)      Drop both guaranteed restore point.




More Usability of Flashback Database-

Typically, in following situations you can use Flashback Database. 

  • A user error or corruption removes needed data or introduces corrupted data. For example, a user or DBA might erroneously delete or update the contents of one or more tables, drop database objects that are still needed during an update to an application, or run a large batch update that fails midway.
  • A database upgrade fails or an upgrade script goes awry.
  • A complete database recovery after a media failure cannot succeed because you do not have all of the needed redo logs or incremental backups.


A Solution for Removing duplicate records from parent table and update child tables accordingly in oracle

Think a requirement from your manager like below -


"We are trying to clean up the ORDERs table. We want to remove the duplicates records in this table with the same PRODUCT_ID, EFFECTIVE_STATE_DATE combination which are active (RECORD_STATUS=1, DEACTIVE_DATE is null and EFFECTIVE_END_DATE is null).

We only want to retain one record of such combination (oldest ORDER.PRODUCT_ID) and mark others as deleted (RECORD_STATUS=99).

When we mark any row as deleted, we need to go to the ORDER_DETAILS table and
Check if there’s any record with that PRODUCT_ID which is going to be marked as deleted. If there’s any record, that record in ORDER_DETAILS table needs to get updated with the retained PRODUCT_ID for that combination."

Solutions- 

You can use this as a template to solve this type of requirement.  This is not a real time working script. I modified the working script with sample tables (ORDERS AND ORDER_DETAILS) . please change your tables name and columns name as necessary and fixed the script if needed.

We need to go through few steps for this.

1)     Creating a temp table WITH RANK () - First TEMP table



create table TEMP_TABLE_WITH_RANK as
SELECT  order_id,product_id,effective_start_date,record_status,
       RANK ()
       OVER (PARTITION BY product_id,effective_start_date
             ORDER BY product_id)   rnk  ---------------RANKING them in this combination  --we will keep only  rnk=1 rest of the records need to marked deleted.
  FROM ORDERS
WHERE (product_id,effective_start_date) in (   
        select product_id,effective_start_date ----------Fetching all records including all duplicates and original
                      from ( select product_id,effective_start_date, count(*)  ------Finding dulicate orders in this combination
                                from ORDERS t
                                where t.RECORD_STATUS=1
                                    and t.DEACTIVE_DATE is null
                                    and t.effective_end_date is null
                                group by product_id,effective_start_date
                                having count(*) >1 )) ;
                                                                             
                                                                             
                                                                             


2)  Creating second temp table - Summary table with self joining to make a relation with the DELETING order_id and KEEPING order id.                       


                              

CREATE TABLE temp_table_summary                 
AS
select a.product_id,a.effective_start_date, b.order_id need_to_changed_order_id , a.order_id keeping_order_id
from  TEMP_TABLE_WITH_RANK a, TEMP_TABLE_WITH_RANK b
where a.rnk=1
and b.rnk<> 1
and a.product_id = b.product_id
and a.effective_start_date = b.effective_start_date ;





3)  ORDER_DETAILS  Update statement -First update statement

    --using second temp table - temp_table_summary

   

UPDATE order_details a 
   SET ORDER_ID =
          (SELECT keeping_order_id
             FROM temp_table_summary    b
            WHERE b.need_to_changed_order_id = a.ORDER_ID)
WHERE EXISTS
       (SELECT keeping_order_id
             FROM temp_table_summary    b
            WHERE b.need_to_changed_order_id = a.ORDER_ID) ;
           

4) ORDERS table update statement --Second update statement

  --using first temp table        



update ORDERS
set record_status=99 --deleted
where order_id in (select order_id from TEMP_TABLE_WITH_RANK where rnk<>1) ;



Tuesday, March 20, 2018

Accent Insensitive search in Oracle

Here is way to search non english characters in Oracle :  Please see the example demo below -

Here I have created a table "accent" with few accent and non-accent data. then tried to search - 

 CREATE TABLE accent (name VARCHAR2 (200));  
 
  INSERT INTO accent  
    VALUES ('Peña Báináõ');  
 
  INSERT INTO accent  
    VALUES ('Pena Bainao');  
 
  INSERT INTO accent  
    VALUES ('Pena Bainaoooo');  
 
  INSERT INTO accent  
    VALUES ('Penaffff Bainao');  
 
  COMMIT;  
 
 
  SELECT UPPER (  
      UTL_RAW.cast_to_varchar2 (NLSSORT (name, 'nls_sort=binary_ai')))  
      name  
  FROM accent;  
 
 
 
  SELECT *  
  FROM (SELECT UPPER (  
          UTL_RAW.cast_to_varchar2 (  
            NLSSORT (name, 'nls_sort=binary_ai')))  
          name  
      FROM accent)  
  WHERE UPPER (TRIM (name)) LIKE UPPER ('PENA BAINAO%');  
 
 
   --example from dual table  
 
  SELECT 'Peña Báináõ' original,  
     UTL_RAW.cast_to_varchar2 (  
      NLSSORT ('Peña Báináõ', 'nls_sort=binary_ai'))  
      converted_like_english  
  FROM DUAL;  


Links -
https://community.oracle.com/thread/2400229
https://stackoverflow.com/questions/6682173/oracle-search-text-with-non-english-characters