KB · Oracle Database · Script

Find SQL that doesn't use bind variables (literal SQL in the shared pool)

Oracle Database · Script · Verified on Oracle 23ai · Updated 2026-12-03

An application that builds SQL by pasting values into the text (where id = 42, where id = 43…) makes the database hard-parse every one as a new statement. At volume that burns CPU on parsing, fills the shared pool with single-use cursors and can end in ORA-04031. This script strips the literals out of every statement in the shared pool and counts how many copies of each shape there are.

Before you run it

nobinds.sql calls Tom Kyte's remove_constants function, which replaces string and number literals with placeholders. It isn't included here because it's his code: get it from AskTom (search for remove_constants) and create it in your schema first.

Usage

SQL> @nobinds.sql 1000

The argument is how many copies a statement shape needs before it's listed.

nobinds.sqlMIT · Download ·
-- nobinds.sql: statements that differ only in their literals, grouped and counted
-- Copyright (c) HASHJOIN. MIT License (https://hashjoin.com/kb/license/)
-- https://hashjoin.com/kb/oracle/sql-without-bind-variables/
--
-- Usage: @nobinds.sql <min-copies>
--        @nobinds.sql 1000
--
-- Needs Tom Kyte's remove_constants function (see the page for the link).
-- Creates a global temporary table sql_area_tmp in the current schema.

alter session set recyclebin=off;

begin
   execute immediate 'truncate table sql_area_tmp';
   execute immediate 'drop table sql_area_tmp';
exception
   when others then null;   -- first run: the table doesn't exist yet
end;
/

CREATE GLOBAL TEMPORARY TABLE sql_area_tmp
on commit preserve rows
AS
SELECT
   sql_text
,  sql_text sql_text_wo_constants
,  module
,  action
FROM v$sqlarea
WHERE 1=0
/

ALTER TABLE sql_area_tmp
MODIFY (
   SQL_TEXT_WO_CONSTANTS VARCHAR2(2000)
)
/

INSERT INTO sql_area_tmp (sql_text,module,action)
SELECT sql_text,module,action FROM v$sqlarea
/

UPDATE sql_area_tmp
SET sql_text_wo_constants = remove_constants(sql_text)
/

COMMIT
/

set pages 60
set lines 100
set trims on
set head on
set feed on
set verify off

col sql_text_wo_constants format a80 word_wrapped heading "SQL needs rewrite to use bind variables"

SELECT
   sql_text_wo_constants
,  COUNT(*) cnt
FROM sql_area_tmp
GROUP BY sql_text_wo_constants
HAVING COUNT(*) > &&1
ORDER BY 2 DESC
/

undefine 1

curl -O https://hashjoin.com/kb/oracle/sql-without-bind-variables/nobinds.sql

Reading the output

Each row is one statement shape with literals replaced by # (strings) and @ (numbers), and how many copies of it are in the shared pool. After a loop ran select count(*) from dual where 1 = <n> 1,500 times, a lab run showed:

SQL needs rewrite to use bind variables                                                 CNT
-------------------------------------------------------------------------------- ----------
SELECT COUNT(*) FROM DUAL WHERE @ = @                                                   523

523, not 1,500: the shared pool had already aged out the rest, which is the churn this causes. The top rows are the code to change to bind variables. module and action are in the temporary table if you need to trace them back to an application.

A quicker check that needs no objects: statements that differ only in literals share a force_matching_signature in v$sql.

select force_matching_signature, count(distinct sql_id) copies, max(sql_id) example_sql_id
from   v$sql
where  force_matching_signature <> 0
group  by force_matching_signature
having count(distinct sql_id) > 1000
order  by 2 desc;

Gotchas

What it reads

v$sqlarea v$sql

Tired of fixing the same thing twice?

HASHJOIN turns the checks and fixes your on-call runs by hand into tooling that gives the same answer every time. Fixed scope, and you own all of the code.