Saturday, December 1, 2012

ASH and AWR Performance Tuning Scripts

ASH and AWR Performance Tuning Scripts

Ref: http://gavinsoorma.com/2012/11/ash-and-awr-performance-tuning-scripts/


Top Recent Wait Events

 col EVENT format a60

select * from (
select active_session_history.event,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history
where active_session_history.event is not null
group by active_session_history.event
order by 2 desc)
where rownum < 6
/


Top Wait Events Since Instance Startup


 col event format a60

select event, total_waits, time_waited
from v$system_event e, v$event_name n
where n.event_id = e.event_id
and n.wait_class !='Idle'
and n.wait_class = (select wait_class from v$session_wait_class
 where wait_class !='Idle'
 group by wait_class having
sum(time_waited) = (select max(sum(time_waited)) from v$session_wait_class
where wait_class !='Idle'
group by (wait_class)))
order by 3;
List Of Users Currently Waiting
 col username format a12
col sid format 9999
col state format a15
col event format a50
col wait_time format 99999999
set pagesize 100
set linesize 120

select s.sid, s.username, se.event, se.state, se.wait_time
from v$session s, v$session_wait se
where s.sid=se.sid
and se.event not like 'SQL*Net%'
and se.event not like '%rdbms%'
and s.username is not null
order by se.wait_time;


Find The Main Database Wait Events In A Particular Time Interval

First determine the snapshot id values for the period in question.


In this example we need to find the SNAP_ID for the period 10 PM to 11 PM on the 14th of November, 2012.
 select snap_id,begin_interval_time,end_interval_time
from dba_hist_snapshot
where to_char(begin_interval_time,'DD-MON-YYYY')='14-NOV-2012'
and EXTRACT(HOUR FROM begin_interval_time) between 22 and 23;
set verify off
 select * from (
 select active_session_history.event,
 sum(active_session_history.wait_time +
 active_session_history.time_waited) ttl_wait_time
 from dba_hist_active_sess_history active_session_history
 where event is not null
 and SNAP_ID between &ssnapid and &esnapid
 group by active_session_history.event
 order by 2 desc)
 where rownum

Top CPU Consuming SQL During A Certain Time Period


Note – in this case we are finding the Top 5 CPU intensive SQL statements executed between 9.00 AM and 11.00 AM


 select * from (
select
SQL_ID,
 sum(CPU_TIME_DELTA),
sum(DISK_READS_DELTA),
count(*)
from
DBA_HIST_SQLSTAT a, dba_hist_snapshot s
where
s.snap_id = a.snap_id
and s.begin_interval_time > sysdate -1
and EXTRACT(HOUR FROM S.END_INTERVAL_TIME) between 9 and 11
group by
SQL_ID
order by
sum(CPU_TIME_DELTA) desc)
where rownum


Which Database Objects Experienced the Most Number of Waits in the Past One Hour


 set linesize 120
col event format a40
col object_name format a40

select * from
(
  select dba_objects.object_name,
 dba_objects.object_type,
active_session_history.event,
 sum(active_session_history.wait_time +
  active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history,
    dba_objects
 where
active_session_history.sample_time between sysdate - 1/24 and sysdate
and active_session_history.current_obj# = dba_objects.object_id
 group by dba_objects.object_name, dba_objects.object_type, active_session_history.event
 order by 4 desc)
where rownum < 6;
Top Segments ordered by Physical Reads
 col segment_name format a20
col owner format a10
select segment_name,object_type,total_physical_reads
 from ( select owner||'.'||object_name as segment_name,object_type,
value as total_physical_reads
from v$segment_statistics
 where statistic_name in ('physical reads')
 order by total_physical_reads desc)
 where rownum
Top 5 SQL statements in the past one hour
 select * from (
select active_session_history.sql_id,
 dba_users.username,
 sqlarea.sql_text,
sum(active_session_history.wait_time +
active_session_history.time_waited) ttl_wait_time
from v$active_session_history active_session_history,
v$sqlarea sqlarea,
 dba_users
where
active_session_history.sample_time between sysdate -  1/24  and sysdate
  and active_session_history.sql_id = sqlarea.sql_id
and active_session_history.user_id = dba_users.user_id
 group by active_session_history.sql_id,sqlarea.sql_text, dba_users.username
 order by 4 desc )
where rownum
SQL with the highest I/O in the past one day
 select * from
(
SELECT /*+LEADING(x h) USE_NL(h)*/
       h.sql_id
,      SUM(10) ash_secs
FROM   dba_hist_snapshot x
,      dba_hist_active_sess_history h
WHERE   x.begin_interval_time > sysdate -1
AND    h.SNAP_id = X.SNAP_id
AND    h.dbid = x.dbid
AND    h.instance_number = x.instance_number
AND    h.event in  ('db file sequential read','db file scattered read')
GROUP BY h.sql_id
ORDER BY ash_secs desc )
where rownum
Top CPU consuming queries since past one day
 select * from (
select
 SQL_ID,
 sum(CPU_TIME_DELTA),
 sum(DISK_READS_DELTA),
 count(*)
from
 DBA_HIST_SQLSTAT a, dba_hist_snapshot s
where
 s.snap_id = a.snap_id
 and s.begin_interval_time > sysdate -1
 group by
 SQL_ID
order by
 sum(CPU_TIME_DELTA) desc)
where rownum
Find what the top SQL was at a particular reported time of day

First determine the snapshot id values for the period in question.

In thos example we need to find the SNAP_ID for the period 10 PM to 11 PM on the 14th of November, 2012.
 select snap_id,begin_interval_time,end_interval_time
from dba_hist_snapshot
where to_char(begin_interval_time,'DD-MON-YYYY')='14-NOV-2012'
and EXTRACT(HOUR FROM begin_interval_time) between 22 and 23; select * from
 (
select
 sql.sql_id c1,
sql.buffer_gets_delta c2,
sql.disk_reads_delta c3,
sql.iowait_delta c4
 from
dba_hist_sqlstat sql,
dba_hist_snapshot s
 where
 s.snap_id = sql.snap_id
and
 s.snap_id= &snapid
 order by
 c3 desc)
 where rownum < 6
/
Analyse a particular SQL ID and see the trends for the past day
 select
 s.snap_id,
 to_char(s.begin_interval_time,'HH24:MI') c1,
 sql.executions_delta c2,
 sql.buffer_gets_delta c3,
 sql.disk_reads_delta c4,
 sql.iowait_delta c5,
sql.cpu_time_delta c6,
 sql.elapsed_time_delta c7
 from
 dba_hist_sqlstat sql,
 dba_hist_snapshot s
 where
 s.snap_id = sql.snap_id
 and s.begin_interval_time > sysdate -1
 and
sql.sql_id='&sqlid'
 order by c7
 /
Do we have multiple plan hash values for the same SQL ID – in that case may be changed plan is causing bad performance
 select
  SQL_ID
, PLAN_HASH_VALUE
, sum(EXECUTIONS_DELTA) EXECUTIONS
, sum(ROWS_PROCESSED_DELTA) CROWS
, trunc(sum(CPU_TIME_DELTA)/1000000/60) CPU_MINS
, trunc(sum(ELAPSED_TIME_DELTA)/1000000/60)  ELA_MINS
from DBA_HIST_SQLSTAT
where SQL_ID in (
'&sqlid')
group by SQL_ID , PLAN_HASH_VALUE
order by SQL_ID, CPU_MINS;
Top 5 Queries for past week based on ADDM recommendations
 /*
Top 10 SQL_ID's for the last 7 days as identified by ADDM
from DBA_ADVISOR_RECOMMENDATIONS and dba_advisor_log
*/

col SQL_ID form a16
col Benefit form 9999999999999
select * from (
select b.ATTR1 as SQL_ID, max(a.BENEFIT) as "Benefit"
from DBA_ADVISOR_RECOMMENDATIONS a, DBA_ADVISOR_OBJECTS b
where a.REC_ID = b.OBJECT_ID
and a.TASK_ID = b.TASK_ID
and a.TASK_ID in (select distinct b.task_id
from dba_hist_snapshot a, dba_advisor_tasks b, dba_advisor_log l
where a.begin_interval_time > sysdate - 7
and  a.dbid = (select dbid from v$database)
and a.INSTANCE_NUMBER = (select INSTANCE_NUMBER from v$instance)
and to_char(a.begin_interval_time, 'yyyymmddHH24') = to_char(b.created, 'yyyymmddHH24')
and b.advisor_name = 'ADDM'
and b.task_id = l.task_id
and l.status = 'COMPLETED')
and length(b.ATTR4) > 1 group by b.ATTR1
order by max(a.BENEFIT) desc) where rownum < 6;

Wednesday, October 3, 2012

Metalink Note Oracle 11gR2

Oracle Database 11gR2 Metalink Notes


Doc ID 1385682.1 The New My Oracle Support User Interface
Doc ID 1371759.1 How To Migrate A Huge ASM Database From Windows 64 bit To Linux 64 bit With The Minimal Down Time?
Doc ID 413484.1 Data Guard Support for Heterogeneous Primary and Physical Standbys in Same Data Guard Configuration
Doc ID 252219.1 Document TitleSteps To Migrate/Move a Database From Non-ASM to ASM And Vice-Versa
Doc ID 369644.1 Document TitleFrequently Asked Questions about Restoring Or Duplicating Between Different Versions And Platforms
Doc ID 881421.1 Using Active Database Duplication to Create Cross Platform Data Guard Setup (Windows/Linux)
Doc ID 988222.1 Oracle Database 11g Release 2 Information Center
Doc ID 785351.1 Oracle 11gR2 Upgrade Companion
Doc ID 958181.1 Rolling a Standby Forward using an RMAN Incremental Backup To Fix The Nologging Changes
Doc ID 881421.1 Using Active Database Duplication to Create Cross Platform Data Guard Setup (Windows/Linux)
Doc ID 874352.1 RMAN 11GR2 : DUPLICATE WITHOUT CONNECTING TO TARGET DATABASE
Doc ID 805438.1 How To Open Physical Standby For Read Write Testing and Flashback
Doc ID 330535.1 Restore Points in Oracle10g Release2
Doc ID 430221.1 How To Reload Datapump Utility EXPDP/IMPDP
Doc ID 247611.1 Known RMAN Performance Problems
Doc ID 262066.1 How To Size UNDO Tablespace For Automatic Undo Management
Doc ID 311615.1 Oracle 10G new feature – Automatic Undo Retention Tuning
Doc ID 286496.1 Export/Import DataPump Parameter TRACE – How to Diagnose Oracle Data Pump
Doc ID 398838.1 FAQ: SQL Query Performance – Frequently Asked Questions
Doc ID 1232802.1 RAC One — Changes in 11.2.0.2
Doc ID 1312225.1 Things to Consider Before Upgrade to 11.2.0.2 Grid Infrastructure
Doc ID 1267828.1 SYS Password Management with RAC and Data Guard
Doc ID 1276368.1 Complete checklist for out-of-place manual upgrade from 11.2.0.1 to 11.2.0.2
Doc ID 341880.1 How to convert a 32-bit database to 64-bit database on Linux?
Doc ID 1080410.1 How To Upgrade 10.2 Repository to 11.2 In The Same Database Instance
Doc ID 883335.1 How To Downgrade From Database 11.2 To Previous Release
Doc ID 1304939.1 11.2 Data Guard Physical Standby Switchover Best Practices using SQL*Plus
Doc ID 1315926.1 11.2.0.1 to 11.2.0.2 Database Upgrade on Exadata Database Machine
Doc ID 953846.1 Grid Control 10.2.0.5.0 now Supports DB 11.2.0.1.0 as a Target
Doc ID 763386.1 Requirements for Installing Oracle 11gR2 32-bit on SLES 10 (x86)
Doc ID 1139563.1 10.2.0.5.4 Grid Control Patch Set Update (PSU)
Doc ID 556610.1 Script to Collect DB Upgrade/Migrate Diagnostic Information (dbupgdiag.sql)
Doc ID 1276368.1 Complete checklist for out-of-place manual upgrade from 11.2.0.1 to 11.2.0.2
Doc ID 1269321.1 Automatic Degree of Parallelism in 11.2.0.2
Doc ID 727062.1 Configuring and using Calibrate I/O
Doc ID 1279458.1 Exadata Database Machine Reference Guide for Upgrade 11.2.0.1 to 11.2.0.2
Doc ID 837570.1 Complete Checklist for Manual Upgrades to 11gR2
Doc ID 884232.1 11gR2 Install (Non-RAC): Understanding New Changes With All New 11.2 Installer
Doc ID 948061.1 How to Check and Enable/Disable Oracle Binary Options
Doc ID 1069015.1 How to determine enabled/disabled components in an 11.2 software-only installation
Doc ID 883299.1 Oracle 11gR2 Relink New Feature
Doc ID 888934.1 Is it possible to deinstall/remove a specific component from already installed Oracle Database Home using OUI?
Doc ID 942406.1 Customize Enterprise Edition Options under 11.2.0.1 version
Doc ID 1245784.1 Installing Component using Installer from 11.2 ORACLE_HOME Fails with OUI-10150
Doc ID 1267942.1 Installing 11.2.0.X Cman Raises Java Exception Referencing S_dlgcfgnaminglabelsid
Doc ID 1232802.1 RAC One — Changes in 11.2.0.2
Doc ID 810394.1 RAC Assurance Support Team: RAC and Oracle Clusterware Starter Kit and Best Practices (Generic)
Doc ID 250.1 Oracle Support Upgrade Advisors
Doc ID 1152016.1 Master Note For Oracle Database Upgrades and Migrations
Doc ID 1084132.1 Differences Between Enterprise, Standard and Personal Editions on Oracle 11.2
Doc ID 1116484.1 Master Note For Oracle Recovery Manager (RMAN)
Doc ID 1187723.1 Master Note for Automatic Storage Management (ASM)
Doc ID 1050908.1 How to Troubleshoot Grid Infrastructure Startup Issues
Doc ID 274526.1 How To Download And Install OPatch
Doc ID 300062.1 How To Clone An Existing RDBMS Installation Using OUI
Doc ID : 949322.1 Oracle11g Data Guard: Database Rolling Upgrade Shell Script
Doc ID : 300062.1 How To Clone An Existing RDBMS Installation Using OUI
Doc ID : 1101938.1 Master Note for Data Guard
Doc ID 1112983.1 How to Reload the JVM in 11.2.0.x
Doc D : 869084.1 New PL/SQL Features for 11g Release 1 (11.1)
Doc ID : 1092213.1 ASM 11.2 Configuration KIT (ASM 11gR2 Installation & Configuration, Deinstallation, Upgrade, ASM Job Role Separation.
Doc ID : 884522.1 How to Download and Run Oracle’s Database Pre-Upgrade Utility.
Doc ID : 386408.1 What Is The Fastest Way To Cleanly Shutdown An Oracle Database?
Doc ID : 1089476.1 Patch 11gR2 Grid Infrastructure Standalone Server.
Doc ID : 1059516.1 Step by step Installation of 11.1 Grid control
Doc ID : 1086562.1 My Oracle Support is now available through Grid Control 11g
Doc ID : 1076420.1 Upgrade Path to Oracle Enterprise Manager 11g Grid Control Release 1 (11.1.0.1.0) from previous releases (10.1.0.x.0 / 10.2.0.x.0)
Doc ID : 1073647.1 How to check/set the database parameters housing the GC repository before GC Install/upgrade
Doc ID : 605398.1 How to to find the version of the main EM components
Doc ID : 412431.1 Oracle Enterprise Manager 10g Grid Control Certification Checker
Doc ID : 810394.1 RAC Assurance Support Team: RAC and Oracle Clusterware Starter Kit and Best Practices (Generic)
Doc ID : 952302.1 Is Microsoft Windows 7 certified to install/run Oracle Database Server/Client ?
Doc ID : 742060.1 Release Schedule of Current Database Releases
Doc ID : 161818.1 Oracle Database (RDBMS) Releases Support Status Summary
Doc ID : 1060197.1 Self-PacedTraining for Oracle Database 11g Release 2
Doc ID : 988222.1 Oracle Database 11g Release 2 Information Center
Doc ID : 1059163.1 Database Release 11.2 Product Info Center: Planning Information Directory
Doc ID : ID 948187.1 ACFS Technical Overview and Deployment Guide
Doc ID : 950200.1 ASM 11gR2: How To Upgrade An ASM Instance To Release 11gR2 (11.2.0.1.0)?
Doc ID : 1069369.1 How to Delete or Add Resource in 11gR2 Grid Infrastructure
Doc ID : 1063299.1 Tablespace Transport for a Single Partition
Doc ID : 733205.1 Migration of Oracle Database Instances Across OS Platforms
Doc ID : 433472.1 OS Watcher For Windows (OSWFW) User Guide
Doc ID : 1072545.1 RMAN Performance Tuning Using Buffer Memory Parameters
Doc ID : 943567.1 11g new feature: Extended Composite Partitioning (Overview, Example and Use)
Doc ID : 948061.1 How to Check and Enable/Disable Oracle Binary Options
Doc ID : 1056322.1 How to Troubleshoot 11gR2 Installation Issue
Doc ID : 1058646.1 How to integrate a 10g/11gR1 RAC database with 11gR2 clusterware (SCAN)
Doc ID : 1050908.1 How to Troubleshoot Grid Infrastructure Startup Issues
Doc ID : 1058646.1 How to integrate a 10g/11gR1 RAC database with 11gR2 clusterware (SCAN)
Doc ID : 1050908.1 How to Troubleshoot Grid Infrastructure Startup Issues
Doc ID : 1053147.1 11gR2 Clusterware and Grid Home – What You Need to Know
Doc ID : 330358.1 CRS 10gR2/ 11gR1/ 11gR2 Diagnostic Collection Guide
Doc ID : 161818.1 Oracle Database (RDBMS) Releases Support Status Summary
Doc ID : 301137.1 OS Watcher User Guide
Doc ID : 1050693.1 Troubleshooting 11.2 Clusterware Node Evictions (Reboots)
Doc ID : 169706.1 Oracle® Database on Unix AIX®,HP-UX®,Linux®,Mac OS® X,Solaris®,Tru64 Unix® Operating Systems Installation and Configuration Requirements Quick Reference (8.0.5 to 11.2)
Doc ID : 883028.1 New Background Processes introduced by ACFS
Doc ID : 810394.1 RAC Assurance Support Team: RAC Starter Kit and Best Practices (Generic)
Doc ID : 220970.1 RAC: Frequently Asked Questions
Doc ID : 970473.1 Manual installation, deinstallation and verification of Oracle Text 11gR2
Doc ID : 421191.1 Complete checklist for manual upgrades of Oracle databases from anyversion to any version on any platform
Doc ID : 763386.1 Requirements for Installing Oracle 11gR2 32-bit on SLES 10 (x86)
Doc ID : 785351.1 Oracle 11gR2 Upgrade Companion
Doc ID : 837570.1 Complete Checklist for Manual Upgrades to 11gR2
Doc ID : 851598.1 Linux OS Requirements Reference List for Database Server
Doc ID : 870814.1 Complete checklist to upgrade the database to 11gR2 using DBUA
Doc ID : 871665.1 How To Restore The Database To The Source Release After Upgrading The Database To Oracle 11gR2 Using DBUA
Doc ID : 880211.1 Requirements for Installing Oracle 11gR2 RDBMS on RHEL (and OEL) 4 x86
Doc ID : 870877.1 How To Save Oracle Enterprise Manager Database Control Data Before Upgrading The Single Instance Database To Other Release ?
Doc ID : 880936.1 Requirements for Installing Oracle 11gR2 RDBMS on RHEL (and OEL) 5 on 32-bit x86
Doc ID : 881025.1 Requirements for Installing Oracle 11gR2 32-bit on SLES 11 (x86)
Doc ID : 881044.1 Requirements for Installing Oracle 11gR2 64-bit (AMD64/EM64T) on SLES 11
Doc ID : 883299.1 Oracle 11gR2 Relink New Feature
Doc ID : 883335.1 How to downgrade from 11.2 to previous release.
Doc ID : 883743.1 How To Deinstall Oracle Home In 11gR2 ?
Doc ID : 884232.1 11gR2 Install (Non-RAC): Understanding New Changes With All New 11.2 Installer
Doc ID : 884282.1 “Grid infrastructure” is not Grid Control 11gR2 (11.2.0.1.0)
Doc ID : 884435.1 Requirements for Installing Oracle 11gR2 64-bit (AMD64/EM64T) on SLES 10
Doc ID : 886749.1 Oracle 11gR2 Deinstall And Deconfig Tool Options
Doc ID : 886807.1 How To Create A Parameter File For Silent Deinstallation Of Oracle 11gR2
Doc ID : 948040.1 How To Rename A Diskgroup On ASM 11gR2?
Doc ID : 954552.1 11.2 Oracle Restart cannot manage 10.1.x single instances
Doc ID : 886407.1 ACFS/ADVM is NOT started automatically after node reboot or after CRS is restarted in non-RAC environment
Doc ID : 887658.1 Reconfigure HAS and CSS for nonRAC ASM on 11.2
Doc ID : 947520.1 AFTER NODE REBOOT CSSD IS NOT STARTED IN 11gR2 Non-RAC
Doc ID : 942166.1 How to Proceed from Failed 11gR2 Grid Infrastructure (CRS) Installation
Doc ID : 948456.1 Pre-11.2 Database Issues in 11gR2 Grid Infrastructure Environment
Doc ID : 975457.1 How to Troubleshoot Connectivity Issue with 11gR2 SCAN Name
Doc ID : 887962.1 11gR2 New Feature: Deferred Segment Creation

Thursday, September 6, 2012

Dataguard Administration

SELECT 'Last Applied : ' Logs,
TO_CHAR(next_time,'DD-MON-YY:HH24:MI:SS') TIME,thread#,sequence#
FROM v$archived_log
WHERE sequence# =
(SELECT MAX(sequence#) FROM v$archived_log WHERE applied='YES'
)
UNION
SELECT 'Last Received : ' Logs,
TO_CHAR(next_time,'DD-MON-YY:HH24:MI:SS') TIME,thread#,sequence#
FROM v$archived_log
WHERE sequence# =
(SELECT MAX(sequence#) FROM v$archived_log );

-- Check that Archive Logs are being Shipped
-- This query needs to be run on the Primary database

SET PAGESIZE 124
COL DB_NAME FORMAT A8
COL HOSTNAME FORMAT A12
COL LOG_ARCHIVED FORMAT 999999
COL LOG_APPLIED FORMAT 999999
COL LOG_GAP FORMAT 9999
COL APPLIED_TIME FORMAT A12
SELECT DB_NAME, HOSTNAME, LOG_ARCHIVED, LOG_APPLIED,APPLIED_TIME,
LOG_ARCHIVED-LOG_APPLIED LOG_GAP
FROM
(
SELECT NAME DB_NAME
FROM V$DATABASE
),
(
SELECT UPPER(SUBSTR(HOST_NAME,1,(DECODE(INSTR(HOST_NAME,'.'),0,LENGTH(HOST_NAME),
(INSTR(HOST_NAME,'.')-1))))) HOSTNAME
FROM V$INSTANCE
),
(
SELECT MAX(SEQUENCE#) LOG_ARCHIVED
FROM V$ARCHIVED_LOG WHERE DEST_ID=1 AND ARCHIVED='YES'
),
(
SELECT MAX(SEQUENCE#) LOG_APPLIED
FROM V$ARCHIVED_LOG WHERE DEST_ID=2 AND APPLIED='YES'
),
(
SELECT TO_CHAR(MAX(COMPLETION_TIME),'DD-MON/HH24:MI') APPLIED_TIME
FROM V$ARCHIVED_LOG WHERE DEST_ID=2 AND APPLIED='YES'
);



Find archive generation over last one month


SELECT  TRUNC(FIRST_TIME)
,       COUNT(*)
FROM    V$ARCHIVED_LOG
WHERE   FIRST_TIME BETWEEN ADD_MONTHS(SYSDATE,-1) AND SYSDATE
AND   DEST_ID = 1
GROUP BY TRUNC(FIRST_TIME)
ORDER BY 1

To check the gap history (last 24 hours) in DR run the below query:


select thread#, sequence#, to_char(next_time,'DD-HH24:MI'), to_char(completion_time,'DD-HH24:MI'), round((completion_time-next_time)*24*60) as delta_minutes from v$archived_log where completion_time>SYSDATE-1 and (completion_time-next_time)*24*60>30;


Run the primary and see the any Gap in the sequence

select thread#, applied, max(sequence#) from gv$archived_log group by thread#, applied order by thread#, applied desc;

Last applied and Last received status

select 'Last applied : ' Logs, to_char(next_time,'DD-MON-YY:HH24:MI:SS') Time from v$archived_log 
 where sequence# = (select max(sequence#) from v$archived_log where applied='YES') union select 'Last received : 
' Logs, to_char(next_time,'DD-MON-YY:HH24:MI:SS') Time from v$archived_log where sequence# = (select max(sequence#) from v$archived_log);


- Verify that the last sequence# received and the last sequence# applied to standby
-- database.


select al.thrd "Thread", almax "Last Seq Received", lhmax "Last Seq Applied"
from (select thread# thrd, max(sequence#) almax
      from v$archived_log
      where resetlogs_change#=(select resetlogs_change# from v$database)
      group by thread#) al,
     (select thread# thrd, max(sequence#) lhmax
      from v$log_history
      where first_time=(select max(first_time) from v$log_history)
      group by thread#) lh
where al.thrd = lh.thrd;

The following query
can be used on the primary database to identify peak archive times for a specified day:


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)

Archive log gaps can be monitored by examining the low and high sequence numbers in the
V$ARCHIVE_GAP view, as shown here:


SELECT THREAD#, LOW_SEQUENCE#, HIGH_SEQUENCE#
FROM V$ARCHIVE_GAP;

When you detect a redo transport delay, execute the following query on the primary database
to identify archive logs that have not made it to the standby destination:


SELECT L.THREAD#, L.SEQUENCE#
FROM
(SELECT THREAD#, SEQUENCE#
FROM V$ARCHIVED_LOG
WHERE DEST_ID=1) L
WHERE L.SEQUENCE# NOT IN
(SELECT SEQUENCE#
FROM V$ARCHIVED_LOG
WHERE DEST_ID=2
AND THREAD# = L.THREAD#);

Archive log genarating per hour

select day,  MAX(DECODE(thread#, 1, switches_per_thread, NULL)) thread_1,  MAX(DECODE(thread#, 2, switches_per_thread, NULL)) thread_2,MAX(DECODE(thread#, 3, switches_per_thread, NULL)) thread_3 from (select trunc(first_time) day, thread#, sum(1) switches_per_thread from v$log_history group by trunc(first_time), thread#) group by day order by 1

how to calculate the amount of redo per day and thread.  To find out, use this query:

SELECT TRUNC(first_time),  thread#,  ROUND(SUM(blocks * block_size) / 1024/ 1024,2) m FROM v$archived_log WHERE dest_id = 1 GROUP BY TRUNC(first_time),  thread #ORDER BY 1

standby logs are being used by running following query :



set lines 155 pages 9999
col thread# for 9999990
col sequence# for 999999990
col grp for 990
col fnm for a50 head "File Name"
col "Fisrt SCN Number" for 999999999999990
break on thread
# skip 1
select a.thread#
,a.sequence#
,a.group# grp     
, a.bytes/1024/1024 Size_MB     
,a.status     
,a.archived     
,a.first_change# "First SCN Number"     
,to_char(FIRST_TIME,'DD-Mon-RR HH24:MI:SS') "First SCN Time"   
,to_char(LAST_TIME,'DD-Mon-RR HH24:MI:SS') "Last SCN Time"  from
 v$standby_log a  order by 1,2,3,4
 /



column name format a45 trunc
set lines 300
set pages 999
set trimspool on
column dest_id format 9999
column thread# format 9999
alter session set nls_date_format='dd-mon-yy hh24:mi:ss';
select sequence#, name, dest_id, thread#, first_time,
round(sum((blocks*block_size/1024/1024))) mbytes
from v$archived_log
where first_time >=trunc(sysdate-3)
group by sequence#, name, dest_id, thread#, first_time
order by sequence# desc, dest_id asc;



PROMPT
PROMPT
PROMPT Run on Standby Database. This script checks last log applied and last log received time
PROMPT
PROMPT
col time format a40
select 'Last applied  : ' Logs, to_char(next_time,'DD-MON-YY:HH24:MI:SS') Time
    from v$archived_log
    where sequence# = (select max(sequence#) from v$archived_log where applied='YES')
    union
    select 'Last received : ' Logs, to_char(next_time,'DD-MON-YY:HH24:MI:SS') Time
    from v$archived_log
   where sequence# = (select max(sequence#) from v$archived_log);


PROMPT last sequence# received and the last sequence# applied to standby database.
PROMPT
select al.thrd "Thread", almax "Last Seq Received", lhmax "Last Seq Applied"
from (select thread# thrd, max(sequence#) almax
from v$archived_log
where resetlogs_change#=(select resetlogs_change# from v$database)
group by thread#) al,
(select thread# thrd, max(sequence#) lhmax
from v$log_history
where first_time=(select max(first_time) from v$log_history)
group by thread#) lh
where al.thrd = lh.thrd;




SELECT ARCH.THREAD# "Thread", ARCH.SEQUENCE# "Last Sequence Received", APPL.SEQUENCE# "Last Sequence Applied", (ARCH.SEQUENCE# - APPL.SEQUENCE#) "Difference"
FROM
(SELECT THREAD# ,SEQUENCE# FROM V$ARCHIVED_LOG WHERE (THREAD#,FIRST_TIME ) IN (SELECT THREAD#,MAX(FIRST_TIME) FROM V$ARCHIVED_LOG GROUP BY THREAD#)) ARCH,
(SELECT THREAD# ,SEQUENCE# FROM V$LOG_HISTORY WHERE (THREAD#,FIRST_TIME ) IN (SELECT THREAD#,MAX(FIRST_TIME) FROM V$LOG_HISTORY GROUP BY THREAD#)) APPL
WHERE
ARCH.THREAD# = APPL.THREAD#
ORDER BY 1;

EManager scripts to monitor

EManager scripts to monitor

set linesize 200
col BEGIN_INTERVAL_TIME format a70
select * from (select snap_id,begin_interval_time from dba_hist_snapshot order by begin_interval_time desc) where rownum < 3;
---------------------------------------------
Set pages 1000
Set lines 75
Select a.execution_end, b.type, b.impact, d.rank, d.type,
'Message           : '||b.message MESSAGE,
'Command To correct: '||c.command COMMAND,
'Action Message    : '||c.message ACTION_MESSAGE
From dba_advisor_tasks a, dba_advisor_findings b,
Dba_advisor_actions c, dba_advisor_recommendations d
Where a.owner=b.owner and a.task_id=b.task_id
And b.task_id=d.task_id and b.finding_id=d.finding_id
And a.task_id=c.task_id and d.rec_id=c.rec_Id
And a.task_name like 'ADDM%' and a.status='COMPLETED'
and a.description like '%4782%'
Order by b.impact, d.rank;

----------------------------------------------------------- cat

get_addm_report.sql which gets each task from the last snapshot from dba_advisor_tasks

set long  10000000
set pagesize 50000
column get_clob format a80

select dbms_advisor.get_task_report (task_name) as ADDM_report
from dba_advisor_tasks
where task_id = (
        select max(t. task_id)
        from dba_advisor_tasks t, dba_advisor_log l
        where t.task_id = l.task_id
        and t.advisor_name = 'ADDM'
        and l.status = 'COMPLETED');


---------------------------------------------------------------
export ORACLE_SID=$1
export ORAENV_ASK=NO
. oraenv
sqlplus -S / as sysdba <<oogy
set echo off
set lines 100
set pages 200
set trimspool on
set termout off
set feedback off

column dcol new_value mydate noprint
select to_char(sysdate,'YYMMDD') dcol from dual;

spool /home/oraprd/scripts/dbreport_$1_&mydate..txt
ttitle 'Average Active Sessions in the last week: Instance $1'
column sample_hour format a16
select
   to_char(round(sub1.sample_time, 'HH24'), 'YYYY-MM-DD HH24:MI') as sample_hour
,    round(avg(sub1.on_cpu),1) as cpu_avg
,    round(avg(sub1.waiting),1) as wait_avg
,    round(avg(sub1.active_sessions),1) as act_avg
,    round( (variance(sub1.active_sessions)/avg(sub1.active_sessions)),1) as act_var_mean
from
   ( -- sub1: one row per second, the resolution of SAMPLE_TIME
     select
        sample_id,
        sample_time,
        sum(decode(session_state, 'ON CPU', 1, 0))  as on_cpu,
        sum(decode(session_state, 'WAITING', 1, 0)) as waiting,
        count(*) as active_sessions
     from
        dba_hist_active_sess_history
     where
        sample_time > sysdate - 7
     group by
        sample_id,
        sample_time
   ) sub1
group by
   round(sub1.sample_time, 'HH24')
order by
   round(sub1.sample_time, 'HH24');


ttitle 'Most expensive queries in the last week: Instance $1'
-- gets most expensive queries
-- (by time spent, change "order by" to use another metric)
-- after a specific date
select
   sub.sql_id,
   sub.seconds_used,
   sub.executions,
   sub.gets
from
   ( -- sub to sort before rownum
     select
        sql_id,
        round(sum(elapsed_time_delta)/1000000) as seconds_used,
        sum(executions_delta) as executions,
        sum(buffer_gets_delta) as gets
     from
        dba_hist_snapshot natural join dba_hist_sqlstat
     where
        begin_interval_time > sysdate - 7
     group by
        sql_id
     order by
        2 desc
   ) sub
where
   rownum < 30
;


set long 32768
ttitle 'Text for most expensive SQL in the last week: Instance $1'
select sql_text
from dba_hist_sqltext
where sql_id =
(
select sub.sql_id
from
   ( -- sub to sort before rownum
     select
        sql_id,
        round(sum(elapsed_time_delta)/1000000) as seconds_since_date,
        sum(executions_delta) as execs_since_date,
        sum(buffer_gets_delta) as gets_since_date
     from
        dba_hist_snapshot natural join dba_hist_sqlstat
     where
        begin_interval_time > sysdate - 7
     group by
        sql_id
     order by
        2 desc
   ) sub
where
   rownum = 1
);

spool off;
exit

 

User Privilege

User Privilege

SELECT USERNAME, ACCOUNT_STATUS,CREATED,profile FROM DBA_USERS WHERE USERNAME ='&A';

SELECT USERNAME, ACCOUNT_STATUS,CREATED FROM DBA_USERS WHERE USERNAME LIKE '%&A%';

SELECT GRANTEE, GRANTED_ROLE FROM DBA_ROLE_PRIVS WHERE GRANTEE='&A';
select owner,table_name, privilege from dba_tab_privs where grantee='&a';

select * from dba_sys_privs where grantee='&A';

SELECT * FROM DBA_SYS_PRIVS WHERE GRANTEE='&A';


SELECT DISTINCT GRANTEE,OWNER,TABLE_NAME, PRIVILEGE FROM DBA_TAB_PRIVS
WHERE OWNER='NORSNAPADM' AND GRANTEE LIKE 'SELECT_%_%'
AND TABLE_NAME IN ('DEALER_PROFILE');

------------------------------------------------------
select * from dba_role_privs
where granted_role in ('ADMIN_ROLE','CONNECT','RESOURCE') and
grantee not in ('SYS','SYSTEM');

Log on  in trigger

CREATE OR REPLACE TRIGGER sysadm_logon_trg_jay
after logon on Raj.schema
BEGIN
  EXECUTE IMMEDIATE
          'declare begin ' ||
          'dbms_application_info.set_client_info ( 101 ); end;';
  EXECUTE IMMEDIATE 'ALTER SESSION SET CURRENT_SCHEMA =Scott';
end;
/



select * from DBA_SYS_PRIVS -- users

select * from ROLE_SYS_PRIVS -- roles

select * from DBA_TAB_PRIVS -- obj


System Privileges:
select * from DBA_SYS_PRIVS where GRANTEE='USERNAME';
Object Privileges:
select * from DBA_TAB_PRIVS where GRANTEE='USERNAME'


select
     lpad(' ', 2*level) || granted_role "User, his roles and privileges"
    from
     (
     /* THE USERS */
       select
        null     grantee,
        username granted_role
        from
         dba_users
      where
         username like upper('%&enter_username%')
   /* THE ROLES TO ROLES RELATIONS */
     union
       select
         grantee,
        granted_role
         from
        dba_role_privs
    /* THE ROLES TO PRIVILEGE RELATIONS */
      union
       select
        grantee,
          privilege
      from
        dba_sys_privs
    )
   start with grantee is null
  connect by grantee = prior granted_role



select grantee, granted_role
from dba_role_privs drp
where drp.grantee in (select username from dba_users)
order by granted_role


Script: To clone or Duplicate Oracle User

select username from dba_users order by username
/
undefine user
accept userid prompt 'Enter user to clone: '
accept newuser prompt 'Enter new username: '
accept passwd prompt 'Enter new password: '
select username, created from dba_users
where  lower(username) = lower('&newuser')
/
accept poo prompt 'Continue? (ctrl-c to exit)'
spool D:\TEST\clone.sql
select 'create user ' || '&newuser' || ' identified by ' || '&passwd' ||
       ' default tablespace ' || default_tablespace ||
       ' temporary tablespace ' || temporary_tablespace || ';' "user"
from   dba_users
where  username = '&userid'
/
select 'alter user &newuser quota '||
       decode(max_bytes, -1, 'unlimited', ceil(max_bytes / 1024 / 1024) || 'M') ||
       ' on ' || tablespace_name || ';'
from   dba_ts_quotas where username = '&&userid'
/
select 'grant ' ||granted_role || ' to &newuser' ||
       decode(admin_option, 'NO', ';', 'YES', ' with admin option;') "ROLE"
from   dba_role_privs where  grantee = '&&userid'
/
select 'grant ' || privilege || ' to &newuser' ||
       decode(admin_option, 'NO', ';', 'YES', ' with admin option;') "PRIV"
from   dba_sys_privs where  grantee = '&&userid'
/
spool off
undefine user
set verify on
set feedback on
set heading on
------------------------------------------------------------------------------------------------------------------------------------------------------
SQL>@D:\TEST\clone.sql

ASM Administration

ASM Administration

select path, disk_number, mount_status, header_status, mode_status, state, name,label from v$asm_disk
Select mount_status, header_status, mode_status, state, total_mb, free_mb, name, path, create_date from v$asm_disk
set wrap off
set lines 120
set pages 999
col “Group Name”   form a25
col “Disk Name”    form a30
col “State”  form a15
col “Type”   form a7
col “Free GB”   form 9,999
 select group_number  "Group",  name "Group Name",state  "State" ,type  "Type",total_mb/1024 "Total GB" , free_mb/1024  "Free GB" from   v$asm_diskgroup
 /
col “Instance” form a8
 select c.group_number  "Group"
 ,      g.name          "Group Name"
 ,      c.instance_name "Instance"
 from   v$asm_client c
 ,      v$asm_diskgroup g
 where  g.group_number=c.group_number

/


select *
from   v$asm_operation
/
select header_status , mode_status, path from V$asm_disk where header_status in ('FORMER','CANDIDATE');

/
ol “Instance” form a8
 select c.group_number  "Group"
 ,      g.name          "Group Name"
 ,      c.instance_name "Instance"
 from   v$asm_client c
 ,      v$asm_diskgroup g
 where  g.group_number=c.group_number

/


select *
from   v$asm_operation
/
select header_status , mode_status, path from V$asm_disk where header_status in ('FORMER','CANDIDATE');

/
col "Group"          form 999
 col "Disk"           form 999
 col "Header"         form a9
 col "Mode"           form a8
 col "Redundancy"     form a10
 col "Failure Group"  form a10
 col "Path"           form a19
 
select group_number  "Group"
 ,      disk_number   "Disk"
 ,      header_status "Header"
 ,      mode_status   "Mode"
 ,      state         "State"
 ,      redundancy    "Redundancy"
 ,      total_mb      "Total MB"
 ,      free_mb       "Free MB"
 ,      name          "Disk Name"
 ,      failgroup     "Failure Group"
 ,      path          "Path"
 from   v$asm_disk  order by group_number,disk_number



spool asm1.html 
SET MARKUP HTML ON 
set echo on 
set pagesize 200 
alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS'; 
select 'THIS ASM REPORT WAS GENERATED AT: ==)> ' , sysdate " " from dual; 
select 'HOSTNAME ASSOCIATED WITH THIS ASM INSTANCE: ==)> ' , MACHINE " " from v$session where program like '%SMON%'; 
select * from v$asm_diskgroup; 
SELECT * FROM V$ASM_DISK ORDER BY GROUP_NUMBER,DISK_NUMBER; 
SELECT * FROM V$ASM_CLIENT; 
select * from V$ASM_ATTRIBUTE; 
select * from v$asm_operation; 
select * from gv$asm_operation; 
select * from v$version; 
show parameter asm 
show parameter cluster 
show parameter instance_type 
show parameter instance_name 
show parameter spfile 
show sga 
spool off 
exit 


sqlplus "/ as sysasm" 

spool /tmp/asm.out 
set pages 1000 
set linesize 160 
col path format a45 
col name format a35 
select group_number,disk_number,name,path,state,header_status,mode_status,mount_status from v$asm_disk order by 1,2,3; 
select failgroup,header_status,count(*) from v$asm_disk group by failgroup,header_status order by 1,2; 
spool off 

About Me

My photo
Sydney, NSW, Australia
An experienced IT professional (14+ years) worked in multiple industries such as consulting, education, financial, retail sectors. Carries good work experience in relational database design, analysis, modeling, development, administration, implementation, trouble shooting, support etc. Experienced in Oracle/SQL Server/MySQL DBA involving setup, configuration, tuning, backups, disaster recovery, high availability Oracle 11g/12C/19C RAC clusters; SQL server 2008/2012/2016 clusters, Oracle Engineered Systems such as EXADATA, ODA and Oracle and Azure Cloud. Performed Software Installations, Migrations, Database Capacity Planning, Automation of backup implementation, Cloud migration Tuning Oracle in windows/Unix platforms. In addition, experienced in UNIX administration, Shell scripting. PowerShell scripting A team player with communication skills.