#!/bin/bash
function PreChks
{
ORACLE_HOME=/u01/app/oracle/product/12.1.0.2
if [[ ! -e $ORACLE_HOME ]]; then
echo " "
echo "Error ::: Given ORACLE_HOME :: $ORACLE_HOME does not exist!!! "
echo " "
exit;
fi
ORACLE_SID=mvmrp1
export PATH=$ORACLE_HOME/bin:$PATH:.
export ORACLE_BASE ORACLE_HOME PATH ORACLE_SID
LOGFILE=/tmp/main.log
echo "Task Start at `date` " > $LOGFILE
export MAIL_RECIPIENT_INFO='ktexperts@admin.com'
}
function get_objects
{
echo "#CHECK FOR DATABASE and Tables STATUS##">>"$LOGFILE"
DB_OPEN_MODE=`sqlplus -s / as sysdba <<EOF
set pagesize 0 feedback off verify off heading off echo off
select open_mode from v\\$database;
EOF`
echo "$DB_OPEN_MODE">>"$LOGFILE"
if [[ $DB_OPEN_MODE = "READ WRITE" ]]
then
echo "Fetch Stale_Stats Data"
sqlplus -s / as sysdba <<EOF > /tmp/a.log
set pagesize 0 feedback off verify off heading off echo off
set linesize 300
select table_name, owner from dba_tab_statistics where stale_stats='YES' and owner in ('MVM1') order by table_name;
EOF
else
##DATABASE IS NOT OPEN##
mail_subject="Database $INSTANCE is not open hence Gather Stats cant not Start...."
echo " "
echo "Database $INSTANCE is not open hence Gather Stats cant not Start...."
echo " "
mailx -s "${mail_subject}" ${MAIL_RECIPIENT_INFO}<<EOF
" Database $INSTANCE is not open hence Gather Stats cant not Start...."
EOF
exit 0
fi
cat /tmp/a.log >> /tmp/main.log
}
function gather_stats
{
cat /tmp/a.log | while read p
do
tbl=`echo $p | awk '{ print $1 }'`
owner=`echo $p | awk '{ print $2 }'`
SQL=`echo "Exec dbms_stats.gather_table_stats('$owner','$tbl',ESTIMATE_PERCENT=>dbms_stats.auto_sample_size,METHOD_OPT=>'for all indexed columns size 1',CASCADE=>True,degree=> 4);"`
echo $SQL >> /tmp/main.log
sqlplus -s / as sysdba <<EOF >> /tmp/main.log
Exec dbms_stats.gather_table_stats('$owner','$tbl',ESTIMATE_PERCENT=>dbms_stats.auto_sample_size,METHOD_OPT=>'for all indexed columns size 1',CASCADE=>True,degree=> 4);
EOF
done
}
PreChks
get_objects
gather_stats
get_objects
mail_subject="mvmrp1 Stale Stats Report"
cat "$LOGFILE" | mailx -s "${mail_subject}" ${MAIL_RECIPIENT_INFO}