We have the following simple stored procedure that runs as an SQL agent job overnight. It usually starts after 20 minutes, but recently MatchEvent and MatchResult tables have grown to more than 9 million rows. This led to the storage procedure taking more than 2 hours, and all 8 GB of memory in our SQL box was used up. This makes the database inaccessible to regular queries trying to access it.
I assume that the problem is that the temporary table is too large and causes memory and database problems.
How can I rewrite a stored procedure to make it more efficient and less memory intensive?
Note. I edited SQL to indicate that a condition appeared on the original SELECT statement. I previously left this for simplicity. In addition, when the request is launched using the processor, it is 1-2%, but the memory, as indicated above, is maximized
CREATE TABLE
(
matchId VARCHAR(50)
)
INSERT INTO #tempMatchResult
SELECT MatchId FROM MatchResult WHERE SOME_CONDITION
DELETE FROM MatchEvent WHERE
MatchId IN (SELECT MatchId FROM #tempMatchResult)
DELETE FROM MatchResult WHERE
MatchId In (SELECT MatchId FROM #tempMatchResult)
DROP TABLE #tempMatchResult
source
share