| Author |
Topic |
|
MorrisK
Yak Posting Veteran
83 Posts |
Posted - 2011-10-06 : 15:13:45
|
| I've got a table with multiple columns that will need to be updated at least once a day. I'm wondering if it's OK to just write a single stored procedure that contains multiple updates or if it's best to write an sp for each update and call them from a "master" sp.Thanks in advance,Kevin |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
denis_the_thief
Aged Yak Warrior
596 Posts |
Posted - 2011-10-06 : 15:48:18
|
quote: Originally posted by tkizer It depends. You have to be careful with multiple code paths due to differing execution plans. Could you show us a sample of the updates?Tara Kizer
I'm curious what you mean? Since each query will have its own execution plan regardless of which Stored Procedure it belongs too. |
 |
|
|
MorrisK
Yak Posting Veteran
83 Posts |
Posted - 2011-10-06 : 15:48:53
|
| Here you go - --Update job statusUPDATE pSET JobComplete = 'Y',ActualCompleteDate = j.ActCompleteDate,PercentComplete = 1FROM dbo.MfgProgress pINNER JOIN dbo.JobMaster jON p.Job = j.JobWHERE j.Complete = 'Y'AND p.JobComplete <> 'Y'--Update wire cut statusUPDATE dbo.MfgProgressSET WCComplete = 'Y'WHERE Job NOT IN(SELECT Job FROM dbo.JobOperations WHERE Job IN (SELECT Job FROM dbo.MfgProgress) AND LEFT(WorkCentre, 2) = 'WC' AND OperCompleted = 'N')AND WCComplete <> 'Y'--Update shortage statusUPDATE dbo.MfgProgressSET Shortage = 'Y'WHERE Job IN(SELECT Job FROM dbo.OperationAllocations WHERE Job IN (SELECT Job FROM dbo.MfgProgress) AND JobShortageStatus = 'S')AND Shortage <> 'Y'--Update run time issuedUPDATE pSET RunTimeIssued = o.SUMRunTimeIssuedFROM dbo.MfgProgress pINNER JOIN (SELECT Job, SUM(RunTimeIssued) AS SUMRunTimeIssued FROM dbo.JobOperations GROUP BY Job) oON p.Job = o.JobWHERE RunTimeIssued <> o.SUMRunTimeIssued--Update expected run timeUPDATE pSET ExpectedRunTime = o.SUMICapcityReqdFROM dbo.MfgProgress pINNER JOIN (SELECT Job, SUM(ICapacityReqd) AS SUMICapcityReqd FROM dbo.JobOperations GROUP BY Job) oON p.Job = o.JobWHERE ExpectedRunTime <> o.SUMICapcityReqd--Update adjusted expected run timeUPDATE pSET AdjExpectedRunTime = o.SUMAdjExpRunTimeFROM dbo.MfgProgress pINNER JOIN (SELECT Job, SUM( CASE WHEN RunTimeIssued > ICapacityReqd THEN RunTimeIssued ELSE ICapacityReqd END ) AS SUMAdjExpRunTime FROM dbo.JobOperations GROUP BY Job) oON p.Job = o.JobWHERE AdjExpectedRunTime <> o.SUMAdjExpRunTime--Update percent completeUPDATE dbo.MfgProgressSET PercentComplete = RunTimeIssued/AdjExpectedRunTimeWHERE PercentComplete <> RunTimeIssued/AdjExpectedRunTimeAND PercentComplete <> 1 |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2011-10-06 : 15:52:06
|
quote: Originally posted by denis_the_thief
quote: Originally posted by tkizer It depends. You have to be careful with multiple code paths due to differing execution plans. Could you show us a sample of the updates?Tara Kizer
I'm curious what you mean? Since each query will have its own execution plan regardless of which Stored Procedure it belongs too.
A stored procedure can only have one execution plan for a given set of connection settings. Within that one plan can be multiple plans from each query. The multiple code paths will lead to problems when that one stored procedure plan doesn't cover the various combinations for the code paths. This type of thing is one example of a bad execution plan. Sure, it can recompile, but that can be costly if it runs frequently. If it doesn't recompile and the plan in cache doesn't cover your execution, well that's a bad plan. As a result, you should break them into their own stored procedures.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-10-06 : 15:59:21
|
quote: Originally posted by denis_the_thief
quote: Originally posted by tkizer It depends. You have to be careful with multiple code paths due to differing execution plans. Could you show us a sample of the updates?Tara Kizer
I'm curious what you mean? Since each query will have its own execution plan regardless of which Stored Procedure it belongs too.
Execution plans are generated per-batch, not per statement. So if you have multiple execution paths in a proc, all of them get optimised at the same time and the plan for the procedure gets reused and reused and reused.--Gail ShawSQL Server MVP |
 |
|
|
GilaMonster
Master Smack Fu Yak Hacker
4507 Posts |
Posted - 2011-10-06 : 16:02:08
|
quote: Originally posted by tkizer The multiple code paths will lead to problems when that one stored procedure plan doesn't cover the various combinations for the code paths. This type of thing is one example of a bad execution plan. Sure, it can recompile, but that can be costly if it runs frequently.
The generated plan will always cover all of the possible execution paths because the optimiser will optimise every query within the plan regardless or whether it can be executed or not. The optimiser doesn't evaluate conditionals.The procedure won't recompile when executed with different parameters and that reuse of the plan can cause poor performance if a query was optimised with a parameter value it can't be executed with.--Gail ShawSQL Server MVP |
 |
|
|
MorrisK
Yak Posting Veteran
83 Posts |
Posted - 2011-10-06 : 16:02:23
|
| OK. Thanks for the help.Kevin |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
MorrisK
Yak Posting Veteran
83 Posts |
Posted - 2011-10-06 : 16:09:48
|
quote: The procedure won't recompile when executed with different parameters and that reuse of the plan can cause poor performance if a query was optimised with a parameter value it can't be executed with.--Gail ShawSQL Server MVP
In this case there are no input parameters so does that make a difference?Kevin |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
jassi.singh
Posting Yak Master
122 Posts |
Posted - 2011-10-08 : 10:21:25
|
| Whatever way you choose but dont forget to implement transaction in you code.Please mark answer as accepted if it helped you.Thanks,Jassi Singh |
 |
|
|
|