Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Can this Routine Be Faster?
Message
 
To
All
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Can this Routine Be Faster?
Miscellaneous
Thread ID:
00801022
Message ID:
00801022
Views:
65
Hi everyone

I'm trying to speed up this routine, it is in a stored procedure but am showing it here from the SA. The statement calls a user-defined function to calculate an average cost by case, the overall query is to return the top number of procedures for a given time period. If I take out the line that calls the udf(), it returns in 0 seconds. With the routine it returns in 5 seconds. I remember when 5 seconds was cooking with gas :). The reason I use the UDF, is I couldn't seem to get around a grouping error on meetingnumber if I tried to use a subquery. It kept coming back saying that "meetingnumber" was invalid because it was not contained in an arrigant or group by clause. I can't AVG() on the subquery, and I can't have the data broken out by meetingnumber, or I'm down to a detail report. Anyway, if anyone has some pointers I'd appreciate it. I have included both the main query and UDF below:
declare @lnDeptID int,
@ltQueryFrom datetime,
@ltQueryTo datetime

set @lnDeptID=23
set @ltQueryFrom = '10/01/2002 00:00:00'
set @ltQueryTo   = '10/31/2002 23:59:59'

SELECT TOP 100 PERCENT 
	COUNT(dbo.meetings.meetingnumber) AS CaseCount, 
	dbo.coproc.dcode as Proc_Code, 
	dbo.coproc.description as Proc_Description,
	avg(datediff(mi,meetings.enteror,meetings.exitor)) as Avg_Minutes,
	avg(datediff(mi,meetings.enteror,meetings.exitor) * meetings.DeptCostPerMinute) as AvgORCost,
	avg(dbo.caseResourceCost(meetings.meetingnumber)) as AvgResCost,
	dbo.coProc.procid as ProcID
FROM    dbo.meetings INNER JOIN
        dbo.meetproc ON dbo.meetings.meetingnumber = dbo.meetproc.meetingnumber INNER JOIN
        dbo.coproc ON dbo.meetproc.procid = dbo.coproc.procid
WHERE   (dbo.meetings.iscompleted = 1) 
and     dbo.meetings.begintime between @ltQueryFrom and @ltQueryTo 
and 	meetings.deptID=@lnDeptID
GROUP BY dbo.coproc.procid, dbo.coproc.dcode, dbo.coproc.description
ORDER BY COUNT(dbo.meetings.meetingnumber) DESC
UDF
CREATE FUNCTION CaseResourceCost  (@meetID int)  
RETURNS decimal(12,2) AS  
BEGIN 
        Declare @returnValue decimal(12,2)
        select  @returnvalue=isnull(sum(cocasedata.itemunitcost * coCaseData.ActualQty),0)
	From coCaseData where coCaseData.meetingnumber=@meetID
      return (@returnValue)
END
Next
Reply
Map
View

Click here to load this message in the networking platform