Friday, September 27, 2013

generate awr report for database


// generate awr report for database ( not instance) for period 8 -9 am

conn sys/manager@&1 as sysdba
variable begin_snap_id number
variable end_snap_id number
variable db_id number
begin
  select distinct snap_id into :begin_snap_id from dba_hist_snapshot
  where BEGIN_INTERVAL_TIME like '01-SEP-13 07%AM%';
  :end_snap_id := :begin_snap_id + 1;
  select DBID into :db_id from v$database;
end;
/
define  num_days     = 1;
define  db_name      = &1;
define  dbid         = :db_id;
define  begin_snap   = :begin_snap_id;
define  end_snap     = :end_snap_id;
define  report_type  = 'html';
define  instance_numbers_or_ALL = '1,2,3'
define  time         = 8am
define  report_name  = /tmp/AWR/awr_&1._&time..html
@@?/rdbms/admin/awrgrpti
exit

Wednesday, September 25, 2013

dynamic tablespaces creation script

// dynamic tablespaces creation script

// crtbs.sh


while read tbs
   do
   echo "CREATE SMALLFILE TABLESPACE "$tbs " DATAFILE
   '/oradata01/oracle/oradata/${ORACLE_SID}/$tbs.dbf' SIZE 10M AUTOEXTEND ON NEXT 100M MAXSIZE UNLIMITED
   LOGGING
   ONLINE
   EXTENT MANAGEMENT LOCAL  UNIFORM SIZE 10M
   SEGMENT SPACE MANAGEMENT AUTO
   ;"
   echo " "
   done< crtbs.dat

// crtbs.dat
MAX_DATA
MAX_INDX

list table parent / childs


//  finds child table in recursive . input parent table

Prompt By default starting tables are those that does not referenced other one
Accept Table_ CHAR PROMPT "Starting tables (% allowed) ? "
Set Termout OFF
Column table_ NOPRINT NEW_VALUE Table_
Select nvl(upper('&Table_'),'*NO REF*') table_ from dual
/
Set Termout ON
Column line_ FORMAT A120 HEADING "Foreign Key Tree"
Prompt
Set feedback off
With
  data as (
      /* Tables/FK along with the referenced table/constraints */
    select decode(b.table_name, a.table_name, '*SELF REF*', b.table_name)
             table_name,
           b.constraint_name,
           a.table_name r_table_name,
           a.constraint_name r_unq_name
    from user_constraints a, user_constraints b
    where b.r_owner = a.owner
      and b.r_constraint_name = a.constraint_name
      and b.constraint_type = 'R'
      and a.constraint_type in ('P', 'U')
    union all
      /* Referenced tables that referenced noone */
    select a.table_name, null, null, null
    from user_constraints a
    where exists ( select 1 from user_constraints b
                   where b.r_owner = a.owner
                     and b.r_constraint_name = a.constraint_name
                     and b.constraint_type = 'R'
                     and a.constraint_type in ('P', 'U') )
      and not exists ( select 1 from user_constraints b
                       where b.table_name = a.table_name
                         and b.constraint_type = 'R' )
  )
Select lpad(' ',3*(level-1)) ||
       decode(table_name, '*SELF REF*', r_table_name, table_name) ||
       decode(r_table_name,
              null, '',
              ' ('||constraint_name||' ref. '||
              r_table_name||'/'||r_unq_name||')' )||
       decode(table_name,
              '*SELF REF*', ' ...',
              decode(connect_by_iscycle, 1, ' ...'))
from data
connect by nocycle prior table_name = r_table_name
start with    ( '&Table_' = '*NO REF*' and r_table_name is null )
           or ( '&Table_' != '*NO REF*' and table_name like '&Table_' )
/

compile invalids

// get_dependencies.sql

// dependencies for packages etc

SELECT distinct referenced_name, referenced_type
  FROM  dba_dependencies
  WHERE
    REFERENCED_OWNER=upper('&owner')
  and name =upper('&objname') order by referenced_type ;

//
exec dbms_utility.get_dependency('PACKAGE BODY', 'SCOTT','MYPKG');




// get_ddl.sql
SET LONG 2000000
SET PAGESIZE 0
select DBMS_METADATA.GET_DDL('PACKAGE_BODY','MYPKG') from dual;

// gen_compile.sql


set pages 0

spool gen_compile.out
accept inuser char prompt 'Enter Userid: '
select 'alter '||case when object_type='PACKAGE BODY' then 'PACKAGE' else object_type end||' '||owner||'.'||object_name||' compile;'
from dba_objects
where owner like upper('&inuser')
and status='INVALID'
order by object_type
/
spool off


//  count_invalids.sql

rem
ttitle 'Count of Invalid Objects'
rem
col owner format a8 heading 'OWNER'
col invalids format 999,999 heading '# of INVALIDS OBJECTS'
break on owner skip 1
rem
select owner,
       object_type,
       count(*)  invalids
  from   dba_objects
 where  owner like upper('&owner')
  and   status ='INVALID'
 group by rollup(owner, object_type);rem  count_invalids.sql



//  list_invalids.sql

rem
set time on
set timing on
set echo off
set feedback on
rem
break on owner on type skip 1
rem
select substr(owner,1,12) owner
       ,substr(object_type,1,30) type
       ,substr(object_name,1,30) object
       ,status
  from
    dba_objects
 where
   owner like upper('&owner')
 and
   object_type like upper('&type')
 and
   status <> 'VALID'
 order by owner,object_type,object_name;
rem
rem end of script


// object not recompiling as it is in use

   COLUMN lock_id2 FORMAT A30
   select to_char(SESSION_ID,'999') sid ,
      substr(LOCK_TYPE,1,30) Type,
      substr(lock_id1,1,45) Object_Name,
      substr(mode_held,1,4) HELD,
      substr(mode_requested,1,4) REQ,
      lock_id2 lock_addr
   FROM dba_lock_internal;


you may try following steps, too

// step 1

Identify sessions that are using PACKAGES using the query below.

The following query displays all objects currently being accessed by each session:

set lines 120
col username format a20
col owner    format a15
col object   format a30
col type     format a15

select a.sid, a.serial#, a.username, b.owner, b.object, b.type

  from
    v$session a,
    v$access  b
  where
    a.sid = b.sid
  and
    b.object like upper('%&object%');

// step2

ALTER SYSTEM KILL SESSION 'SID,SERIAL#';


Friday, September 13, 2013

Usefull Unix ls commands


Don’t you hate when you want to see the specific time on a file and ls -l gives you this on older files:

oracle@ > ls -ltr
total 2328
-rw-r--r-- 1 oracle oinstall  22413 Jun  5 10:59 ashrpt_1_0605_0900.txt
-rw-r--r-- 1 oracle oinstall    247 Jun  5 11:31 afiedt.buf
-rw-r--r-- 1 oracle oinstall 255934 Jun  5 11:33 awrrpt_1_11390_11391.txt
-rw-r--r-- 1 oracle oinstall  14594 Jun  5 11:33 addmrpt_1_11390_11391.txt

Fortunately, ls gives us some options:

oracle@  > ls -ltr --full-time
total 2328

-rw-r--r-- 1 oracle oinstall  22413 2013-06-05 10:59:55.000000000 -0400 ashrpt_1_0605_0900.txt
-rw-r--r-- 1 oracle oinstall    247 2013-06-05 11:31:29.000000000 -0400 afiedt.buf
-rw-r--r-- 1 oracle oinstall 255934 2013-06-05 11:33:09.000000000 -0400 awrrpt_1_11390_11391.txt
-rw-r--r-- 1 oracle oinstall  14594 2013-06-05 11:33:25.000000000 -0400 addmrpt_1_11390_11391.txt




or this using the same format options the date command uses. In this case year-month-day.24 hour:minute

oracle@ > ls -ltr --time-style=+%F.%R
total 2328

-rw-r--r-- 1 oracle oinstall  22413 2013-06-05.10:59 ashrpt_1_0605_0900.txt
-rw-r--r-- 1 oracle oinstall    247 2013-06-05.11:31 afiedt.buf
-rw-r--r-- 1 oracle oinstall 255934 2013-06-05.11:33 awrrpt_1_11390_11391.txt
-rw-r--r-- 1 oracle oinstall  14594 2013-06-05.11:33 addmrpt_1_11390_11391.txt


Got to love it.