Translate into your own language

Sunday, July 22, 2018

Query to find Active SQL’s in database

set feedback off
set serveroutput on size 9999
column username format a20
column sql_text format a55 word_wrapped
begin
for x in
(select username||'(‘||sid||’,’||serial#||’) ospid = ‘|| process ||
‘ program = ‘ || program username,
to_char(LOGON_TIME,’ Day HH24:MI’) logon_time,
to_char(sysdate,’ Day HH24:MI’) current_time,
sql_address,
sql_hash_value
from v$session
where status = ‘ACTIVE’
and rawtohex(sql_address) <> ’00’
and username is not null ) loop
for y in (select sql_text
from v$sqlarea
where address = x.sql_address ) loop
if ( y.sql_text not like ‘%listener.get_cmd%’ and
y.sql_text not like ‘%RAWTOHEX(SQL_ADDRESS)%’ ) then
dbms_output.put_line( ‘——————–‘ );
dbms_output.put_line( x.username );
dbms_output.put_line( x.logon_time || ‘ ‘ || x.current_time || ‘ SQL#=’ || x.sql_hash_value);
dbms_output.put_line( substr( y.sql_text, 1, 250 ) );
end if;
end loop;
end loop;
end;


/

Script to find redo generated by current sessions

#################################################
redo generated by current sessions
#################################################
select v$session.sid, username, value redo_size
from v$sesstat, v$statname, v$session
where v$sesstat.STATISTIC# = v$statname.STATISTIC#
and v$session.sid = v$sesstat.sid
and name = ‘redo size’
and value > 0
and username is not null
order by value
/

How to find in which datafile object resides

Lets say i created a new tablespace with 8 datafiles.
SQL> select file_name from dba_data_files where tablespace_name=’TESTNDEX’;
FILE_NAME
——————————————————————————–
/data1/oradata/TESTINDEX01.dbf
/data1/oradata/TESTINDEX02.dbf
/data1/oradata/TESTINDEX03.dbf
/data1/oradata/TESTINDEX04.dbf
/data1/oradata/TESTINDEX05.dbf
/data1/oradata/TESTINDEX06.dbf
/data1/oradata/TESTINDEX07.dbf
/data1/oradata/TESTINDEX08.dbf
8 rows selected.
i created a table called TEST in this tablespace.
now many people think that space will be allocated (or extents will be allocated) from 1st datafile of this tablespace and once it is full, it will use 2nd, 3rd etc….which is not the real picture
the fact is, if i create a table, when i start inserting data into that, extents will be allocated from all the datafiles in ROUND ROBIN fashion (if you don’t know about this, just do a google !)
So, in our example it will go to all the 8 datafiles
so based on the allocation of extents and size of data, object can reside in all the datafiles or some datafiles.
How can we check in which datafile my object resides?
select a.segment_name,a.file_id,b.file_name Datafile_name from dba_extents a,dba_data_files b where a.file_id=b.file_id and a.segment_name=’YOUR OBJECT NAME’;
How can we check what objects are there in a datafile?
select a.segment_name,a.file_id,b.file_name Datafile_name from dba_extents a,dba_data_files b where a.file_id=b.file_id and b.file_name=<your datafile name with path>;

Script for trimming alert log to 1 day and taking backup of prev day



#!/usr/bin/ksh

############################################################################
##  Program :   save_alert_log.sh                                          #
##                                                                         #
##  Purpose :   The alert logs on many Oracle databases can grow to a very #
##              large size over time.  This can often impede the maintenace#
##              of the system – because the DBA will need to sometimes scan#
##              through many days or months of data when researching an    #
##              issue.  This script tries to avoid that by ensuring that   #
##              the log file can be “refreshed” on a daily basis, meaning  #
##              that only the current day’s data will be kept in the log,  #
##              while the previous day’s data will be saved to another file#
##              in a backup area.                                          #
##                                                                         #
##              This script should be run from Oracle’s crontab at midnight#
##              every night, so that the database will always have a new   #
##              alert log file each day.  An example crontab entry could be#
##              0 00 * * * /oracle/product/local/scripts/save_alert.sh 2>&1#
##                                                                         #
##  Date    :   19 May 2006.                                               #
##  Author  :   Basil S. Mullings                                          #
############################################################################
##  Modified:                                                              #
##                                                                         #
##                                                                         #
#  Modification History:                                                   #
#  DATE       WHO      DESC                                                #
#  ——–   —–    —————————————————-#
#  05/29/06   Basil    Add an extra variable LOG_KEEP_DAYS to hold the     #
#                      number of days that the log files should be kept on #
##                     the server before being deleted.                    #
##                                                                         #
##                                                                         #
############################################################################

  ##Setup some needed variables.
BKUP=bkup   ##The backup directory to store the logs…
ORATAB=”/etc/oratab”
LOG_KEEP_DAYS=365   ##Keep this many days of log files on the server.
TMPFILE=/var/tmp/OracleAlertLog   ##Just a temp scratch work area.
SQLUSER=”/ as sysdba”
GEN_ORA_ERROR=”ORA\-[0-9][0-9]*”
PATH=”$HOME:$HOME/bin:/usr/contrib/bin:/usr/local/bin:/usr/bin:/bin:/etc:.”
export PATH

  ## Now, parse the oratab file for all databases on the system.
  ## Then use the ORACLE_SID that is found in the oratab file
  ## to log onto that database, and retrieve the directory where
  ## the alter log file is stored (.ie. retrieve the path to the
  ## bdump directory.
  ##
#for sidEntry in `cat $ORATAB | grep -v “^#”`
for sidEntry in `cat $ORATAB | awk -F: ‘{print $1}’ | grep -v “^#”`
do
       ## Get date and time
    CURR_DATE=`date ‘+%a_%m%d%H%M’`    ##Example Fri_05191256   for Friday May 19th @1256 PM.

    #ORACLE_SID=`echo  $sidEntry | cut -f 1 -d :`
    ORACLE_SID=$sidEntry
    echo “Oracle Sid is $ORACLE_SID”
                                                                                        
    export ORACLE_SID                                                                   
       ## Set the Oracle environment for this SID.                                      
    ORAENV_ASK=NO                                                                     
    . /usr/local/bin/oraenv                                                             
    rm -f $TMPFILE > /dev/null 2>&1

       ##Now, let’s log onto the DB, and try to
       ##retrieve the bdump directory path.
    sqlplus -s /nolog << EOF > $TMPFILE
    connect $SQLUSER
    set heading off;
    set echo off;
    set feedback off;

    select ‘BACKGROUND_DUMP_DEST=’ ||value
    from   v\$parameter
    where  name=’background_dump_dest’;
    exit;
EOF

       ##Ok, we had a problem talking to the database.
    if [ `grep -c $GEN_ORA_ERROR $TMPFILE` -ne 0 ]
    then
         echo “ERROR: Unable to find the path to the alert log for DB $ORACLE_SID”
         rm -f $TMPFILE > /dev/null 2>&1

    else  ##Ok, we can log into the DB, now let’s go find our bdump directory.
      
         bdump=`grep BACKGROUND_DUMP_DEST $TMPFILE | awk -F “=” ‘{print $2}’`
         #echo “BDUMP is $bdump”
         bkupDir=$bdump/$BKUP

            ##Make sure our backup directory exists.
         if [ ! -d $bkupDir ]
         then
               mkdir $bkupDir  > /dev/null 2>&1
         fi

           ##Now, move the alert log.                                                 
         #echo “now moving $bdump/alert_${ORACLE_SID}.log to $bkupDir/alert_${ORACLE_SID}.$CURR_DATE”
         mv $bdump/alert_${ORACLE_SID}.log  $bkupDir/alert_${ORACLE_SID}.$CURR_DATE
       
             #Procedure to shrink the log to 365 days
             ##Keep only the last 365 days worth of logs…delete all logs older than 365 days.
         #echo “Now shrinking the logs in dir $bkupDir …”
         find $bkupDir  -name “*.*” -mtime +${LOG_KEEP_DAYS} -exec rm -f {} \;
    fi

done

Script to check space for one mount point

##########################################
SPACE CHECK FOR A MOUNT POINT IN UNIX BOX
###########################################
#!/bin/ksh
lis=`df -k /d801/oracle/SDSS | grep % | awk ‘{print $5}’| sed ‘s/%//g’`
ci=80
SCRIPT_DIR=/d002/oracle/SDSS/utilitys/
CTIME=`date +%HH%MM%SS`
if test $lis -gt $ci
then
( echo “$lis% space is used in /d801/oracle/SDSS/” > /d002/oracle/SDSS/utilitys/spacecheck.txt
mailx -s “Archive Destination > 80% in /d801/oracle/SDSS/!!” dba_ora_offshore@bp.com 919949520316@airtelap.com < /d002/oracle/
SDSS/utilitys/spacecheck.txt
)
fi
Note : The above script is only for one mount point

Script to check mount point space and get email

######################################
SCRIPT FOR CHECKING DISK SPACE IN UNIX
######################################
 #!/bin/ksh
#rm /tmp/dfk.txt
#echo “df -k output for `date` `uname -n`” > /tmp/dfk.txt
echo “File system usage exceeded the threshold on `uname -n` server- `date`” > /tmp/dfk.txt
echo “” >> /tmp/dfk.txt
i=1
while [ $i -le `df -k | grep -v proc | grep -v capacity | wc -l` ] ;do
if [ `df -k | grep -v proc | grep -v capacity | head -n $i | tail -1 | awk ‘{print $5}’ | \
sed -e ‘s/%//’` -gt 97 ] ; then
#echo “File system usage exceeded the threshold on `uname -n` server- `date`” > /tmp/dfk.txt
#echo “” >> /tmp/dfk.txt
df -k | grep -v proc | grep -v capacity | head -n $i | tail -1 >> /tmp/dfk.txt
fi
((i=i+1))
done
if [ `cat /tmp/dfk.txt | wc -l` -gt 2 ] ; then
cat /tmp/dfk.txt | mailx -s “File system full alert” bp_hostsupport@satyam.com,dba_ora_offshore@satyam.com
#cat /tmp/dfk.txt
else
exit
fi
Note: the above script will check for mount point space and will send an alert if the used space is > 97%

Script for tablespace free space email alert

#####################################################################################
MONITOR SCRIPT – it will monitor objects close to max extents, tablespace free space
#####################################################################################
#! /bin/sh
TS=`date “+%y%m%d”`
ORACLE_HOME=/d001/oracle/9.2.0.8-64
LD_LIBRARY_PATH=$ORACLE_HOME/lib
ORACLE_TERM=vt100
ORACLE_SID=ORVIT8QA
LOG=/d002/oracle/$ORACLE_SID/utilitys
PATH=/$ORACLE_HOME/bin:$PATH
export ORACLE_HOME ORACLE_SID ORACLE_TERM PATH LOG LD_LIBRARY_PATH
for n in 1
do
if [ $n -eq 1 ]; then
TWO_TASK=brcvita8_ORVIT8QA
DB_NAME=ORVIT8QA
fi
export TWO_TASK
monitor_file=$LOG/$DB_NAME.log
#n=`expr $n + 1`
 rm $monitor_file
echo “Objects close to max extents:” >> $monitor_file
sqlplus -s system/systemorvit8QA << eof >> $monitor_file
set pagesize 100;
set linesize 80;
column segment_name format a35;
column type format a10;
column owner format a10;
column extents format 9999;
column max_extents format 9999;
select owner, segment_name, segment_type “TYPE”, extents, max_extents
from sys.dba_segments
where extents/greatest(max_extents,1) >= 0.90
and segment_type not in(‘CACHE’,’ROLLBACK’);
exit;
eof
 echo “Objects that cannot get next extent:” >> $monitor_file
sqlplus -s system/systemorvit8QA << eof >> $monitor_file
set pagesize 100;
set linesize 80;
column segment_name format a33;
column type format a8;
column owner format a10;
column tablespace format a13;
column next_extent format 999999999;
select owner, segment_name, segment_type “TYPE”,
tablespace_name “TABLESPACE”, next_extent
from sys.dba_segments b
where segment_type !=’ROLLBACK’ and
next_extent > (select max(bytes) from sys.dba_free_space a
where a.tablespace_name = b.tablespace_name);
exit;
eof
 echo “Tablespaces with low freespace (< 20% & < 250 MB):” >> $monitor_file
sqlplus -s system/systemorvit8QA << eof >> $monitor_file
set echo off
set pagesize 30
set feedback off;
drop table system.mon_smt_ts;
create table system.mon_smt_ts as
(select x.tablespace_name, round(sum(x.bytes)/1048576,1) freespace
from sys.dba_free_space x
group by x.tablespace_name);
 drop table system.mon_smt_ts1;
create table system.mon_smt_ts1 as
(select tablespace_name, round(sum(bytes)/1048576,1) availspace
from sys.dba_data_files
group by tablespace_name);
set feedback on;
column “Table Space” format a20
column “Allocated” format a12
column “Free Space” format a12
column “Space Used” format a12
select x.tablespace_name “Table Space”,
to_char(availspace,’99999.9′)||’ Meg’ “Allocated”,
to_char(freespace,’99999.9′)||’ Meg’ “Free Space”,
round(freespace/availspace*100) “Pct Free”,
to_char((availspace-freespace),’99999.9′)||’ Meg’ “Space Used”
from system.mon_smt_ts x, system.mon_smt_ts1 y
where y.tablespace_name = x.tablespace_name
and round(freespace/availspace*100) < 20 and freespace<250
order by round(freespace/availspace*100);
exit;
eof
 null_findings=`egrep “no rows selected” $monitor_file | wc -l`
null_findings=`echo $null_findings`
 if [ “$null_findings” != 3 ]; then
/usr/bin/mailx -s “$DB_NAME Exception Report” dba_ora_offshore@bp.com < $monitor_file
fi
 done