473,698 Members | 2,295 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 7967
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
6963
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
2771
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
5956
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
1968
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
2457
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
3701
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
2960
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
7697
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
9007
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
8674
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8604
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
9028
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
8895
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
7728
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...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3046
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
2330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.