Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Stored Procedure - possible to optimize?

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2008-08-05 : 08:06:28
Current sp works fine but the processing time is at the high end. Is there a possibility to optimize?

In the sp there are several "MAX(CASE..." statements with either "Yes" or "No" results. I'm not totally sure how the "max" works and if it is reading all the records. However, since the result is Yes or No I only need to check if there is at least one record (do not need to read thru the whole table). Can anyone tell me if the sp can be optimized? Thank you

SELECT	dbo.Job.CompanyJobID,
dbo.Job.ChangeDate as date,
dbo.Job.Name,
dbo.Region.CompanyRegionID,
dbo.Job.Active,
MAX(CASE dbo.SourceType.CompanySourceTypeID WHEN 'MA' THEN 'Yes' ELSE 'No' END) AS Material,
MAX(CASE dbo.SourceType.CompanySourceTypeID WHEN 'PR' THEN 'Yes' ELSE'No' END) AS Production,
MAX(CASE dbo.SourceType.CompanySourceTypeID WHEN 'SB' THEN 'Yes' ELSE'No' END) AS Sub,
MAX(CASE dbo.EquipmentLaborEvent.Hours WHEN 0 THEN 'No' ELSE 'Yes' END) AS Equipment,
MAX(CASE dbo.EmployeeLaborEvent.Hours WHEN 0 THEN 'No' ELSE 'Yes' END) AS Labor,
MAX(CASE dbo.Diary.DiaryEntry WHEN 0 THEN 'No' ELSE 'Yes' END) AS Diary
FROM dbo.Job
INNER JOIN dbo.Event ON dbo.Event.JobGuid = dbo.Job.JobGuid
INNER JOIN dbo.ProductionEvent ON dbo.ProductionEvent.EventGuid = dbo.Event.EventGuid
LEFT JOIN dbo.Product ON dbo.Product.ProductGuid = dbo.ProductionEvent.ProductGuid
LEFT JOIN dbo.Item ON dbo.Item.ItemGuid = dbo.Event.ItemGuid
INNER JOIN dbo.Source ON dbo.Source.SourceGuid = dbo.ProductionEvent.SourceGuid
INNER JOIN dbo.SourceType ON dbo.SourceType.SourceTypeGuid = dbo.Source.SourceTypeGuid
LEFT JOIN dbo.EquipmentLaborEvent ON dbo.EquipmentLaborEvent.EventGuid = dbo.Event.EventGuid
LEFT JOIN dbo.EmployeeLaborEvent ON dbo.EmployeeLaborEvent.EventGuid = dbo.Event.EventGuid
LEFT JOIN dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.EventGuid
LEFT JOIN dbo.Diary ON dbo.Diary.BatchGuid = dbo.Batch.BatchGuid
INNER JOIN #AuthorityTable AS at ON at.busunit_from <= dbo.Job.CompanyJobId
AND at.busunit_to >= dbo.Job.CompanyJobId
LEFT JOIN dbo.Region ON dbo.Region.RegionGuid = dbo.Job.RegionGuid
WHERE dbo.SourceType.CompanySourceTypeID IN ('PR', 'MA', 'SB')
AND (dbo.Job.CompanyJobID LIKE CAST(@JobNumber AS VARCHAR(10)) + '%' OR @JobNumber = 0 OR @JobNumber IS NULL)
AND (dbo.Job.Name LIKE '%' + @Text + '%' OR @Text IS NULL)
AND (dbo.Job.ChangeDate >= @DateFrom OR @DateFrom IS NULL)
AND (dbo.Job.ChangeDate <= @DateTo OR @DateTo IS NULL)
GROUP BY dbo.Job.CompanyJobID,
dbo.Job.ChangeDate,
dbo.Region.CompanyRegionID,
dbo.Job.Name,
dbo.Job.Active
ORDER BY dbo.Job.CompanyJobID

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-09-03 : 06:02:37
Check the execution plan.
I think you will find this line taking long time.
AND (dbo.Job.Name LIKE '%' + @Text + '%' OR @Text IS NULL)

because it can't use an index.

Also this JOIN is prone to "RBAR".
INNER JOIN #AuthorityTable AS at ON at.busunit_from <= dbo.Job.CompanyJobId
AND at.busunit_to >= dbo.Job.CompanyJobId




E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page
   

- Advertisement -