# Bind variable peeking doesn't work

**URL:** <https://forums.toadworld.com/t/bind-variable-peeking-doesnt-work/33411>\
**Category:** Toad for Oracle & DevOps Toolkit\
**Created:** [June 3, 2015, 12:19am UTC](https://forums.toadworld.com/t/bind-variable-peeking-doesnt-work/33411 "2015-06-03T00:19:43Z")\
**Posts on this page:** 1\
**Showing post:** 16

<div class="post-metadata">

**Author:** ![dinch.osman](https://sea1.discourse-cdn.com/quest/user_avatar/forums.toadworld.com/dinch.osman/32/16327_2.png) [@dinch.osman](https://forums.toadworld.com/u/dinch.osman)\
**Post date:** [December 4, 2023, 3:22pm UTC](https://forums.toadworld.com/t/bind-variable-peeking-doesnt-work/33411/16 "2023-12-04T15:22:33Z")

</div>

> [@dinch.osman](#):
>
> 13386678

Hi John,

Here is the test case.

--Checking ACS related parameters (It is all default values)  
SELECT a.KSPPINM "PARAMETER",  
b.KSPPSTDF "IS\_DEFAULT",  
b.KSPPSTVL "SESSION",  
c.KSPPSTVL "INSTANCE"  
FROM X$KSPPI a, X$KSPPCV b, X$KSPPSV c  
WHERE a.INDX = b.INDX  
AND a.INDX = c.INDX  
AND a.KSPPINM IN ('\_optim\_peek\_user\_binds',  
'\_optimizer\_adaptive\_cursor\_sharing',  
'\_optimizer\_extended\_cursor\_sharing',  
'\_optimizer\_extended\_cursor\_sharing\_rel');

PARAMETER IS\_DEFAULT SESSION INSTANCE  
\_optimizer\_extended\_cursor\_sharing TRUE UDO UDO  
\_optimizer\_extended\_cursor\_sharing\_rel TRUE SIMPLE SIMPLE  
\_optimizer\_adaptive\_cursor\_sharing TRUE TRUE TRUE  
\_optim\_peek\_user\_binds TRUE TRUE TRUE

create table system.test (pk int, data char(100));

-- 500k rows, almost all of them pk=20

INSERT INTO system.test  
SELECT CASE WHEN ROWNUM \< 20 THEN ROWNUM ELSE 20 END, ROWNUM  
FROM DUAL connect by level\<= 500000

commit;

create index index on test(pk);

exec dbms\_stats.gather\_table\_stats ('SYSTEM','test');

-- create histogram  
exec dbms\_stats.gather\_table\_stats ('SYSTEM','test', method\_opt=\>'for columns pk size 200');

-- Now run all commands in SQLPLUS  
-- bind input 10 , we expect an index scan  
var vr1 number;  
exec :vr1:=10;

select max(data) max\_data, count(_) from system.test where pk =:vr1;  
MAX\_DATA COUNT(_)  
10 1

select \* from dbms\_xplan.display\_cursor(format=\>'+peeked\_binds');

## PLAN\_TABLE\_OUTPUT

## SQL\_ID 4csc6dsrzjnc8, child number 0

select max(data) max\_data, count(\*) from system.test where pk =:vr1

Plan hash value: 2838211638

* * *

## | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

## | 0 | SELECT STATEMENT | | | | 4 (100)| | | 1 | SORT AGGREGATE | | 1 | 104 | | | | 2 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST | 1 | 104 | 4 (0)| 00:00:01 | |\* 3 | INDEX RANGE SCAN | INDEX1 | 1 | | 3 (0)| 00:00:01 |

## Peeked Binds (identified by position):

1 - :VR1 (NUMBER): 10

Predicate Information (identified by operation id):

## PLAN\_TABLE\_OUTPUT

3 - access("PK"=:VR1)

--sql\_id is 4csc6dsrzjnc8. -- sql is marked as bind\_sensitive  
select sql\_id,sql\_text,child\_number,is\_shareable,is\_bind\_sensitive,is\_bind\_aware from v$sql where sql\_id='4csc6dsrzjnc8';  
SQL\_ID SQL\_TEXT CHILD\_NUMBER IS\_SHAREABLE IS\_BIND\_SENSITIVE IS\_BIND\_AWARE  
4csc6dsrzjnc8 select max(data) max\_data, count(\*) from test where pk =:vr1 0 Y Y N

-- bind input 20 this time , we expect an index scan in first execute, as rows returned will fluctuate too much, it will switch to fulltable scan on second execution  
var vr1 number;  
exec :vr1:=20;  
select max(data) max\_data, count(_) from system.test where pk =:vr1;  
select max(data) max\_data, count(_) from system.test where pk =:vr1;

MAX\_DATA COUNT(\*)

* * *

99999 499981

select \* from dbms\_xplan.display\_cursor(format=\>'+peeked\_binds');

## PLAN\_TABLE\_OUTPUT

## SQL\_ID 4csc6dsrzjnc8, child number 1

select max(data) max\_data, count(\*) from system.test where pk =:vr1

Plan hash value: 1950795681

* * *

## | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

## | 0 | SELECT STATEMENT | | | | 2060 (100)| | | 1 | SORT AGGREGATE | | 1 | 104 | | | |\* 2 | TABLE ACCESS FULL| TEST | 499K| 49M| 2060 (1)| 00:00:01 |

## Peeked Binds (identified by position):

1 - :VR1 (NUMBER): 20

## Predicate Information (identified by operation id):

## PLAN\_TABLE\_OUTPUT

2 - filter("PK"=:VR1)

select sql\_id,sql\_text,child\_number,is\_shareable,is\_bind\_sensitive,is\_bind\_aware from v$sql where sql\_id='4csc6dsrzjnc8';

SQL\_ID SQL\_TEXT CHILD\_NUMBER IS\_SHAREABLE IS\_BIND\_SENSITIVE IS\_BIND\_AWARE  
4csc6dsrzjnc8 select max(data) max\_data, count(_) from test where pk =:vr1 0 N Y N  
4csc6dsrzjnc8 select max(data) max\_data, count(_) from test where pk =:vr1 0 Y Y Y

--flush shared pool  
alter system flush shared\_pool;

**--- Now run all commands in TOAD session**  
-- for binds enter input (10 first, then 20)  
-- You wont get a new child cursor, you will always see an index scan and your sql will not be marked as bind\_sensitive.  
-- enter 10  
select max(data) max\_data, count(_) from system.test where pk =:vr1;  
MAX\_DATA COUNT(_)  
10 1

select \* from dbms\_xplan.display\_cursor(format=\>'+peeked\_binds');

## SQL\_ID 4csc6dsrzjnc8, child number 0

select max(data) max\_data, count(\*) from system.test where pk =:vr1

Plan hash value: 2838211638

* * *

## | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

## | 0 | SELECT STATEMENT | | | | 430 (100)| | | 1 | SORT AGGREGATE | | 1 | 104 | | | | 2 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST | 25000 | 2539K| 430 (0)| 00:00:01 | |\* 3 | INDEX RANGE SCAN | INDEX1 | 25000 | | 51 (0)| 00:00:01 |

## Predicate Information (identified by operation id):

3 - access("PK"=TO\_NUMBER(:VR1))

--sql\_id is 4csc6dsrzjnc8. -- sql is not marked as bind\_sensitive  
select sql\_id,sql\_text,child\_number,is\_shareable,is\_bind\_sensitive,is\_bind\_aware from v$sql where sql\_id='4csc6dsrzjnc8';  
SQL\_ID SQL\_TEXT CHILD\_NUMBER IS\_SHAREABLE IS\_BIND\_SENSITIVE IS\_BIND\_AWARE  
4csc6dsrzjnc8 select max(data) max\_data, count(\*) from test where pk =:vr1 0 Y N N

--enter 20  
select max(data) max\_data, count(_) from system.test where pk =:vr1;  
MAX\_DATA COUNT(_)  
20 499981

select \* from dbms\_xplan.display\_cursor(format=\>'+peeked\_binds');

## SQL\_ID 4csc6dsrzjnc8, child number 0

select max(data) max\_data, count(\*) from system.test where pk =:vr1

Plan hash value: 2838211638

* * *

## | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |

## | 0 | SELECT STATEMENT | | | | 430 (100)| | | 1 | SORT AGGREGATE | | 1 | 104 | | | | 2 | TABLE ACCESS BY INDEX ROWID BATCHED| TEST | 25000 | 2539K| 430 (0)| 00:00:01 | |\* 3 | INDEX RANGE SCAN | INDEX1 | 25000 | | 51 (0)| 00:00:01 |

## Predicate Information (identified by operation id):

3 - access("PK"=TO\_NUMBER(:VR1))

--sql\_id is 4csc6dsrzjnc8. -- sql is not marked as bind\_sensitive  
select sql\_id,sql\_text,child\_number,is\_shareable,is\_bind\_sensitive,is\_bind\_aware from v$sql where sql\_id='4csc6dsrzjnc8';  
SQL\_ID SQL\_TEXT CHILD\_NUMBER IS\_SHAREABLE IS\_BIND\_SENSITIVE IS\_BIND\_AWARE  
4csc6dsrzjnc8 select max(data) max\_data, count(\*) from test where pk =:vr1 0 Y N N

I think, In TOAD you are parsing sql statement with a DBMS\_SQL command before execution.

And according to the Doc ID 2977764.1, "As far as why bind peeking doesn't happen, this is because DBMS\_SQL doesn't support it. See this in the known issue Bug 13386678.  
In short, DBMS\_SQL uses the old RPI interface, which doesn't support bind peeking."

As bind peeking is not fired in TOAD, CBO(Cost based optimizer) makes estimation of returned rows will be 25000, as you may see in the plan.(Total rows(500.000) x 0,05) This is a hardcoded value according to the Doc ID 70075.1 Use of Bind Variables in Queries (Pre-9i).

---

_[View the full topic](https://forums.toadworld.com/t/bind-variable-peeking-doesnt-work/33411)._
