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...

Complete Guide to PostgreSQL 18 Source Installation with Performance Optimization

PostgreSQL 18 brings powerful new features and performance improvements that make it an excellent choice for modern database workloads. In this comprehensive guide, I'll walk you through installing PostgreSQL 18 from source code while implementing a strategic disk layout that maximizes performance. Why Install from Source? While package managers offer convenience, building PostgreSQL from source gives you complete control over configuration and optimization options. This flexibility allows you to tailor the database precisely to your hardware and workload requirements. Prerequisites Before we begin, ensure you're working with a RHEL, CentOS, or Fedora-based system. We'll be installing several development tools and libraries needed for compilation. Installation Process 1. Download PostgreSQL 18 Source Code First, grab the latest PostgreSQL 18 source tarball: wget https://ftp.postgresql.org/pub/source/v18.0/postgresql-18.0.tar.gz 2. Install Required Dependencies Install all n...

⚡ Automating Oracle 19c Database Patching on Windows Server with PowerShell

Applying Oracle patches on Windows often feels repetitive and error-prone — stopping services, updating OPatch, applying patches, running datapatch , and restarting services. To save time ⏱ and reduce mistakes ⚠, I created a PowerShell automation script that performs the patching process end-to-end 🚀. 🔹 Why Automate Oracle Patching? ⏱ Save time by automating repetitive steps ⚙ Avoid manual errors during patching 📜 Maintain logs for auditing and troubleshooting 🛡 Ensure consistent, reliable patching 🔹 Full PowerShell Script # --- CONFIGURATION --- $oracleHome = "c:\users\administrator\downloads\v982656-01" # Change if this is not your actual Oracle Home! $patchDir = "C:\Users\Administrator\Downloads\p37962957_190000_MSWIN-x86-64\37962957" #update the Patchfile directory $opatchDir = "$oracleHome\OPatch" $opatchZip = "C:\Users\Administrator\Downloads\p6880880_190000_MSWIN-x86-64.zip" #Based on your setup update Opatch di...