473,400 Members | 2,145 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,400 software developers and data experts.

Stored procedure slower than an ad-hoc query ?!

I have a stored procedure that wraps a moderately complex query over 5,
6 related tables. The performance of the procedure is unacceptably slow
as it takes on average 5-10 min to complete.
To diagnose the problem, I copied the query in db2 command editor, and
substituted the procedure parameters that appear in the query with
fixed values with intention to locate the parts that are slowing it
down. However when I run the query unmodified it returns almost
instantaneously (while calling the procedure with the same parameters
takes 10mins, and with btw no records returned).
I am expecting that a query inside a stored procedure would only run
faster then an ad-hoc one.
I am running DB2 8.2 on Linux.
Does anybody have an idea on what might be wrong there?
Thanks
Nesa

Apr 20 '06 #1
5 5062
Nesa wrote:
I have a stored procedure that wraps a moderately complex query over 5,
6 related tables. The performance of the procedure is unacceptably slow
as it takes on average 5-10 min to complete.
To diagnose the problem, I copied the query in db2 command editor, and
substituted the procedure parameters that appear in the query with
fixed values with intention to locate the parts that are slowing it
down. However when I run the query unmodified it returns almost
instantaneously (while calling the procedure with the same parameters
takes 10mins, and with btw no records returned).
I am expecting that a query inside a stored procedure would only run
faster then an ad-hoc one.
I am running DB2 8.2 on Linux.
Does anybody have an idea on what might be wrong there?
Thanks
Nesa

Try this:
--#SET TERMINATOR @
DROP PROCEDURE ...@
CALL SET_ROUTINE_OPTS('REOPT ONCE')@
CREATE PROCEDURE ...@
CALL SET_ROUTINE_OPTS(CAST(NULL AS VARCHAR))@

Now call the procedure.
You apparently got a better plan because Db2 could exploit the exact
values for optimization.

In addition to REOPT ONCE you can experiment with REOPT ALWAYS

Cheers
Serge
--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 20 '06 #2
Thanks for your reply. Unfortunately it didn't help. Tried with both
once and always but no difference on response time (at least not a
noticeable one).
Is there a tool I can use to pinpoint the problem?
Thanks
Nesa

Apr 20 '06 #3
Nesa wrote:
Thanks for your reply. Unfortunately it didn't help. Tried with both
once and always but no difference on response time (at least not a
noticeable one).
Is there a tool I can use to pinpoint the problem?
Thanks
Nesa

First get explains for both cases and make sure they are the same.
One more thought add the the options to 'BLOCKING ALL'.
Maybe the procedure doesn't use a blocked cursor.

Cheers
Serge

--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Apr 20 '06 #4
Nesa wrote:
Thanks for your reply. Unfortunately it didn't help. Tried with both
once and always but no difference on response time (at least not a
noticeable one).


Differences in the execution plans are most likely the main issue here.
What you have to remember is that the DB2 optimizer has better information
if you provide exact values. For example, if you say "col > ?" then DB2
does not know how many rows might qualify for this predicate. But if you
say "col > 10000", then the estimate will be much more accurate (assuming
up-to-date statistics).

Also note that there is no reason why a stored procedure running a single
query would be any faster than running the query ad-hoc at the CLP. At
most you could save the compilation time. But the actual execution has to
be performed in both cases.

You can also try to use "db2batch" (or snapshots) to narrow things down a
bit more.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Apr 21 '06 #5
Been there myself. :)

Place the exact values in the PROCEDURE itself and see how it runs. If
it runs slowly, there is a real issue. It should run just as quickly.

Then, start removing the parameters one by one and replace them with
the variables. See when it slows down. Usually, it is just one
parameter that ends up slowing it down. Check the optimizer's plan for
that specific clause.

Sometimes it simply optimnizes for an assumed number of records. Add
OPTIMIZE FOR x ROWS to the end of the query in the PROCEDURE or DYNAMIC
RESULT SETS x to the PROCEDURE declaration itself for better
performance, x being the actual numbewr of records returned right now.
Or use 1, or use 32000 to see what happens.

B.

Apr 21 '06 #6

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

Similar topics

4
by: deprins | last post by:
Hello, I have wrote a stored procedure but its real slow. Its activated by a button on web page but its takes to long to process and the web server gives a timeout message after 5 minutes. Is...
15
by: Philip Mette | last post by:
I am begginner at best so I hope someone that is better can help. I have a stored procedure that updates a view that I wrote using 2 cursors.(Kind of a Inner Loop) I wrote it this way Because I...
2
by: Kent Lewandowski | last post by:
hi all, Recently I wrote some stored procedures using java jdbc code (admittedly my first stab) and then tried to implement the same within java packages (for code reuse). I encountered...
8
by: John Reynolds | last post by:
Hi all, I have a software product that includes stored procedures using sql. Is there a way to obsfucate or otherwise NOT deliver readable source to a stored procedure? I don't really want to...
9
by: joun | last post by:
Hi all, i'm using this code to insert records into an Access table from asp.net, using a stored procedure, called qry_InsertData: PARAMETERS Long, Long, Text(20), Long, DateTime; INSERT...
1
by: kentk | last post by:
Is there a difference in how SQL Server 7 and SQL 2000 processes SQL passed from a program by an ADO command object. Reason I ask is I rewrote a couple applications a couple years ago were the SQL...
2
by: shahnawaz shaikh | last post by:
hi friends i want to send email thoruth stored procedure or i want to give functionality like alert which is send email automaticaly from database. just like a news or any ad email weekly or...
14
by: jehugaleahsa | last post by:
Hello: I am working with Oracle .NET Stored Procedures. I would like to know how to return the results of a SELECT statement. I have tried returning a OracleRefCursor and a DataTable, but...
3
by: gopi2ks | last post by:
Dear All, I have one stored procedure like sp_insertEmployee Employee Table Fileds Eno int pk, ename varchar(100), designation varchar
3
by: fabiomoggi | last post by:
Hello Guys, I am developing a web application to manage Active Directory resources, and one of my tasks is to map Organizational Units hierarchy into a SQL Server database. Let's suppose that I...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
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
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...

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.