Tuesday, June 9, 2015

Create Baseline to change execution plan hash value

Issue:

When a query is having a new plan which is worst and you found by SQL tuning Advisor there is another plan which is having better execution time.














       

There is a better plan in AWR as in above Fig.

Steps to change plan hash value.

Step1:  Create SQL tuning set with SQL_ID attached.

Drop if same name exists before
 exec dbms_sqltune.DROP_SQLSET('sqlid_1vktnmmbp6cz3');

Create new one as follows.


 exec DBMS_SQLTUNE.CREATE_SQLSET('sqlid_1vktnmmbp6cz3');


Step2: Load plan  you want for the SQL ID , replace sql_id and plan hash value you want in following blocks.

Following will extract the plan from awr.

declare
 baseline_ref_cursor DBMS_SQLTUNE.SQLSET_CURSOR;
 begin
 open baseline_ref_cursor for
 select VALUE(p) from table(DBMS_SQLTUNE.SELECT_WORKLOAD_REPOSITORY(1, 128,'sql_id='||CHR(39)||'1vktnmmbp6cz3'||CHR(39)||' and plan_hash_value=487911821',NULL,NULL,NULL,NULL,NULL,NULL,'ALL')) p;
 DBMS_SQLTUNE.LOAD_SQLSET('sqlid_1vktnmmbp6cz3', baseline_ref_cursor);
 end;
 /
 above when executed will throw out correct snapshot ids as follows

 08:35:15 SQL> declare
 08:35:34   2   baseline_ref_cursor DBMS_SQLTUNE.SQLSET_CURSOR;
 08:35:34   3   begin
 08:35:34   4   open baseline_ref_cursor for
 08:35:34   5   select VALUE(p) from table(DBMS_SQLTUNE.SELECT_WORKLOAD_REPOSITORY(1, 128,'sql_id='||CHR(39)||'1vktnmmbp6cz3'||CHR(39)||' and plan_hash_value=487911821',NULL,NULL,NULL,NULL,NULL,NULL,'ALL')) p;
 08:35:34   6   DBMS_SQLTUNE.LOAD_SQLSET('sqlid_1vktnmmbp6cz3', baseline_ref_cursor);
 08:35:34   7   end;
 08:35:34   8   /
 declare
 *
 ERROR at line 1:
 ORA-13768: Snapshot ID must be between 83431 and 93052.
 ORA-06512: at "SYS.DBMS_SQLTUNE", line 4715
 ORA-06512: at line 6

Now replace start and end snap ids as follows

declare
 baseline_ref_cursor DBMS_SQLTUNE.SQLSET_CURSOR;
 begin
 open baseline_ref_cursor for
 select VALUE(p) from table(DBMS_SQLTUNE.SELECT_WORKLOAD_REPOSITORY(83431, 93052,'sql_id='||CHR(39)||'1vktnmmbp6cz3'||CHR(39)||' and plan_hash_value=487911821',NULL,NULL,NULL,NULL,NULL,NULL,'ALL')) p;
 DBMS_SQLTUNE.LOAD_SQLSET('sqlid_1vktnmmbp6cz3', baseline_ref_cursor);
 end;
 /

 


 ***************** OR ***********************

Following to get plan from cursor cache.


 declare
  baseline_ref_cursor DBMS_SQLTUNE.SQLSET_CURSOR;
  begin
  open baseline_ref_cursor for
  select value(p) from table(dbms_sqltune.select_cursor_cache('sql_id=''1vktnmmbp6cz3'' and plan_hash_value=487911821',NULL,NULL,NULL,NULL,1,NULL,'ALL')) p;
  DBMS_SQLTUNE.LOAD_SQLSET('sqlid_1vktnmmbp6cz3', baseline_ref_cursor);
  end;
 /

Step3: Queries to check plan.

SELECT NAME,OWNER,CREATED,STATEMENT_COUNT FROM DBA_SQLSET where name='sqlid_1vktnmmbp6cz3';

 select sql_id, substr(sql_text,1, 15) text
 from dba_sqlset_statements
 where sqlset_name = 'sqlid_1vktnmmbp6cz3'
 order by sql_id;

 SELECT * FROM table (DBMS_XPLAN.DISPLAY_SQLSET('sqlid_1vktnmmbp6cz3','1vktnmmbp6cz3'));

select *  from dba_sql_plan_baselines;

 Step4: Create baseline and fix it.


 set serveroutput on
 declare
 my_integer pls_integer;
 begin
 my_integer := dbms_spm.load_plans_from_sqlset(sqlset_name => 'sqlid_1vktnmmbp6cz3',
                                               sqlset_owner => 'SYSTEM',
                                               fixed => 'YES',
                                               enabled => 'YES');
                                               DBMS_OUTPUT.PUT_line(my_integer);
 end;
 /





Wednesday, April 15, 2015

Copy files from ASM diskgroup to remote ASM diskgroup.




ASMCMD> cp  +DATA_XDB4/SRCDB/PARAMETERFILE/spfile.3151.869812217  sys@10.112.39.14.+ASM2:+RECO_XDB5/TGTDB/test/spfile_sk.ora  --port 2483
Enter password: *******

10.112.39.14=remote host IP make sure this is the IP where +ASM2 listener listening to.
+ASM2= remote ASM instance

--port 2483 = port number




To debug
export DBI_TRACE=1

then

ASMCMD> cp  +DATA_XDB4/SRCDB/PARAMETERFILE/spfile.3151.869812217  sys@10.112.39.14.+ASM2:+RECO_XDB5/TGTDB/test/spfile_sk.ora  --port 2483
Enter password: *******

Tuesday, March 3, 2015



Shell script to read list of databases from a file.

while read line;
do
DBNAME=`echo $line |awk {'print $1'}`
TBS_THRESHOLD=`echo $line |awk {'print $2'}`
DBA=`echo $line |awk {'print $3'}`
RMANCAT=`echo $line |awk {'print $4'}`
INSTANCES=`echo $line |awk {'print $5'}`
. /orahome/work/sk/dbmon.sh $DBNAME
. /orahome/work/sk/tbsmon.sh $DBNAME $TBS_THRESHOLD
. /orahome/work/sk/dbimon.sh $DBNAME
done < /orahome/work/sk/Listofdatabases.txt

Above script will execute following for every db listed in /orahome/work/sk/Listofdatabases.txt

. /orahome/work/sk/dbmon.sh $DBNAME
. /orahome/work/sk/tbsmon.sh $DBNAME $TBS_THRESHOLD
. /orahome/work/sk/dbimon.sh $DBNAME

 <@dbax1:/orahome/work/sk>cat Listofdatabases.txt
db1 15 SK rmandbq 2
db2 15 SK rmandbq 2
db3 15 SK rmandbq 2
db4 15 SK rmandbq 2

db1,db2..are database names in tnsnames.ora file.


Wednesday, February 11, 2015

RMAN restore of few tablespaces in DEV from backup of Prod.

Create a dummy temporary instance DEVDB and restore the required tablespaces skipping all other tablespaces not required(DO NOT skip system,SYSAUX,TEMP,UNDO) during restore it will check dependencies of tablespace.

copy init parameter from existing DEVDB1 and create DEVDB dummy instance and change parameters accordingly to the required locations.

*.DB_FILE_NAME_CONVERT='/oradata/PRODDB/','/orabackup/DEVDB'
*.LOG_FILE_NAME_CONVERT='/oralogs/PRODDB/','/oralogs/DEVDB/'
*.db_create_file_dest='/orabackup/DEVDB/'
*.DB_CREATE_ONLINE_LOG_DEST_1='/orabackup/DEVDB/'
*.DB_CREATE_ONLINE_LOG_DEST_2='/orabackup/DEVDB/'
 *.control_files='/orabackup/DEVDB/control01.ctl','/orabackup/DEVDB/control02.ctl'
*.db_name='DEVDB'

Add tns entries in dev server for  rmancatalogdb,DEVDB,productionDB
Add listener.ora entries for dummy DB and reload.

SID_LIST_LISTENER =
  (SID_LIST =
      (SID_DESC =
      (GLOBAL_DBNAME = DEVDB)
      (ORACLE_HOME = /orahome/oracle/product/11.2.0.3/dbhome_1)
      (SID_NAME = DEVDB)
    )
  )


This restore is from BACKUP_HOST=DDBOOSTSERVER



DEVDB is a dummy temp instance

$sqlplus "/ as sysdba"
SQL> startup nomount
  
$rman log=rmanout.log

connect catalog rman@rmancatalogdb
connect auxiliary sys@DEVDB
connect target sys@productionDB
run
    {
    set UNTIL TIME "to_date('02/06/2015 06:18:00','MM/DD/YYYY HH24:MI:SS')";
    allocate AUXILIARY CHANNEL c1 DEVICE TYPE 'SBT_TAPE' connect sys@DEVDB  PARMS 'BLKSIZE=1048576,SBT_LIBRARY=/orahome/oracle/product/11.2.0.3/dbhome_1/lib/libddobk.so,ENV=(STORAGE_UNIT=Oracle_Production,BACKUP_HOST=DDBOOSTSERVER,ORACLE_HOME=/orahome/oracle/product/11.2.0.3/dbhome_1)';
    allocate AUXILIARY CHANNEL c2 DEVICE TYPE 'SBT_TAPE' connect sys@DEVDB  PARMS 'BLKSIZE=1048576,SBT_LIBRARY=/orahome/oracle/product/11.2.0.3/dbhome_1/lib/libddobk.so,ENV=(STORAGE_UNIT=Oracle_Production,BACKUP_HOST=DDBOOSTSERVER,ORACLE_HOME=/orahome/oracle/product/11.2.0.3/dbhome_1)';
    allocate AUXILIARY CHANNEL c3 DEVICE TYPE 'SBT_TAPE' connect sys@DEVDB  PARMS 'BLKSIZE=1048576,SBT_LIBRARY=/orahome/oracle/product/11.2.0.3/dbhome_1/lib/libddobk.so,ENV=(STORAGE_UNIT=Oracle_Production,BACKUP_HOST=DDBOOSTSERVER,ORACLE_HOME=/orahome/oracle/product/11.2.0.3/dbhome_1)';
    allocate AUXILIARY CHANNEL c4 DEVICE TYPE 'SBT_TAPE' connect sys@DEVDB  PARMS 'BLKSIZE=1048576,SBT_LIBRARY=/orahome/oracle/product/11.2.0.3/dbhome_1/lib/libddobk.so,ENV=(STORAGE_UNIT=Oracle_Production,BACKUP_HOST=DDBOOSTSERVER,ORACLE_HOME=/orahome/oracle/product/11.2.0.3/dbhome_1)';
DUPLICATE TARGET DATABASE TO DEVDB skip tablespace     GSGCAPPBKP_DATA,GSGCAPPBKP_INDX,GSGCPUB_DATA,GSGCPUB_INDX,GSIRAPP_DATA,GSIRAPP_INDX,USERS;
}


If this is from DISK then use following.


connect catalog rman@rmancatalogdb
connect auxiliary sys@DEVDB
connect target sys@productionDB
run
    {
    set UNTIL TIME "to_date('02/06/2015 06:18:00','MM/DD/YYYY HH24:MI:SS')";
    allocate AUXILIARY CHANNEL c1 DEVICE TYPE DISK;
    allocate AUXILIARY CHANNEL c2 DEVICE TYPE DISK;
    allocate AUXILIARY CHANNEL c3 DEVICE TYPE DISK;
    allocate AUXILIARY CHANNEL c4 DEVICE TYPE DISK;
DUPLICATE TARGET DATABASE TO DEVDB skip tablespace     GSGCAPPBKP_DATA,GSGCAPPBKP_INDX,GSGCPUB_DATA,GSGCPUB_INDX,GSIRAPP_DATA,GSIRAPP_INDX,USERS;
}


then export the schema from dummy temp DEVDB and import into DEVDB1

Key command:

DUPLICATE TARGET DATABASE TO DEVDB skip tablespace     GSGCAPPBKP_DATA,GSGCAPPBKP_INDX,GSGCPUB_DATA,GSGCPUB_INDX,GSIRAPP_DATA,GSIRAPP_INDX,USERS;


Thursday, December 11, 2014

Oracle Database restore from backup using Duplicate RMAN.

Assumptions

There is a full backup level0 with control file and spfile in +DATA/ocdb/rmandata, it can be any location not necessarily ASM

Restoring backup onto database ocdb, db_name=ocdb
Backup belongs to OCDB database.
Incarnation is set to correct one.

STEP1

Startup new instance with following



db_name=ocdb
CONTROL_FILES='+DATA','+RECO'
db_create_file_dest='+DATA'
DB_CREATE_ONLINE_LOG_DEST_1='+RECO'
DB_CREATE_ONLINE_LOG_DEST_2='+RECO'
DB_RECOVERY_FILE_DEST='+RECO'


startup nomount pfile='/tmp/init.ora'




STEP2

Run following to restore database untill time from old backup.

connect auxiliary  /
run
 {
 ALLOCATE AUXILIARY CHANNEL ch1 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch2 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch3 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch4 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch5 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch6 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch7 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch8 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch9 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch10 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch11 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch12 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch13 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch14 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch15 DEVICE TYPE disk;
 ALLOCATE AUXILIARY CHANNEL ch16 DEVICE TYPE disk;
 DUPLICATE DATABASE TO ocdb
 BACKUP LOCATION '+DATA/ocdb/rmandata'
  until time "to_date ('15-10-2014 21:00:00', 'DD-MM-YYYY HH24:MI:SS')"
  NOFILENAMECHECK;
}

Active Duplicate using RMAN.

Two big disadvantages of the ACTIVE database duplication method are:

    Negative performance impact on the source database.
    High network traffic on the connection between the source and target databases.



STEP1

On TARGET database server.

change following parameters in init parameter file specific to target database

alter system set db_name=targetdb scope=spfile;
alter system set cluster_database=false scope=spfile;
alter system set db_create_file_dest='+DATA';
alter system set db_create_online_log_dest_1='+REDO';
alter system set db_create_online_log_dest_2='+REDO';


startup nomount pfile='/tmp/init.ora'

STEP2

On TARGET database server,change listener.ora make sure ORACLE_HOME is there in the file.

SID_LIST_LISTENER =
  (SID_LIST =
    )
    (SID_DESC =
      (SID_NAME = targetdb1)
      (ORACLE_HOME = /apps/oracle/product/11.2.0/db_1)
      (GLOBAL_DBNAME = targetdb)
    )
  )

and do lsnrctl status, lsnrctl reload


STEP3

Add following entries to both tnsnames.ora files on both SOURCE and TARGET

SOURCE =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = sourceserv)(PORT = 1521))
      (CONNECT_DATA =
        (SERVER = DEDICATED)
        (SERVICE_NAME = source)
      )
    )
  
  target =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = targetserv)(PORT = 1521))
      (CONNECT_DATA =
        (SERVER = DEDICATED)
        (SERVICE_NAME = target)
      )
  )
 
   
 
STEP4

Create password file on both SOURCE and TARGET
If the password file already exists on source just copy it to TARGET $ORACLE_HOME/dbs


STEP5

Test connectivity to both SOURCE and TARGET from both servers using TNS entries created in STEP3 and
make sure it works


sqlplus sys/sys@target as sysdba
sqlplus sys/sys@source as sysdba



STEP6

On Auxillary host(TARGET) start RMAN and run DUPLICATE

Here very important auxillary connection is TARGET and connection target is SOURCE



$export ORACLE_SID=HRPRD1

$rman target sys/sys@SOURCE auxiliary sys/sys@TARGET
RMAN>run{
         DUPLICATE target DATABASE TO TARGET------> (the first target is the connection target and the second is db name of target database.)
         FROM ACTIVE DATABASE;
        }

Wednesday, December 3, 2014

View data in a datafile.

SQL> create tablespace testts datafile '/tmp/testts_01.dbf' size 1M;

SQL> create table mythbuster1 (col1 varchar2(200)) tablespace testts;


Insert a row:


SQL> insert into mythbuster1 values (‘ORIGINAL_VALUE’);
SQL> Commit; 
 
 
 
$ strings /tmp/testts_01.dbf
}|{z
-N?pD112D2
TESTTS
 1j)
 w>!
ORIGINAL_VALUE
 
 
 
Wait event.
 
The server process then identifies the block the row exists in. After 
the database instance just came up the buffer cache is empty and the 
block will not be found. Therefore the server process issues a read call
 from the datafile for that specific block. The block is read from the 
disk to the buffer cache.
Until the loading of the block from the disk 
to the buffer cache is complete, 
the session waits with the event – db file scattered read
 
 
Two Task Architecture 
 
 There are two tasks – the 
user task that a regular user has written and the server task that 
performs the database operations. This is an important concept 
established during the early foundations of the Oracle database to 
protect the database from errant code in the user task introduced either
 maliciously or inadvertently.
 
usertask=user processes=Java program, a Pro*C code, SQL*Plus process
server task=server processes=oracleinstance