oracle Gather stats for a table

BEGIN
DBMS_STATS.GATHER_TABLE_STATS (
ownname => ‘HR’,
tabname => ‘EMP’,
cascade => true, —- For collecting stats for respective indexes
method_opt=>’for all indexed columns size 1′,
granularity => ‘ALL’,
estimate_percent =>dbms_stats.auto_sample_size,
degree => 4);
END;
/

— For a single table partition
BEGIN
DBMS_STATS.GATHER_TABLE_STATS (
ownname => ‘HR’,
tabname => ‘EMP’, — TABLE NAME
partname => ‘EMP_P1’ — PARTITOIN NAME
method_opt=>’for all indexed columns size 1′,
GRANULARITY => ‘APPROX_GLOBAL AND PARTITION’,
degree => 8);
END;
/

Oracle Archivelog generation per hour

set lines 300
SELECT TO_CHAR(TRUNC(FIRST_TIME),’Mon DD’) “DG Date”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’00’,1,0)),’9999′) “12AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’01’,1,0)),’9999′) “01AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’02’,1,0)),’9999′) “02AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’03’,1,0)),’9999′) “03AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’04’,1,0)),’9999′) “04AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’05’,1,0)),’9999′) “05AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’06’,1,0)),’9999′) “06AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’07’,1,0)),’9999′) “07AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’08’,1,0)),’9999′) “08AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’09’,1,0)),’9999′) “09AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’10’,1,0)),’9999′) “10AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’11’,1,0)),’9999′) “11AM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’12’,1,0)),’9999′) “12PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’13’,1,0)),’9999′) “1PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’14’,1,0)),’9999′) “2PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’15’,1,0)),’9999′) “3PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’16’,1,0)),’9999′) “4PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’17’,1,0)),’9999′) “5PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’18’,1,0)),’9999′) “6PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’19’,1,0)),’9999′) “7PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’20’,1,0)),’9999′) “8PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’21’,1,0)),’9999′) “9PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’22’,1,0)),’9999′) “10PM”,
TO_CHAR(SUM(DECODE(TO_CHAR(FIRST_TIME,’HH24′),’23’,1,0)),’9999′) “11PM”
FROM V$LOG_HISTORY
GROUP BY TRUNC(FIRST_TIME)
ORDER BY TRUNC(FIRST_TIME) DESC
/

Oracle Database Size

col “Database Size” format a20
col “Free space” format a20
col “Used space” format a20
select round(sum(used.bytes) / 1024 / 1024 / 1024 ) || ‘ GB’ “Database Size”
, round(sum(used.bytes) / 1024 / 1024 / 1024 ) –
round(free.p / 1024 / 1024 / 1024) || ‘ GB’ “Used space”
, round(free.p / 1024 / 1024 / 1024) || ‘ GB’ “Free space”
from (select bytes
from v$datafile
union all
select bytes
from v$tempfile
union all
select bytes
from v$log) used
, (select sum(bytes) as p
from dba_free_space) free
group by free.p
/

Oracle-Delete Old Archives Using RMAN

Deletion of archive logs in oracle 11g database by comparing the sysdate

Prepare the shell script.

cat rman_arch_del.sh

#!/bin/bash
export ORACLE_HOME=/oracle/app/oracle/product/12.1.0.2.0
export ORACLE_SID=FRANCE
export PATH=$ORACLE_HOME/bin:$PATH

delBackup () {
rman log=/home/oracle/arch_del.log << EOF
connect target /
DELETE noprompt ARCHIVELOG ALL COMPLETED BEFORE ‘sysdate-1’;
CROSSCHECK ARCHIVELOG ALL;
DELETE EXPIRED ARCHIVELOG ALL;
exit
EOF
}
# Main

delBackup

Now configure in crontab:

00 30 * * * /u01/app/oracle/rman_arch_del.sh > /tmp/rmanarch.log

Catch Me On:- Hariprasath Rajaram 

Telegram:https://t.me/joinchat/I_f4DkeGfZuxgMIoJSpQZg LinkedIn:https://www.linkedin.com/in/hari-prasath-aa65bb19/ Facebook:https://www.facebook.com/HariPrasathdba                       FB Group:https://www.facebook.com/groups/894402327369506/                   FB Page: https://www.facebook.com/dbahariprasath/? Twitter: https://twitter.com/hariprasathdba

 

 

Oracle Schema DDL

Vi Schema.sql (create sqlfile and execute)

set long 20000 longchunksize 20000 pagesize 0 linesize 1000 feedback off verify off trimspool on
column ddl format a1000

begin
dbms_metadata.set_transform_param (dbms_metadata.session_transform, ‘SQLTERMINATOR’, true);
dbms_metadata.set_transform_param (dbms_metadata.session_transform, ‘PRETTY’, true);
end;
/

variable v_username VARCHAR2(30);

exec:v_username := upper(‘&1’);

select dbms_metadata.get_ddl(‘USER’, u.username) AS ddl
from dba_users u
where u.username = :v_username
union all
select dbms_metadata.get_granted_ddl(‘TABLESPACE_QUOTA’, tq.username) AS ddl
from dba_ts_quotas tq
where tq.username = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl(‘ROLE_GRANT’, rp.grantee) AS ddl
from dba_role_privs rp
where rp.grantee = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl(‘SYSTEM_GRANT’, sp.grantee) AS ddl
from dba_sys_privs sp
where sp.grantee = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl(‘OBJECT_GRANT’, tp.grantee) AS ddl
from dba_tab_privs tp
where tp.grantee = :v_username
and rownum = 1
union all
select dbms_metadata.get_granted_ddl(‘DEFAULT_ROLE’, rp.grantee) AS ddl
from dba_role_privs rp
where rp.grantee = :v_username
and rp.default_role = ‘YES’
and rownum = 1
union all
select to_clob(‘/* Start profile creation script in case they are missing’) AS ddl
from dba_users u
where u.username = :v_username
and u.profile <> ‘DEFAULT’
and rownum = 1
union all
select dbms_metadata.get_ddl(‘PROFILE’, u.profile) AS ddl
from dba_users u
where u.username = :v_username
and u.profile <> ‘DEFAULT’
union all
select to_clob(‘End profile creation script */’) AS ddl
from dba_users u
where u.username = :v_username
and u.profile <> ‘DEFAULT’
and rownum = 1
/

set linesize 80 pagesize 14 feedback on trimspool on verify on

To check the Library Cache Lock contention

Library cache resource types waited for over the life of the instance

set linesize 152
column average_wait format 9999990.00

select substr(e.event, 1, 40) event,
e.time_waited,e.time_waited / decode(e.event,
‘latch free’, e.total_waits,
decode(e.total_waits – e.total_timeouts,0, 1,e.total_waits – e.total_timeouts)) average_wait
from sys.v$system_event e,sys.v$instance i
where e.event like ‘%library cache%’;

Detect sessions waiting for a Library Cache Locks

select sid Waiter, p1raw,
substr(rawtohex(p1),1,30) Handle,
substr(rawtohex(p2),1,30) Pin_addr
from v$session_wait where wait_time=0 and event like ‘%library cache%’;

Sessions waiting for lib cache in RAC

select a.sid Waiter,b.SERIAL#,a.event,a.p1raw,
substr(rawtohex(a.p1),1,30) Handle,
substr(rawtohex(a.p2),1,30) Pin_addr
from v$session_wait a,v$session b where a.sid=b.sid
and a.wait_time=0 and a.event like ‘library cache%’;

Objects locked by Library Cache based on sessions detected above

select to_char(SESSION_ID,’999′) sid ,
substr(LOCK_TYPE,1,30) Type,
substr(lock_id1,1,23) Object_Name,
substr(mode_held,1,4) HELD, substr(mode_requested,1,4) REQ,
lock_id2 Lock_addr
from dba_lock_internal
where
mode_requested<>’None’
and mode_requested<>mode_held
and session_id in ( select sid
from v$session_wait where wait_time=0
and event like ‘%library cache%’) ;

Detect Library Cache holders that sessions are waiting for

select sid Holder ,KGLPNUSE Sesion , KGLPNMOD Held, KGLPNREQ Req
from x$kglpn , v$session
where KGLPNHDL in (select p1raw from v$session_wait
where wait_time=0 and event like ‘%library cache%’)
and KGLPNMOD <> 0
and v$session.saddr=x$kglpn.kglpnuse ;

Sessions holding the lib cache in RAC

select a.sid Holder ,a.SERIAL#,b.INST_ID,b.KGLPNUSE Sesion , b.KGLPNMOD Held, b.KGLPNREQ Req
from x$kglpn b , v$session a
where b.KGLPNHDL in (select p1raw from v$session_wait
where wait_time=0 and event like ‘library cache%’)
and b.KGLPNMOD <> 0
and a.saddr=b.kglpnuse ;

Library cache pin sessions
SELECT s.sid,
waiter.p1raw w_p1r,
holder.event h_wait,
holder.p1raw h_p1r,
holder.p2raw h_p2r,
holder.p3raw h_p2r,
count(s.sid) users_blocked,
sql.hash_value
FROM
v$sql sql,
v$session s,
x$kglpn p,
v$session_wait waiter,
v$session_wait holder
WHERE
s.sql_hash_value = sql.hash_value and
p.kglpnhdl=waiter.p1raw and
s.saddr=p.kglpnuse and
waiter.event like ‘library cache pin’ and
holder.sid=s.sid
GROUP BY
s.sid,
waiter.p1raw ,
holder.event ,
holder.p1raw ,
holder.p2raw ,
holder.p3raw ,
sql.hash_value;

Library Cache lock Query
select decode(lob.kglobtyp,
0, ‘NEXT OBJECT ‘,
1, ‘INDEX ‘,
2, ‘TABLE ‘,
3, ‘CLUSTER ‘,
4, ‘VIEW ‘,
5, ‘SYNONYM ‘,
6, ‘SEQUENCE ‘,
7, ‘PROCEDURE ‘,
8, ‘FUNCTION ‘,
9, ‘PACKAGE ‘,
11, ‘PACKAGE BODY ‘,
12, ‘TRIGGER ‘,
13, ‘TYPE ‘,
14, ‘TYPE BODY ‘,
19, ‘TABLE PARTITION ‘,
20, ‘INDEX PARTITION ‘,
21, ‘LOB ‘,
22, ‘LIBRARY ‘,
23, ‘DIRECTORY ‘,
24, ‘QUEUE ‘,
28, ‘JAVA SOURCE ‘,
29, ‘JAVA CLASS ‘,
30, ‘JAVA RESOURCE ‘,
32, ‘INDEXTYPE ‘,
33, ‘OPERATOR ‘,
34, ‘TABLE SUBPARTITION ‘,
35, ‘INDEX SUBPARTITION ‘,
40, ‘LOB PARTITION ‘,
41, ‘LOB SUBPARTITION ‘,
42, ‘MATERIALIZED VIEW ‘,
43, ‘DIMENSION ‘,
44, ‘CONTEXT ‘,
46, ‘RULE SET ‘,
47, ‘RESOURCE PLAN ‘,
48, ‘CONSUMER GROUP ‘,
51, ‘SUBSCRIPTION ‘,
52, ‘LOCATION ‘,
55, ‘XML SCHEMA ‘,
56, ‘JAVA DATA ‘,
57, ‘SECURITY PROFILE ‘,
59, ‘RULE ‘,
62, ‘EVALUATION CONTEXT ‘,
‘UNDEFINED ‘) object_type,
lob.kglnaobj object_name,
pn.kglpnmod lock_mode_held,
pn.kglpnreq lock_mode_requested,
ses.sid,
ses.serial#,
ses.username
from sys.v$session_wait vsw,
sys.x$kglob lob,
sys.x$kglpn pn,
sys.v$session ses
where vsw.event = ‘library cache lock ‘
and vsw.p1raw = lob.kglhdadr
and lob.kglhdadr = pn.kglpnhdl
and pn.kglpnmod != 0
and pn.kglpnuse = ses.saddr
/

Detect Library Cache holders that sessions are waiting for

set pagesize 40
select x$kglpn.inst_id,sid Holder ,KGLPNUSE Sesion , KGLPNMOD Held, KGLPNREQ Req
from x$kglpn , gv$session
where KGLPNHDL in (select p1raw from gv$session_wait
where wait_time=0 and event like ‘library cache%’)
and KGLPNMOD <> 0
and gv$session.saddr=x$kglpn.kglpnuse ;
PROMPT Detect Library Cache holders that sessions are waiting for

Detect sessions waiting for a Library Cache Locks

select sid Waiter, p1raw,
substr(rawtohex(p1),1,30) Handle,
substr(rawtohex(p2),1,30) Pin_addr
from gv$session_wait where wait_time=0 and event like ‘library cache%’;