Cursor Sharing and Bind Variable Usage Effect on Oracle Database Performance

Hi,

I will explain what is the Cursor Sharing and Bind Variable Usage Effect on Oracle Database Performance.

 

If you don’t know how to tune of Oracle database, you should read the following article before this.

https://ittutorial.org/oracle-database-performance-tuning-tutorial-2-performance-tuning-features-and-tools/

 

When any SQL is sent Oracle for the first time, Oracle will create a plan by following the steps below. Or, if the plan has been created, the existing plan will be used or new plan will be created even if the SQL is the same (different literal values, or not using bind variable ).

blank

 

 

blank

 

If a new SQL is not exists in the Shared pool, it must be parsed from scratch.

The cost of creating the plan for the first time is very high for Oracle database.

If any SQL will run for the first time, Oracle has to do a hard parse. If this SQL has been run at least once before, that is, Oracle does a Soft parse for this execution.

 

If you want to learn more detail about SQL Statement phases and parsing, you should read the following article.

SQL Statement Processing in Oracle Database

 

For Oracle, One character of SQL is different or existing lower case-upper case character, then new plan is created for this time.

 

If you don’t use bind variables in the SQL queries then Oracle will know similar SQLs differently like following.

select * from customer where id=63;

select * from customer where id=34;

Above queries are almost same, just id variable is different. But Oracle optimizer will evaluate these SQLs like different SQL.

 

If you use bind variable instead of literal like following then Oracle will evaluate as same SQL and will use same execution plan and won’t be hard parse in Oracle.

 

variable SYS_B_0 number; 
exec :SYS_B_0:= 63 
select * from customer where id= :SYS_B_0;

 

 

CURSOR_SHARING is an init.ora parameter and related with bind variables usage which decides whether a SQL send from user is a candidate for new parsing ( hard parse ) or will use an existing plan ( Soft Parse ).

The CURSOR_SHARING parameter can get the following 3 values.

 

Exact

Exact is default value of Cursor_sharing parameter. In this value, Oracle will share the same plan exactly if text of SQL matches exactly with the text of SQL kept in the shared pool and use that cursor and don’t do any changes in the SQL. Otherwise Oracle will generate different execution plans for every query( If they are not same exactly).

 

Let’s look at an example.

 

SQL> create table deveci (id number);

Table created.

SQL> insert into deveci values(1);

1 row created.

SQL> insert into deveci values(2);

1 row created.

SQL> commit;

Commit complete.

SQL> select * from deveci where id=1;

ID
----------
1

SQL> select * from deveci where id=2;

ID
----------
2



SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from deveci%'
4 order by sql_text;

SQL_TEXT
-----------------------------------------------------

select * from deveci where id=1
select * from deveci where id=2

 

As you see there are 2 statements in V$sql result and Shared pook, so Oracle has generated 2 plans for these 2 SQLs.  Oracle will generate the new plan in this case even when the difference between the two SQL was just literal value.

 

Force

Force is introduced in 8.1.6. In this value, Oracle will share the same plan forcibly if text of SQL matches with the text of SQL except the literal values kept in the shared pool.  Oracle will replace every literals with bind variables and share the same plan if 2 SQL’s are same except their literal values

 

SQL> alter session set cursor_sharing=force;

Session altered.


SQL> select * from deveci where id=1;

id
----------
1

SQL> select * from deveci where id=2;

id
----------
2

SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from deveci%'
4 order by sql_text;

SQL_TEXT
---------------------------------------------------

select * from deveci where id=:"SYS_B_0"

There is one SQL in the Shared pool and V$SQL result, it means Oracle didn’t generated a new plan for the second SQL statement.

Duplicate Plan generate will make the Database and Operating system resources busy ( CPU,RAM,IO ) unnecessarily

 

Similar

Similar is introduced in 9i. In this value, Oracle will share the same plan if text of SQL matches with the text of SQL except the literal values kept in the shared pool. Namely SQL statements differ just only in literal values.

Create a new table for simulating this case.

SQL> create table deveci (id1 number,id2 number);

Table created.

SQL>
begin
for i in 1 .. 100 loop
insert into deveci values(1,i);
end loop;
commit;
/

PL/SQL procedure successfully completed.

update deveci set id1=2 where rownum =1;
commit;

 

 

 

Deveci table have 99rows in 1 value and 1 row in 2 value.

 

SQL> create index ix_on_id on deveci(id1);

Index created.




SQL> alter session set cursor_sharing=similar;

Session altered.

SQL> select * from deveci where id=2;

1 row selected.

SQL> select * from deveci where id=1;

99 rows selected.

SQL> select * from deveci where id=10;

0 row selected.

SQL>



SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from deveci%'
4 order by sql_text;

SQL_TEXT
----------------------------------------------------

select * from deveci where id=:"SYS_B_0"
select * from deveci where id=:"SYS_B_0"
select * from deveci where id=:"SYS_B_0"

 

Oracle generated 3 plans for these similar SQLs, even they are similar, but their Plans should be different. Oracle Optimizer stored literals to ensure the reusability of the plan only in case the same literal is provided.

Even though SIMILAR and EXACT look like same but they are not same. For example; Following queries looks like similar but literals are different. Because Plan should be same, Oracle generated same plan for 2 SQLs.

 

SQL> alter system flush shared_pool;

System altered.

SQL> select * from deveci where id1=1 and id1=2;

no rows selected

SQL> select * from deveci where id1=2 and id1=1;

no rows selected

SQL> select sql_text
2 from v$sql
3 where sql_text like 'select * from deveci%'
4 order by sql_text;

SQL_TEXT
--------------------------------------------------------------

select * from deveci where id1=:"SYS_B_0" and id1=:"SYS_B_1"

 

You should set CURSOR_SHARING parameter value according to your database and software. If related application is not used Bind Variable , response time low due to a very high number of library cache misses and lots of SQL Statements matches with the text of SQL except the literal, then you should use CURSOR_SHARING=FORCE. 

Actually Cursor_sharing= force is temporary solution, Permanent solution is to use Bind Variable.

 

Oracle doesn’t recommend CURSOR_SHARING=FORCE in the DSS(Decision support system) environment or if you are using complex queries

 

 

Do you want to learn Oracle Database Performance Tuning detailed, then read the following articles.

https://ittutorial.org/oracle-database-performance-tuning-tutorial-12-what-is-the-automatic-sql-tuning-and-how-to-automated-sql-tuning/

 357 views last month,  1 views today

About Mehmet Salih Deveci

blank
I am Founder of SysDBASoft IT and IT Tutorial and Certified Expert about Oracle & SQL Server database, Goldengate, Exadata Machine, Oracle Database Appliance administrator with 10+years experience. I have OCA, OCP, OCE RAC Expert Certificates I have worked 100+ Banking, Insurance, Finance, Telco and etc. clients as a Consultant, Insource or Outsource. I have done 200+ Operations in this clients such as Exadata Installation & PoC & Migration & Upgrade, Oracle & SQL Server Database Upgrade, Oracle RAC Installation, SQL Server AlwaysOn Installation, Database Migration, Disaster Recovery, Backup Restore, Performance Tuning, Periodic Healthchecks. I have done 2000+ Table replication with Goldengate or SQL Server Replication tool for DWH Databases in many clients. If you need Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS Consultancy and Training you can send my email adress mehmetsalih.deveci@outlook.com. -                                                                                                                                                                                                                                                 - Oracle DBA, SQL Server DBA, APPS DBA,  Exadata, Goldengate, EBS ve linux Danışmanlık ve Eğitim için  mehmetsalih.deveci@outlook.com a mail atabilirsiniz.

Check Also

blank

Alter System Flush Shared pool in Oracle

Hi, I will explain Alter System Flush Shared pool in Oracle in this post.  5,992 views …

Leave a Reply