We have a production stored procedure that executes in 2-3 seconds everywhere except one client's environment.
Their environment appears healthy with 24 cores, 64 GB RAM and is nowhere near capacity. The SQL Server is 2008 r2 SP2.
I have restored the DB in my testing environment and the sproc returns in 2 seconds, but takes 20-50 minutes in the client's environment.
Today I setup a new instance of SQL Server on the same server and it also takes 20-50 minutes to execute the stored procedure.
Our DBA found the problem statement and devised a workaround.
Original:
--This returns in 30 minutes.
SELECT DISTINCT
P.ProjectID, P.ProjectName
FROM DocumentRoute DR
LEFT JOIN Routes R
ON R.RouteID = DR.RouteID
INNER JOIN Documents D
ON D.DocumentID = DR.DocumentID
AND D.Status = 1
INNER JOIN Files F
ON F.FileID = D.FileID
AND F.Status = 1
INNER JOIN Projects P
ON P.ProjectID = F.ProjectID
AND P.Status = 1
LEFT OUTER JOIN Users U
ON U.UserID = DR.UserID
WHERE DR.Status = 1
Original Execution Plan here: http://screencast.com/t/xGcRIE9o
Workaround:
--This returns in 2 seconds.
SELECT DISTINCT
P.ProjectID, P.ProjectName
FROM DocumentRoute DR
LEFT JOIN Routes R
ON R.RouteID = DR.RouteID
AND DR.Status = 1
INNER JOIN Documents D
ON D.DocumentID = DR.DocumentID
AND D.Status = 1
INNER JOIN Files F
ON F.FileID = D.FileID
AND F.Status = 1
INNER JOIN Projects P
ON P.ProjectID = F.ProjectID
AND P.Status = 1
LEFT OUTER JOIN Users U
ON U.UserID = DR.UserID
Revised Execution Plan here: http://screencast.com/t/Fqg90w6NDyZd
What in the client's environment could possibly account for the massive difference in execution time?
Additional info: When I got the execution plans from the client, the problem statement by itself finished in 17 seconds, but the entire sproc has been running for 15+ minutes and will probably take another 15 to finish.