fuzzyocelot
Programmer
System: SQL Server 2005 SP2
We recently discovered a stored procedure containing a large complicated select statement. When the stored procedure is executed by one person, the select statement shows up in an SQL Profiler trace about 500 times. The EventClass I used is "SP:Completed". The stored proc returns 50 records eventually or it times out.
I did not write this procedure. The person who wrote it is no longer here so we're stuck trying to figure it out on our own. The select statement contains 21 table joins alone. We believe the problem is that there is a left join on a user-defined function. When we comment out the function, we have no problems. Now we're not sure how to proceed from here.
Here is the left join containing the function.
Here is the function:
Any ideas how to fix this? Do I need to include the entire select statement from the stored procedure? I'm thinking that someone might need to rewrite this thing without using the user-defined function.
Thanks!
We recently discovered a stored procedure containing a large complicated select statement. When the stored procedure is executed by one person, the select statement shows up in an SQL Profiler trace about 500 times. The EventClass I used is "SP:Completed". The stored proc returns 50 records eventually or it times out.
I did not write this procedure. The person who wrote it is no longer here so we're stuck trying to figure it out on our own. The select statement contains 21 table joins alone. We believe the problem is that there is a left join on a user-defined function. When we comment out the function, we have no problems. Now we're not sure how to proceed from here.
Here is the left join containing the function.
Code:
LEFT JOIN tblPLP plp ON plp.inyRcdId = dbo.fnPLGP(c.intInc,c.inyHouseRcdId,getdate())
Here is the function:
Code:
ALTER FUNCTION [dbo].[fnPLGP]
(@intInc INT
,@inyHouseSize TINYINT = 1
,@dtsRDate SMALLDATETIME = GetDate)
RETURNS TINYINT
AS
BEGIN
DECLARE @inyRcdId TINYINT
SET @inyRcdId = (SELECT MIN(plr.inyRcdId)
FROM (SELECT *
FROM dbo.tblPLR
WHERE inyRcdId NOT IN (6,7)) plr
JOIN dbo.tblPLP pld
ON plr.insRcdId = pld.insRcdId
WHERE plr.inyHouseSize = @inyHouseSize
AND pld.dtsStartDate < @dtsRDate
AND ISNULL(pld.dtsEndDate,'12/12/2078') >= @dtsRDate
AND plr.intMinInc <= @intInc
AND ISNULL(plr.intMaxInc,@intInc) >= @intInc)
RETURN (@inyRcdId)
END
Any ideas how to fix this? Do I need to include the entire select statement from the stored procedure? I'm thinking that someone might need to rewrite this thing without using the user-defined function.
Thanks!