473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

JDBC / Oracle : beware the bind variables ?

Hello all,

I noticed a difference in the explain plans between JDBC using bind
variables (PreparedStatement) and SQLPlus for the same query. The query made
through JDBC using bind variables makes a full table scan, while the query
made in SQLPlus, replacing the "?" by string literals uses an index.
(Platform: Oracle 10.1 / Linux RedHat 9 / JDK 1.4.2 / Oracle 10g thin JDBC
driver )

As a common practice is to write and optimize queries using SQLPLus or Toad
then to replace the literals by bind variables in the Java code, the actual
performance of the queries may be lower than expected.

I found a good explanation of the reason:
"The cost based optimizer (that 's what we 're talking about, not) makes its
choices based on the availability of indexes (among other objects), and the
distribution of values in the indexes (how selective the index will be for a
given value). Obviuosly, when working with bind variables, the suitability
of the index from a distribution point of view is harder to determine. The
optimizer has no way to determine beforehand to what value matches will be
sought. This might (should) lead to another execution plan. No surprise
here, as far as I am concerned."
[
http://groups.google.com/groups?hl=e...C24A8%40hp.com ]

In the situation i ran into, even a hint didn't correct the problem - i had
to avoid using bind variables.

This is completely opposed to the common idea that PreparedStatement is more
efficient for "repeatedly" executed queries.

Achille Carette
------------------------------------------
ac***@nospam.infonie.be
Jul 19 '05 #1
1 25392

"Achille Carette" <ac***@nospam.infonie.be> wrote in message
news:GM**************@amsnews02.chello.com...
Hello all,

I noticed a difference in the explain plans between JDBC using bind
variables (PreparedStatement) and SQLPlus for the same query. The query made through JDBC using bind variables makes a full table scan, while the query
made in SQLPlus, replacing the "?" by string literals uses an index.
(Platform: Oracle 10.1 / Linux RedHat 9 / JDK 1.4.2 / Oracle 10g thin JDBC
driver )

As a common practice is to write and optimize queries using SQLPLus or Toad then to replace the literals by bind variables in the Java code, the actual performance of the queries may be lower than expected.

I found a good explanation of the reason:
"The cost based optimizer (that 's what we 're talking about, not) makes its choices based on the availability of indexes (among other objects), and the distribution of values in the indexes (how selective the index will be for a given value). Obviuosly, when working with bind variables, the suitability
of the index from a distribution point of view is harder to determine. The
optimizer has no way to determine beforehand to what value matches will be
sought. This might (should) lead to another execution plan. No surprise
here, as far as I am concerned."
[
http://groups.google.com/groups?hl=e...C24A8%40hp.com ]
In the situation i ran into, even a hint didn't correct the problem - i had to avoid using bind variables.

This is completely opposed to the common idea that PreparedStatement is more efficient for "repeatedly" executed queries.

Achille Carette
------------------------------------------
ac***@nospam.infonie.be

Yes. for example lets say you did
select * from emp where sex='F';
and there were a million emp records and only 5 of those meet the condition.
(and the tables are analyzed) then the index on sex would be faster in the
above case than if you were looking for 'M'.(assuming a not null condition)
In this special case bind variables would solve it generically and would
probably do a full table scan. In fact in datawharehousing it usually is
preferable to not use bind variables. In OLTP it is highly preferable to
use bind variables. In one version of Oracle , and I thought 10G would have
it, it will do bind variable peeking. (still on 8.1.7.4, so I haven't
looked into it.)\

So usually it is much better to use bind variables especially in an OLTP
application. Also a full table scan is not necessarily bad.

Jim
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jack | last post by:
Hi All, What is the PHP equivilent of Oracle bind variables in a SQL statement, e.g. select x from y where z=:parameter Which in asp/jsp would be followed by some statements to bind a value...
0
by: Michael.Coll-Barth | last post by:
All, As the subject line says, I am having some trouble passing bind variables to Oracle. When I pass a printable string value, such as 'NM', I get data. When I try to pass a non-printable...
1
by: Daniel Roy | last post by:
Hi gurus, I just started to look at a very slow-running SQL statement generated by an application (Siebel). I spooled the SQL from the application, replaced the bind variables by their values, and...
3
by: RDS | last post by:
Does anyone know how to do the following vb6 code using bind variables for oracle dbs, in .net? dim cmdMyCommand as ADODB.COMMAND Set cmdMyCommand = New ADODB.COMMAND With cmdMyCommand...
70
kiss07
by: kiss07 | last post by:
Hi, i want 2 and 3 rd max salary in a table .i want a single query. please help.. Arun
0
by: Shawn Mason | last post by:
We added bind variables to our Oracle calls and once this began getting activity the .net runtime would have an error (as indicated by the event viewer logs). This of course resetst the session...
2
by: Dave | last post by:
ilee@bigpond.net.au (Ivan) wrote in message news:<90137e1b.0403301523.55aed707@posting.google.com>... Ivan - I little more specific to the java syntax. Right now you are probably using an...
0
by: Achille Carette | last post by:
Hello all, I noticed a difference in the explain plans between JDBC using bind variables (PreparedStatement) and SQLPlus for the same query. The query made through JDBC using bind variables...
6
by: mikaelD | last post by:
Hello I've been trying to connect to an oracle db for the last two days and that doesn't work. I solved all the problems concerning configuration and drivers but now my script just freezes at the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.