473,729 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why is my static SQL dramatically faster than dynamic?

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
PreparedStateme nts 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.

That all seems to me, like there is a problem with the execution plans
or something like this and by using static sql, we force it to
recalculate the plans for the given statement. I already tried to
clear the package cache by invoking "flush package cache dynamic" but
had no luck.

My questions are:
1. Does my theory sounds plausible?
2. How to get rid off the bad execution plans?
3. Why there are bad execution plans? Is this because we reimport all
the data every night?

Thanks for your help!

Cheers,
Tim
Sep 29 '08 #1
6 7971
Ian
Tim Büthe wrote:
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
PreparedStateme nts 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. PreparedStateme nts 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.

Sep 29 '08 #2
On Sep 29, 3:17*pm, Ian <ianb...@mobile audio.comwrote:
Tim Büthe wrote:
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
PreparedStateme nts 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. *PreparedStatem ents 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.- Hide quoted text -

- Show quoted text -
Good thread. As a DB2 novice, here is my 2c. I had a similar case.
I managed to improve the performance by making the table volatile and
only having one index (which is the primary key in which c1 is the
leading column).

Question for Ian. I'm confused by the concept of dynamic vs. static
SQLs in DB2. So any prepared SQLs are dynamic? How about SQLs in
stored procedures? If my memory serves me, the dynamic SQL in Oracle
or Sybase is any SQL that is built on run-time and executed using a
special system stored proc. Seems to make more sense to me. Thanks.
Sep 29 '08 #3
Ian
Henry J. wrote:
>
Question for Ian. I'm confused by the concept of dynamic vs. static
SQLs in DB2. So any prepared SQLs are dynamic? How about SQLs in
stored procedures? If my memory serves me, the dynamic SQL in Oracle
or Sybase is any SQL that is built on run-time and executed using a
special system stored proc. Seems to make more sense to me. Thanks.
Static SQL has a fixed query plan that is determined at compile (bind)
time. The plans are stored in the system catalog (as packages). For
Java, static SQL is possible using SQLJ; in C you're writing
standard embedded SQL. You can see examples of this in the sample
application code (look for files named *.sqc or *.sqlj).

In both cases, you run the application code through the DB2 precompiler,
which produces compiler-ready code and the package, which you'll bind
into the database.

SQL Stored procedures can contain both static and dynamic SQL.
Generally any SELECT/INSERT/UPDATE/DELETE statements in the stored
procedure are static unless you're using the PREPARE/EXECUTE
or EXECUTE IMMEDIATE statements.
Sep 30 '08 #4
On Sep 30, 1:12*pm, Ian <ianb...@mobile audio.comwrote:
Henry J. wrote:
Question for Ian. *I'm confused by the concept of dynamic vs. static
SQLs in DB2. *So any prepared SQLs are dynamic? *How about SQLs in
stored procedures? *If my memory serves me, the dynamic SQL in Oracle
or Sybase is any SQL that is built on run-time and executed using a
special system stored proc. *Seems to make more sense to me. *Thanks.

Static SQL has a fixed query plan that is determined at compile (bind)
time. *The plans are stored in the system catalog (as packages). *For
Java, static SQL is possible using SQLJ; *in C you're writing
standard embedded SQL. *You can see examples of this in the sample
application code (look for files named *.sqc or *.sqlj).

In both cases, you run the application code through the DB2 precompiler,
which produces compiler-ready code and the package, which you'll bind
into the database.

SQL Stored procedures can contain both static and dynamic SQL.
Generally any SELECT/INSERT/UPDATE/DELETE statements in the stored
procedure are static unless you're using the PREPARE/EXECUTE
or EXECUTE IMMEDIATE statements.
Thanks very much for the detailed explanations, Ian.
Sep 30 '08 #5
On Sep 30, 1:12*pm, Ian <ianb...@mobile audio.comwrote:
Henry J. wrote:
Question for Ian. *I'm confused by the concept of dynamic vs. static
SQLs in DB2. *So any prepared SQLs are dynamic? *How about SQLs in
stored procedures? *If my memory serves me, the dynamic SQL in Oracle
or Sybase is any SQL that is built on run-time and executed using a
special system stored proc. *Seems to make more sense to me. *Thanks.

Static SQL has a fixed query plan that is determined at compile (bind)
time. *The plans are stored in the system catalog (as packages). *For
Java, static SQL is possible using SQLJ; *in C you're writing
standard embedded SQL. *You can see examples of this in the sample
application code (look for files named *.sqc or *.sqlj).

In both cases, you run the application code through the DB2 precompiler,
which produces compiler-ready code and the package, which you'll bind
into the database.

SQL Stored procedures can contain both static and dynamic SQL.
Generally any SELECT/INSERT/UPDATE/DELETE statements in the stored
procedure are static unless you're using the PREPARE/EXECUTE
or EXECUTE IMMEDIATE statements.
Thanks very much for the detailed explanations, Ian.
Sep 30 '08 #6
Hi,

first of all, thanks for your reply.

Ian schrieb:
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.
I see, got that wrong.
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.
Yes, I googled a lot on that topic and found that REOPT option. From
my understanding, setting REOPT=always is nearly the same as using
Statements instead of PreparedStateme nts or just don't use parameter
markers. Since we've wrapped the PreparedStateme nt class, I can build
an option in, that will replace all markers before sending the SQL.
Maybe that's a workaround...

I still don't understand why DB2 does so bad, even with bad execution
plans and full index scans, execution time of four minutes is a mess.

Tanks again,
Tim
Oct 1 '08 #7

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

Similar topics

1
6964
by: Eduardo Suárez | last post by:
Hi, i have tried to understand how static variables work on a dynamic library, but there's a point i can't see. * Is the data segment (where static variables live, please confirm) copied for every binary that is running linked to the library? This is what it seems from my own experiments. That is, there is a copy of every static variable for every binary linked. Then, is it really useful the library?
1
2773
by: sekhar_ps | last post by:
how would you find static linkage and dynamic linkage in a program?i mean how would you find the function is static linked or dynamically linked
1
5960
by: srikar | last post by:
what is the difference between static linking & dynamic linking, what are the advantages of each? How to perform static linking & Dynamic linking by using gcc -o liniking will be done , but how can we control the type of linking Hi any one please help me to clarify my doubt
4
1974
by: sealo | last post by:
Hello, I have a test in VS2005 that static library A.lib use the dynamic library C.dll. Then a application App use the A.lib. App-->A.lib-->C.dll It works. But if I remove the C.dll, the app tell me can not find the C.dll. I think because A.lib is a static library, so it should contain the
2
2459
by: steven | last post by:
I have heard static html is a good way to do the default.html as it is faster and save resource hits over aspx for example. But what if you want to generate and overwrite the static html pages every day. If you had a busy site like amazon the html pages would always be open and busy by users hitting the web site every second. So how could the update write of the page work. Plus, what if you wanted ads on your page and you were using...
4
3704
by: kukuta | last post by:
Hi I have static library that is created from VC++.Net, however, I would like to use it in Borland C++ and I do any ways but it is not successful. So I want to ask: by the way can I use static library in Borland C++? Or can I change the static library into dynamic library? If the second way can be, can you tell me the ways? If any ways above cannot do, please show me another way. Thanks alot. Any instructions and any ideas are very...
1
2963
by: Suleman | last post by:
Write a Program in some language that has both static and stack-dynamic local variables in subprogrms. Create six large (at least 100*100 ) matrices in the subprogram,three static and three stack dynamic. Fill two of the static matrices and two of the stack-dynamic matrices with random numbers in the range of 1 to 100.The code in the subprogram must perform a large no. of matrix multiplication operations on the static matrices and time the...
11
7704
by: C C++ C++ | last post by:
Hi all, got this interview question please respond. How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Rgrds MA
2
9008
by: shariquehabib | last post by:
Hi All, Can any one let me know the correct diffrence between static library and dynamic library while using C language? ------------- Sharique
0
9426
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9280
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9200
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9142
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8144
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2162
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.