How to flush one Cursor out of the Shared Pool
REM Flushes one
cursor
out
of
the shared pool. Works
on
11g+
REM
To
create
DBMS_SHARED_POOL, run the DBMSPOOL.SQL script.
REM The PRVTPOOL.PLB script
is
automatically executed
after
DBMSPOOL.SQL runs.
REM These scripts are
not
run
by
as
part
of
standard
database
creation.
SPO flush_cursor_&&sql_id..txt;
PRO *** before flush ***
SELECT
inst_id, loaded_versions, invalidations, address, hash_value
FROM
gv$sqlarea
WHERE
sql_id =
'&&sql_id.'
ORDER
BY
1;
SELECT
inst_id, child_number, plan_hash_value, executions, is_shareable
FROM
gv$sql
WHERE
sql_id =
'&&sql_id.'
ORDER
BY
1, 2;
BEGIN
FOR
i
IN
(
SELECT
address, hash_value
FROM
gv$sqlarea
WHERE
sql_id =
'&&sql_id.'
)
LOOP
SYS.DBMS_SHARED_POOL.PURGE(i.address||
','
||i.hash_value,
'C'
);
END
LOOP;
END
;
/
PRO ***
after
flush ***
SELECT
inst_id, loaded_versions, invalidations, address, hash_value
FROM
gv$sqlarea
WHERE
sql_id =
'&&sql_id.'
ORDER
BY
1;
SELECT
inst_id, child_number, plan_hash_value, executions, is_shareable
FROM
gv$sql
WHERE
sql_id =
'&&sql_id.'
ORDER
BY
1, 2;
UNDEF sql_id;
SPO
OFF
;
=========================================================
Script below is an enhanced version, where besides using DBMS_SHARED_POOL.PURGE, we also create a dummy SQL patch, then drop it. This method seems to completely flush parent and child cursors. Why using this method instead?: We are implementing SQL Plan Management (SPM), and we have found that in some cases, some child cursors are still shared several hours after a SQL Plan Baseline (SPB) is created. We could argue a possible bug and pursue as such, but in the meantime my quick and dirty workaround is: whenever I want to flush an individual parent cursor for one SQL, and all of its child cursors, I just execute script below passing SQL_ID.
Anyways, just wanted to share and document this purge_cursor.sql script for those in similar need. I have developed it on 12.1.0.2, and haven’t tested it on lower or higher versions.
-- purge_cursor.sql
Article by kerryosborne regarding Flush of a Single Statement
- flush the shared pool – not a very appealing option in a production environment (although I see almost the same affect frequently at sites that gathering stats every night).
- modify an object that the statement depends on – I usually would add a comment to one of the tables used by the statement. Unfortunately, all statements that use the table will be flushed, so this technique can also be a little hard on a production system, but it’s certainly better than flushing the whole shared pool.
No comments:
Post a Comment