September 22, 2008 at 2:59 am
I have a SP which has code somthing like this.
if (condition1)
begin
insert into #temp1
select * from table1
where clause1
end
if (condition2)
begin
insert into #temp1
select * from table1
where clause2
end
And so on..and there are approximately 20 conditions..
How can I break it smaller modules ?
September 22, 2008 at 4:05 am
Just call another SP in the condition... But would you really want to have 20 SP's each one being called by the other to do the one job? as it would get very messy. I would just have the 20 conditions in the SP (as you have) but just comment them clearly... I'm guessing the SP writes to the one table etc?
September 22, 2008 at 5:14 am
Yes...SP is writing to a single temporary table based on the condition and then that temporary table is returned at the end.
September 22, 2008 at 4:49 pm
we reaped huge benefits from breaking a stored procedure down like that. We ended up having a master stored procedure that had the most used condition in the first If and then it would send them to another stored procedure. Each additional condition was listed below that in order of usage.
We shaved things down from 30 secs to 2 secs in most cases.
MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
September 22, 2008 at 6:26 pm
The only thing different in any of the original code is the IF and the WHERE... why on Earth would anyone break this into 20 separate stored procedures?
Anjali, if you want a better answer to this, we'll need some better information... like what the conditions are and what the Where clause contains. 😉
--Jeff Moden
Change is inevitable... Change for the better is not.
September 22, 2008 at 6:33 pm
my response about our improvement wasn't relative to his specific example. I only stated that we had seen great improvement in our situation and it all involves testing.
MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
September 22, 2008 at 10:15 pm
Heh... and I wasn't responding to your particular post, either. In general, it seems that the direction of this thread is to split the original post into 20 stored procs... definitely NOT the way to do this. It would require duplication of code 20 times for no reason. 🙂
--Jeff Moden
Change is inevitable... Change for the better is not.
September 23, 2008 at 7:05 am
we had a variety of variables that could be there or not, so there would be conditions that determine the sp.
Simple example would be. There are more variables but that's where it was going.
if @a = 1
Begin
exec sp_1
End
If @b-2=1
Begin
exec sp_2
END
MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
September 23, 2008 at 11:33 pm
But that would mean creating 20 SP's more...that would be more difficult to manage. Any other possible solution ?
September 24, 2008 at 6:10 am
Well, look at the idea of a stored proc not needing to recompile everytime. If you are passing it different variables in each condition, we've had the best experiencing when breaking them up. It's not perfect but it really helped in our situation. Yes, it creates more stored procedures to manage, but each time that child stored proc is called it will have the same variables in it and it should have a plan cached.
There may be other options but that one fit us best.
MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
September 24, 2008 at 8:14 am
anjali.vishwakarma (9/23/2008)
But that would mean creating 20 SP's more...that would be more difficult to manage. Any other possible solution ?
You asked about breaking it down into smaller modules and the only way to do that in SQL Server is to create either more stored procedures or functions.
If there are significant logic differences and/or where conditions for each criteria, I agree with Jason that, performance-wise, breaking them out into child procedures will likely perform better. You obviously had or anticipate some issue with maintenance or performance of the current procedure or you wouldn't have asked the question.
Another benefit I see in breaking down the procedures is that you could choose to move the conditional logic to the application and call the specific procedure directly.
I do also agree with Jeff, that without specifics we can't give a specific answer.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
September 24, 2008 at 9:59 am
We just had this issue yesterday where the Query Plan is not what it should be using, and a sp_recompile took the query from 10 secs to 1~2 secs
This is what you may get if IF conditions are very different
Instead of 20 new SPs, I suppose you could find the optimized Query Plan Guide for each
and in each IF condition SELECT statements, call it with the Query Plan Guide hint so it'll use THAT particular plan each time
September 24, 2008 at 6:00 pm
Folks, please... go back and look at the original post... the queries are identical except for the IF and the WHERE... just move the IF to the WHERE and be done with it.
--Jeff Moden
Change is inevitable... Change for the better is not.
September 24, 2008 at 6:35 pm
Jeff Moden (9/24/2008)
Folks, please... go back and look at the original post... the queries are identical except for the IF and the WHERE... just move the IF to the WHERE and be done with it.
I agree that given the example, Jeff's method would likely be the method I implement.
If you're really hell-bent on trying to reduce the amount of duplication of actual SQL code, then you might consider dynamic code (store the base SQL in one string, dynamic smack on a where clause, and execute the result), but again - I'd do that only if the query I was copying over and over was complex.
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
September 24, 2008 at 6:46 pm
Hey MATT! Congrats on becoming a proud member of the over 5k crowd!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 15 posts - 1 through 15 (of 21 total)
You must be logged in to reply to this topic. Login to reply