Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Looping for remote servers and find its database from oratab file. Linux/Unix
  • Shuffle an array PHP/MYSQL/Wordpress
  • DBMS_PROFILER for tuning PLSQL programs. Oracle
  • Find long Running Transaction Linux/Unix
  • find_open_cur.sql Find open cursorts per session Oracle
  • Finding last recovered file on DR and remove all chanracters before any “/” Linux/Unix
  • Find_stale_dr.sql finding stale physical DR.. Oracle
  • cold backup scripts to copy locally Linux/Unix
  • checking redhat linux version Linux/Unix
  • Good Doc 28-JUN-2006 Oracle
  • Optimizer SORT Operations Oracle
  • Small sample shell program Linux/Unix
  • Implementation of key based authentications Linux/Unix
  • How to sort list of files on basis of their sizes. Linux/Unix
  • Running PDB on single node in RAC Oracle

tracksqltime.sql

Posted on 05-Mar-2026 By Admin No Comments on tracksqltime.sql

This PL/SQL block identifies recently active SQL queries from a specific application server and provides details on their performance and execution plans. 

Here is a breakdown of what the script does:

  • Filters Activity: It scans the Active Session History (gv$active_session_history) to find distinct SQL IDs triggered by machines containing “APP02” in their name within the last hour.
  • Retrieves SQL Text: For each identified SQL ID, it attempts to fetch the actual SQL statement from the library cache (gv$sql). If the text is no longer in memory, it labels it as “NO SQL TEXT.”
  • Analyzes Execution Plans: It looks for all unique Plan Hash Values associated with that SQL ID, searching both real-time memory (gv$sql_plan) and historical records (dba_hist_sql_plan).
  • Calculates Performance: For every execution plan found, it calculates the Average Elapsed Time (in seconds) by checking current statistics and historical snapshots (dba_hist_sqlstat).
  • Displays Results: It prints a formatted report to the console showing the SQL ID, the machine name, the SQL text, and a list of all known execution plans with their respective average timings. 
set lines 120 pages 200;
SET SERVEROUTPUT ON;

DECLARE
  header  varchar2(300);
  tmp_sqltext  varchar2(1000);
  CURSOR c1 IS
    SELECT distinct  SQL_ID, machine, program
    FROM gv$active_session_history
    WHERE upper(machine) LIKE upper('%XAAPP02%')
      AND sample_time > SYSDATE - 1/24
      AND sql_id IS NOT NULL ;
BEGIN
--  header := lpad('SQL_ID', 15) || lpad('PHV', 15) || lpad('Avg ET', 14) || lpad('Machine', 30);
--  DBMS_OUTPUT.PUT_LINE(header);
  FOR cur1 IN c1 LOOP
    begin
    select sql_text into tmp_sqltext from gv$sql where sql_id = cur1.sql_id and rownum = 1;
    exception
       when no_data_found then
            tmp_sqltext := 'NO SQL TEXT in V$SQL';
    end;
    dbms_output.put_line(lpad('------------------', 25));
    DBMS_OUTPUT.PUT_LINE(lpad(cur1.sql_id,15) || lpad(cur1.machine, 30) || ' ' || chr(10) || tmp_sqltext);
    begin
    -- Nested loop to handle 1-to-many relationship between SQL_ID and Plans
    FOR plan_cur IN (
        WITH
        p AS (
            SELECT plan_hash_value FROM gv$sql_plan
             WHERE sql_id = cur1.sql_id AND other_xml IS NOT NULL
             UNION
            SELECT plan_hash_value FROM dba_hist_sql_plan
             WHERE sql_id = cur1.sql_id AND other_xml IS NOT NULL
        ),
        m AS (
            SELECT plan_hash_value,
                   SUM(elapsed_time)/SUM(executions) avg_et_secs
              FROM gv$sql
             WHERE sql_id = cur1.sql_id AND executions > 0
             GROUP BY plan_hash_value
        ),
        a AS (
            SELECT plan_hash_value,
                   SUM(elapsed_time_total)/SUM(executions_total) avg_et_secs
              FROM dba_hist_sqlstat
             WHERE sql_id = cur1.sql_id AND executions_total > 0
             GROUP BY plan_hash_value
        )
        SELECT p.plan_hash_value,
               ROUND(NVL(m.avg_et_secs, a.avg_et_secs)/1e6, 3) AS avg_et_secs
          FROM p
          LEFT JOIN m ON p.plan_hash_value = m.plan_hash_value
          LEFT JOIN a ON p.plan_hash_value = a.plan_hash_value
    ) LOOP
      -- This prints for every plan found for the current SQL_ID
      DBMS_OUTPUT.PUT_LINE( lpad(plan_cur.plan_hash_value,15) || ' ' || lpad(plan_cur.avg_et_secs,10));
    END LOOP;
    exception
        when no_data_found  then
             DBMS_OUTPUT.PUT_LINE( 'NO PLAN INFO AVAILABLE');

    end;
  END LOOP;
END;
/
Oracle, SQL scripts

Post navigation

Previous Post: Complete Git Tutorial for Beginners
Next Post: prepfiles.sh for step by step generating pending statistics files

Related Posts

  • Pending Distributed Transations Oracle
  • Pending Transaction Neighbors Script Oracle
  • V$transaction notes for finding XID composition. Oracle
  • AWR license Oracle
  • DBA_HIST_SQLSTAT contents Oracle
  • plan10g.sql Oracle

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Django (0)
  • GIT (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (400)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (1)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (18)
  • rman-dataguard (26)
  • shell (150)
  • SQL scripts (348)
  • SQL Server (6)
  • Uncategorized (3)
  • Videos (0)

Recent Posts

  • Running PDB on single node in RAC09-Apr-2026
  • find_arc.sql09-Apr-2026
  • pvm_pre_change.sql08-Apr-2026
  • find_encr_wallet.sql08-Apr-2026
  • find_pdbs.sql08-Apr-2026
  • Creating a Container Database using dbaascli08-Apr-2026
  • track_autoupgrade_copy_progress.sql01-Apr-2026
  • refre.sql for multitenant01-Apr-2026
  • prepfiles.sh for step by step generating pending statistics files10-Mar-2026
  • tracksqltime.sql05-Mar-2026

Archives

  • 2026
  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • 272332.1 CRS 10g Diagnostic Collection Guide Oracle
  • ORA-8031 issue and solution if it is occuring due to truncate. Oracle
  • checking redhat linux version Linux/Unix
  • Finding locked objects Oracle
  • Find All internal Parameters Oracle
  • Adding a new disk and mount it automatically. on VMWARE LINUX Linux/Unix
  • Some OS level threshold for performance. Linux/Unix
  • note id 373303.1 Linux/Unix

Copyright © 2026 pvmehta.com.

Powered by PressBook News WordPress theme