473,508 Members | 2,308 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance Improvement of Stored Procedure

46 New Member
I have a stored procedure which is being used on an online form.

In the form the user is able to ask for records based on an Age. So 5 days old, 2 days old, etc. The age is based on the date that the record was entered or received. So we have JobReceiveDate.


The stored procedure was written like this. Where @Ageto and @AgeFrom come from the online form.

Expand|Select|Wrap|Line Numbers
  1. JobReceivedDate Between DateAdd(dd, (isnull(@AgeTo,60000)* -1),JobReceivedDate) AND DateAdd(dd, (isnull(@AgeFrom,0)* -1),JobReceivedDate) 

In doing this the query can not use the index on set up for JobReceiveDate.

Can anyone think of a better way to write this code? I can't think of how I could totally remove the function so that the index will be utilized.
Mar 10 '12 #1
5 2167
Rabbit
12,516 Recognized Expert Moderator MVP
I'm not even sure what that code is supposed to do. As far as I can tell, it will always return every record or no records.

If you're trying to filter by age, you have to take into account the current date, which you never do.

It really needs to be something like this
Expand|Select|Wrap|Line Numbers
  1. dateField BETWEEN (GETDATE() - 5) AND GETDATE()
Mar 11 '12 #2
Vikki McCormick
46 New Member
Hmmm... I didn't write it, but that code snippet is in the where clause which prohibits the index utilization.

I agree. What does this do?? I was assigned to re-write the stored procedure. It was over 13 pages when I printed it and included 10 seperate union queries. I was able to combine them all into 1 select atatement but it's still pretty complex, and I am trying now to re-write the predicates. It was running for 13 mintues, now it's running for 15 seconds, but I can do better, if I can resolve this last issue.

There were no requirements but the statement seems to be set up to indicating that there are multiple records based on a jobreceived date. And they want to see all records between like 0 and 5 days from jobreceivedate, but I think maybe you are right. Maybe they just want age based on getdate() which would actually make sense.

Okay. Thanks. I am going to have to go back and run that by someone.

I had pneumonia for the past 2 weeks. A co-worker thinks it was this sproc. Hehe. It's a possibility.

That was really a help.
Mar 11 '12 #3
Rabbit
12,516 Recognized Expert Moderator MVP
As it was, the filter in there didn't do anything but return the wrong results. What was preventing the use of the index was that calculations were being done on the fields. If possible, that should be avoided and all calculations should be moved to the constant side of the equation.

For example
Expand|Select|Wrap|Line Numbers
  1. someField + 5 > 10
  2. someField > 5
The first won't use an index but the second will.

If a calculation on a field is required, you can use either an index with the calculation or use a calculated field and check the option to store it.
Mar 11 '12 #4
Vikki McCormick
46 New Member
Ah right. Thanks. Got it. That was a big help. Can't wait to implement. Then finally I can get it off my task list. :)

Have a great week.
Mar 12 '12 #5
Rabbit
12,516 Recognized Expert Moderator MVP
There have been a couple of posts recently about query optimization and that motivated me to write an article. Take a look at it if you need to further optimize your queries.
How To: Optimize SQL Queries
Mar 12 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

12
8319
by: serge | last post by:
I have an SP that is big, huge, 700-800 lines. I am not an expert but I need to figure out every possible way that I can improve the performance speed of this SP. In the next couple of weeks I...
3
4253
by: Vagif Abilov | last post by:
Hello, I have a question regarding stored procedure desing that provides the optimal performance. Let's say we have a table Products that consists of three columns: Name, Status,...
10
3719
by: Thomas R. Hummel | last post by:
I have a stored procedure that suddenly started performing horribly. The query plan didn't look right to me, so I copy/pasted the code and ran it (it's a single SELECT statement). That ran pretty...
1
1887
by: Peter Arrenbrecht Opus | last post by:
Hello IBM I think that one could improve the performance of DB2 UDB v7.2's stored procedure resolution. Here's what DB2 normally does: SELECT A.PROCSCHEMA, A.PROCNAME, A.PARMNAME,...
3
2587
by: Peter Arrenbrecht | last post by:
Hi all I while ago I posted a suggestion to the DB2 newsgroup: ...
2
5437
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
9
1573
by: wdwedw | last post by:
I have included all the source codes in the attached MyTest.zip (http://www.codeguru.com/forum/attachment.php?attachmentid=11218) There are three projects: VBTestCOM project is a apartment...
7
3194
by: JIM.H. | last post by:
Hello, Is there any difference to between SLQ string in the code and call execute query and call a stored procedure and execute the query that way concerning speed, effectiveness, reliability,...
10
1895
by: db2udbgirl | last post by:
I have coded a stored procedure which is planned to run only once in production to process historic data from DB2 8.2 database. It accesses data from a partitioned table and based on some...
11
3392
by: peter | last post by:
I am trying to get a SQL stored procedure to use user maintained MQT implicitly which raises questions on when they are used or not used. In theory you would expect the stored procedure to pick up...
0
7225
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,...
0
7324
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
7382
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
7495
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...
0
5627
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,...
1
5052
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...
0
4707
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...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
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.