Monday, 15 February 2016

Oracle : Evaluate Oracle Databases SQL's

Nothing better to do, try this:

SQL are running in the database, very hard to tell which ones could be benefited if all is hunky dory in the database.

DBA Passtime:

Grab sql_id that have run in the database, you could get from V$session or V$sql or any other view, my choice is the following in order to grab last hr or so run sql_id's :

SQL> SELECT a.sql_id
FROM gv$active_session_history a
WHERE a.sample_time > SYSDATE - 1/24
AND a.session_type = 'FOREGROUND'
GROUP BY a.sql_id
ORDER BY 1 DESC;  2    3    4    5    6

SQL_ID
-------------

g3f3cw3zy5aat
dcuks1btng04z
a8c7taz0kfnvg
8zc85a8249x81
8hbagg1z18dws
1aa2fpqtx557g
0raht0h154b63



create a script:
$vi autotune.sql <----Name your choice
set serverout on
set long 100000
set line 500
set pagesize 1000
DECLARE
  v_sql_task_id  VARCHAR2(100);
BEGIN
  v_sql_task_id := DBMS_SQLTUNE.create_analyse_task (
                          ---begin_snap  => 1,
                          ---end_snap    => 2,
                          sql_id      => 'SQLID',
                          scope       => DBMS_SQLTUNE.scope_comprehensive,
                          time_limit  => 300,
                          task_name   => 'SQLID_analyse_task',
                          description => 'analyse task for SQLID.');
END;
/
EXEC DBMS_SQLTUNE.execute_tuning_task(task_name => 'SQLID_analyse_task');
SET LONG 10000;
SET PAGESIZE 1000
SET LINESIZE 400
SELECT DBMS_SQLTUNE.report_tuning_task('SQLID_analyse_task') AS recommendations FROM dual;
exit;

Note: SQLID in above script is variable/placeholder for later substitution

you can run code for single sql_id as where bzv5d5qt53wjv is SQL_ID:
cat autotune.sql|sed "s/SQLID/bzv5d5qt53wjv/g"|sqlplus -s / as sysdba

or:

put sql_id's in a file say sql_id.txt

and 

cat sql_id.txt |while read LN
do
cat autotune.sql|sed "s/SQLID/${LN}/g"|sqlplus -s / as sysdba
done

Enjoy reading output, will keep you busy for long time.

Recommendations could be :
1. No recommendations
2. Create profile
3. Gather stats
4. Create index  

to name the few. 

Caution: Not all recommendation can be accepted blindly. Please use judgement or seek help from seniors.

Followup action(suggested only): Make sure to run following to avoid junk buildup.
 
exec DBMS_SQLTUNE.drop_tuning_task (task_name => 'SQLID_analyse_task');


cat sql_id.txt |while read LN
do
echo "exec DBMS_SQLTUNE.drop_tuning_task (task_name => '${LN}_analyse_task');"|sed "s/SQLID/${LN}/g"|sqlplus -s / as sysdba
done


ENJOY!!!!!!


Friday, 5 February 2016

Oracle Event:log file switch (checkpoint incomplete)

Event:

Top 10 Foreground Events by Total Wait Time
    EventWaitsTotal Wait Time (sec)Wait Avg(ms)% DB timeWait Class
    DB CPU 7192.3 42.0 
    log file switch (checkpoint incomplete)4,8485884.21213.7534.4Configuration
    log file sync5,291,1584000.80.7623.4Commit
    enq: TX - allocate ITL entry260222.2854.711.3Configuration
    buffer busy waits176,618199.81.131.2Concurrency
    library cache: mutex X113,018160.41.42.9Concurrency
    virtual circuit wait47,586131.72.77.8Network
    log file switch completion1,877126.167.16.7Configuration
    cursor: pin S7,07071.710.15.4Concurrency
    enq: TX - index contention49,76465.21.31.4Concurrency
    See this line
    log file switch (checkpoint incomplete)4,8485884.21213.7534.4Configuration

    This is high shouldn't be above 20ms. (May vary for systems)

    Action: This is what I did:
    1. Asked for control file copy
    2. and ls -ltr from archive destination for latest picture, frequency of file creation.

    I my case, redo log file was 10M and was switching every minute, see has fast archives are getting created.

    Beefing redologfile to 500M and expecting switch at about 10min each. Increasing log file group to 3 or 6.  Three means 30min for overwrite of redolog file post archival and 6 means 60mins(may see less because of very busy system).

    Commands sequence:
    spool /tmp/redomod.lst
    set line 500
    col MEMBER for a100
    select * from v$logfile;
    select * from v$log;

    alter database add logfile group 4 '/oracle/oradata/mis/redo04.log' size 500M;
    alter database add logfile group 5 '/oracle/oradata/mis/redo05.log' size 500M;
    alter database add logfile group 6 '/oracle/oradata/mis/redo06.log' size 500M;

    --run :
    execute alter system switch logfile;  /* <==== 3times*/
    -- Look for status in output of following query = 'INACTIVE' for 1,2,3. Only inactive can be dropped. Any other status wait and execute following as group 1/2/3 become inactive.
    select * from v$log; 

    ---Now drop 1,2,3 ONLY WHEN GROUP STATUS US INACTIVE !!!! ONE BY ONE.
    alter database drop logfile group 1;
    alter database drop logfile group 2;
    alter database drop logfile group 3;
     -----USE OS command to rename redo file. eg: mv /oracle/oradata/mis/redo01.log /oracle/oradata/mis/redo01.log.bak

    alter database add logfile group 1 '/oracle/oradata/mis/redo01.log' size 500M;
    alter database add logfile group 2 '/oracle/oradata/mis/redo02.log' size 500M;
    alter database add logfile group 3 '/oracle/oradata/mis/redo03.log' size 500M;

    alter database backup controlfile to trace as '/tmp/ctldb.ctl';
    spool off

    In my case, I kept adding file until I saw atleast 2files in INACTIVE state.

    Additional recomendation: Redo file should be multiplexed to protect from mount point corruption.
    Sample:

    ALTER DATABASE ADD LOGFILE  member '/oracle/oradata/mis/redo01a.logreuse to GROUP 1;

    Wednesday, 3 February 2016

    Oracle Control file to trace readable format to known destination

    Oracle control file is a good source of info for database physical layout.

    Here is how you can get text output:

    Sample command:
    alter database backup controlfile to trace as '/tmp/cntl.ctl';   


    Above command is different from :

    Alter database backup controlfile to '/tmp/cntrol.bck'; <= this is actual backup in binary. Different usage.