Skip to main content

Steps to Purge SQL Plan from the Shared Pool

🧱 Steps to Purge SQL Plan from the Shared Pool

In some cases, after assigning an SQL profile for a particular SQL_ID, it may not be used as expected. This guide will help you ensure that the SQL profile is being used and, if necessary, purge the SQL plan from the shared pool to force the system to reoptimize and apply the assigned SQL profile.


🔧 Step-by-Step Instructions

1️⃣ Verify if the SQL Profile is Being Used

Before purging any SQL plan, you should first check if the assigned SQL profile is being used. Run the following query to check:

select distinct
    p.name sql_profile_name,
    s.sql_id
from
    dba_sql_profiles p,
    DBA_HIST_SQLSTAT s
where
    p.name = s.sql_profile
    and sql_id = '5dj81jnf8t03a';  -- Replace with your SQL_ID

If the profile is being used, the output will display the associated SQL profile name. Alternatively, you can use the DBMS_XPLAN function to check the SQL profile:

SELECT * FROM TABLE(DBMS_XPLAN.display_cursor('&sqlid'));

If the profile is being used, you will see a note like the following:

Note
-----
   - SQL profile SYS_SQLPROF_02630dc612e60001 used for this statement

2️⃣ Find the SQL Plan's Address and Hash Value

If the SQL profile is not being applied, the next step is to find the address and hash value of the SQL plan for your SQL_ID. Run the following query to retrieve this information:

SELECT ADDRESS, HASH_VALUE 
FROM V$SQLAREA 
WHERE SQL_ID = '5dj81jnf8t03a';  -- Replace with your SQL_ID

The output will provide the address and hash value, as shown below:

ADDRESS          HASH_VALUE
----------------  ----------
00000010B774F9D8  478969962

3️⃣ Purge the SQL Plan from the Shared Pool

Once you have the address and hash value, you can purge the SQL plan from the shared pool by executing the following command:

EXEC DBMS_SHARED_POOL.PURGE('00000010B774F9D8, 478969962', 'C');

In this command:

  • '00000010B774F9D8' is the ADDRESS of the plan.
  • '478969962' is the HASH_VALUE.
  • 'C' specifies that the cursor (SQL plan) should be purged.

4️⃣ Verify if the SQL Plan is Flushed from Memory

After purging the plan, check again to see if it has been removed from memory by running this query:

SELECT ADDRESS, HASH_VALUE 
FROM V$SQLAREA 
WHERE SQL_ID = '5dj81jnf8t03a';  -- Replace with your SQL_ID

If no rows are returned, the plan has been successfully flushed out from memory.

5️⃣ Recheck if the SQL Profile is Now Being Used

Finally, go back to Step 1 and verify again if the SQL profile is now being applied. If the profile is being used, you have successfully ensured that the assigned SQL profile is applied to the SQL_ID.


🧠 Why Purge SQL Plans?

  • Forces the database to reoptimize a query, applying the latest SQL profile and any optimizations.
  • Helps in situations where the database isn't using the assigned profile as expected.
  • Clears any plans that might have been stuck in memory, preventing performance issues.

🚨 Best Practices

  • Only purge SQL plans when necessary to avoid unnecessary performance overhead.
  • Ensure that the SQL profile is correctly created and assigned before purging the plan.
  • Use DBMS_SHARED_POOL.PURGE carefully as it affects the shared pool memory.

🎯 This process helps ensure that your Oracle database is using the correct SQL profile and that the system is optimized for query performance.

Comments

Popular posts from this blog

🚀 Automating Oracle Database Patching with Ansible: A Complete Guide

Oracle database patching has long been the bane of DBAs everywhere. It's a critical task that requires precision, expertise, and often results in extended maintenance windows. What if I told you that you could automate this entire process, reducing both risk and downtime while ensuring consistency across your Oracle estate? 💡 In this comprehensive guide, I'll walk you through a production-ready Ansible playbook that completely automates Oracle patch application using OPatch. Whether you're managing a single Oracle instance or hundreds of databases across your enterprise, this solution will transform your patch management strategy! 🎯 🔥 The Challenge: Why Oracle Patching is Complex Before diving into the solution, let's understand why Oracle patching is so challenging: 🔗 Multiple dependencies : OPatch versions, Oracle Home configurations, running processes ⚠️ Risk of corruption : Incorrect patch application can render databases unusable ⏰ Downtime requirements : Da...

Oracle RAC Switchover & Switchback: Step-by-Step Guide

 Ensuring business continuity requires regular Disaster Recovery (DR) drills. This guide covers the Switchover and Switchback process between Primary (DC) and Standby (DR) databases . Pre-checks Before Performing Switchover Before starting the activity, ensure there are no active sessions in the database. If any are found, share the session details with the application team, get their confirmation, and terminate the sessions. Primary Database Name: PRIMARY Standby Database Name: STANDBY  Identify Active Sessions set lines 999 pages 999 col machine for a30 col username for a30 col program for a30 compute sum of count on report break on report select inst_id,username,osuser,machine,program,status,count(1) "count" from gv$session where inst_id=1 and program like 'JDBC%' group by inst_id,username,osuser,machine,program,status order by 1,2; select inst_id,username,osuser,machine,program,status,count(1) "count" from gv$session where inst_id=2 and program lik...

Mastering Oracle RAC with SRVCTL Commands

Oracle Real Application Clusters (RAC) provide high availability, scalability, and manageability for databases. One of the most powerful tools for managing RAC databases is srvctl , a command-line utility that allows administrators to control various database services. This blog explores essential srvctl commands to help you efficiently manage Oracle RAC environments. 1. Checking Database Configuration and Status  List all available databases on the host:                  srvctl config database   Check the status of a specific database and its instances:                    srvctl status database -d <database_name>   Retrieve detailed status information about a database, including its instances and states:                    srvctl status database -d <database_name> -v 2. Stopping and Starting Databases   ...