Wednesday, February 1, 2012

backup of encrypted password for oracle databases users , spooling password of database users


Below script can be used to take a backup of encrypted password for oracle  databases users 

sqlplus "/as sysdba "
@/opt/generate_pass_file.txt
-------------------------------------------------------------------------------------------------------------
vi /opt/generate_pass_file.txt ( copy below conetent into generate_pass_file.txt)
-- Use below  script to spool encrypated password  for entire users of the database  and can be reverted later to reset to original password
set pagesize 0
set heading off
set feedback off
spool '/opt/generate_encrp_passwords.sql';
select 'alter user '||username||' identified by values '||chr(39)||password||chr(39)||';' from dba_users
order by username;
spool off
exit
----------------------------------------------------------------------------------------------------------------

sqlplus "/as sysdba "
-----to reset to original password execute the spool file-------
@/opt/generate_encrp_passwords.sql

Tuesday, January 31, 2012

standby database ,rman incremental standby recovery


The  situation of  standby database lagging behind to primary database due to network outage can be tacked, without rebuild in Oracle 10g environment by following the below steps. The database version must be greater than 10.1.0.4 and and in a situation where scping of thousands of archives logs is not feasible
Steps to bring the standby database in sync with Primary Database is listed below.

1. Find the current SCN of standby database.

select current_scn from v$database;
CURRENT_SCN
———–
4793543

2. On the primary database – create the needed incremental backup from the above SCN

rman target /
RMAN> BACKUP DEVICE TYPE DISK INCREMENTAL FROM SCN 4793543 DATABASE FORMAT ‘\opt\bkup_%U’;

3. create a new standby controlfile from production

sqlplus> alter database create standby controlfile ‘stdby’;

4.cancel managed recovery on standby
sqlplus> recover managed standby database cancel;

5. Move your incremental backup from (2) to the standby server (empty folder) and catalog it
rman target /
rman> catalog start with ‘\opt\backup\’;

6. recover your standby from the incremental backup
rman> recover database noredo;

7. shutdown the standby and replace the controlfile with the one you backup in (3)

8.startup the standby and put it back into managed recovery mode
sqlplus> startup mount
sqlplus> alter database recover  managed standby database disconnect;

Top 5 Session consuming buffers in oracle database

prompt
prompt **Top 5 by Buffer Gets:
prompt

set linesize 2000
set pagesize 100
set TIMI ON
select d.sql_text sql,'''', x.sid,'''',x.serial#, '''',x.username,'''',x.machine,'''', x.module,'''', x.program,'''', x.hash_value  from (SELECT * FROM
(SELECT s.sid,s.serial#, s.username,s.machine, a.hash_value, a.address, s.module, s.program, a.buffer_gets  FROM
V$SQLAREA a, v$session s
WHERE a.buffer_gets > 10000 and a.hash_value = s.sql_hash_value and a.address=s.sql_address and s.sid !=(select distinct sid from v$mystat)
ORDER BY buffer_gets DESC)
WHERE rownum <= 5) x, v$sqltext_with_newlines d where x.hash_value=d.hash_value and x.address=d.address order by x.buffer_gets desc, x.hash_value, d.piece;

Delete Old archives which are more than 3 days old in oracle database



Below shell scrip Can be used for deleting old archives from oracle database which are > 3 days old
Description of each value
/bin/find: Location of find executable
/opt/DB_ARCH/  : Location to be searched




Remove  Archived Logs after 3 days
--------------------------------------------------------
00 07 * * * /bin/ksh -c '/bin/find /opt/DB_ARCH/ -type f -mtime +3 -name "arch*.arc" -print -exec rm {} \;' 2>&1  >>/opt/DB_ARCH/archives.deleted.log


This script can also be customized to delete the traces and other files based on your requirement

Monday, January 30, 2012

Recompile Invalid objects in oracle database


Below Scripts can be used for making the database objects valid

set feedback off
      set verify off
      set echo off
      set pagesize 0
      set heading off
      spool compile_invalid_objects

select  'alter ' ||
             decode(object_type, 'PACKAGE BODY', 'package', object_type) ||
             ' ' ||owner ||'.'||
             object_name||
             ' compile' ||
             decode(object_type, 'PACKAGE BODY', ' body;', ';')
      from   dba_objects
      where  status = 'INVALID';

      spool off
      set feedback on
      set verify on
      set heading on
      set pagesize 40
      @compile_invalid_objects

Sunday, January 29, 2012

How to resize a datafile and free up the space at OS level


execute below sql query to find out how much space can be reclaimed at dataffile level as below
SQL> select file_name,
  2  ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) smallest,
ceil( blocks*&&blksize/1024/1024) currsize,
  3    4  ceil( blocks*&&blksize/1024/1024) -
  5  ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) savings
  6  from dba_data_files a,
  7  ( select file_id, max(block_id+blocks-1) hwm
  8  from dba_extents
  9  group by file_id ) b
 10  where a.file_id = b.file_id(+)
 11  /

                                                   Smallest
                                                       Size  Current    Poss.
FILE_NAME                                             Poss.     Size  Savings
-------------------------------------------------- -------- -------- --------
/Ora10g/prod/system01.dbf                   421      544      123
/Ora10gdb10/oradata/prod/collectord01.dbf             1,223    1,374      151
/sv101/db14/oradata/prod/prdsselx00p_06.dbf           19,991   20,000        9
/Ora10gdb01/oradata/prod/disceuld_01.dbf              5,502    5,502        0
/Ora10gdb03/oradata/prod/prodprds2d01.dbf               1,585    1,585        0
/Ora10gdb03/oradata/prod/prodprds2x01.dbf               1,350    1,350        0
/Ora10gdb07/oradata/prod/prdsotlhx00p_01.dbf          19,991   19,991        0
/Ora10gdb08/oradata/prod/prdssrd00p_01.dbf            24,991   25,000        9
/Ora10gdb09/oradata/prod/prdssrx00p_01.dbf            17,251   17,401      150
/Ora10gdb08/oradata/prod/prdssrd00p_02.dbf            24,991   25,000        9
/Ora10gdb04/oradata/prod/tools01.dbf                     20       20        0
/Ora10gdb01/oradata/prod/users_01.dbf                     4       10        6
/Ora10gdb05/oradata/prod/ctsreportsd02.dbf           11,511   11,550       39
/Ora10gdb01/oradata/prod/prdsmatrix_04.dbf            20,000   20,000        0
/Ora10gdb01/oradata/prod/prdsmatrix_05.dbf            20,000   20,000        0
/Ora10gdb01/oradata/prod/datamartd_05.dbf            19,997   19,997        0
/Ora10gdb01/oradata/prod/datamartd_06.dbf            19,997   20,000        3
/Ora10gdb01/oradata/prod/datamartd_07.dbf            19,997   20,000        3
/Ora10gdb05/oradata/prod/prdssachd00p_08.dbf           9,421   19,991   10,570

below query will provide the statement that can executed to shrink the datfiles and reclaim space at OS level
SQL> SQL> SQL> select 'alter database datafile ''' || file_name || ''' resize ' ||
  2  ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) || 'm;' cmd
  3  from dba_data_files a,
  4  ( select file_id, max(block_id+blocks-1) hwm
  5  from dba_extents
  6  group by file_id ) b
where a.file_id = b.file_id(+)
  7    8  and ceil( blocks*&&blksize/1024/1024) -
  9  ceil( (nvl(hwm,1)*&&blksize)/1024/1024 ) > 0
 10  /

alter database datafile '/Ora10gdb10/oradata/prod/collectord01.dbf' resize
1223 m;

Saturday, January 14, 2012

Why the session is taking more time than normal in oracle database ?

Step 1: Check the Alert log for errors
Step 2: Check with the end user on how long the session used to take to complete.
Step 3: Check with user whether any increase in the volume of data being
             Processed.
Step 4:  Check the wait events for this sessions by using the query given below
              select p1, p2, p3, event
from   v$session_wait
where sid=&session_id;
Also check the following:
select count(*), event
from    v$session_wait
group   by event;
Step 5:  Identify the tables begin accessed by that session using below query
select owner, object, type
from   v$access
where sid=&session_id
and     owner not in (‘SYS’,’SYSTEM’);
.
Step 6:  Check whether Statistics have been generated for these tables identified
              using the following query:
              select  last_analyzed, num_rows
              from   dba_tables
              where  owner=’&table_owner’
              and      table_name=’&object_name’;
              Note: If the table is partitioned, check in dba_tab_partitions also and
                         for subpartitions check in dba_tab_subpartitions.
Step 7: Check whether Statistics have been generated for the indexes of the tables
             Identified Using the following query:
             select  last_analyzed, num_rows
             from   dba_indexes
             where  table_owner=’&table_owner’
             and      table_name=’&table_name’;
             Note: If the table is partitioned, check in dba_ind_partitions and for
                       subpartitions check in dba_ind_subpartitions.
Step 8:  If Statistics is not up-to-date, Generate stats using dbms_stats package.
Step 9:  Follow the approach given in below for sql tuning.
Avoid Using the Following:
a. Boolean Operators, Is null & Is not Null.
b. not in, != Operators
c. like ‘%patterns’, not exists
          
             Do’s:
a. Enable aliases to prefix all columns.
b. Use sql joins instead of sub-queries
c. Make the tables with the least number of rows as the driving table by keeping them first in the FROM clause.
d. Use concatenated indexes wherever appropriate.
e. Pick up the Best Join method.
f. Nested loops joins are best for indexed joins of subsets.
g. Hash joins are usually the best choice for "big" joins
h. Pick the best "driving" table
i. Use bind variables. Bind variables are key to application scalability.
j. Use Oracle hints wherever appropriate
k. Compare performance between alternative syntax for your SQL statement

                  Use Explain Plan to Identify the Access path being used by the query.
                  Syntax is explain plan for actual_sql_statement;
                  You can see the output of the explain plan by running the following sql:
                   $ORACLE_HOME/rdbms/admin/utlxplp.sql
                  Alternatively, You can also trace the session by using the
                  following Command:
                  alter session set events '10046 trace name context forever,level 12';
                  Run the sql Query.  This will generate the trace file in udump directory.
                  Use tkprof utility to get the readable output of this trace file.  Use the
                  following Syntax:
                  tkprof trace_file_name trace_file_name.out sys=no explain=userid/pwd
                  This tkprof output file trace_file_name.out will have the access path
                  Used by the queries and as well the various timed statistics like
                  cpu time, elapsed time etc.