Tim Büthe wrote:
Quote:
Hi,
>
we are building a Java webapplication using JSF, running on websphere,
querying a DB2 9 on Suse Enterprise 10. The app uses JDBC and
PreparedStatements only (aka dynamic SQL). Every night, there is a ETL
which deletes most of the data in the database and fills it all new.
>
We observed very bad performance for some statements that ran for
minutes (The queried table is about 100,000 records and the result is
about 500 rows). Executing the same SQL in the control center took
round about 30 ms. After a while we realized that the difference
between our app and the control center, and some other tools we used
to execute the same query, is the use of dynamic and static SQL. We
change the source code to static SQL by removing all the wildcards and
putting the parameters right in the SQL-String and viola the
performance was good.
First of all, you're not describing static SQL. You're describing
dynamic SQL, where one statement uses parameter markers, and the
other does not.
select * from table where c1 = ? <= parameter marker
select * from table where c1 = 1
Both of these statements can be either dynamic SQL or static SQL,
and that depends on the application. PreparedStatements in java
are always dynamic SQL.
Anyway, this sounds like a clear case of distribution statistics
coming in to play.
When your query includes the values for each column, DB2 can use
distribution statistics that you may have collected to make a
better plan.
With a prepared statement that uses parameter markers, DB2 has to
create the executable plan with no knowledge of what value the
parameter marker will have. (i.e., it can't take advantage of
distribution statistics).
This latter case is why IBM added the "REOPT" ability to DB2. But
I am not quite sure if it's possible to take advantage of this
with dynamically prepared SQL statements in Java.