473,881 Members | 1,639 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 7981
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
6970
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
2776
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
5965
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
1979
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
2465
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
3713
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
2967
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
7714
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
9013
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
9776
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11100
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
10718
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...
0
10401
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
9554
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
5781
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...
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3225
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.