问题
I have created a cursor in a stored procedure, my select statement result has around 35 million records.
I am fetching each value of cursor and executing the stored procedure, in turn the next stored procedure is calling another stored procedure which has a few insert statements.
The query is taking more than 2 days to complete its execution, each time a cursor row takes 0.7 second, and there are 35 million rows.
The query is as below:
-- Created a cursor to read over data
DECLARE testCursor CURSOR FAST_FORWARD LOCAL FOR
SELECT
StartTime, EndTime, [datares], Id, address,
c.RCount, c.SCount
FROM
ConData c
-- Opening the cursor and fetching the first Condata
OPEN testCursor
FETCH NEXT FROM testCursor INTO
@startTime, @endTime, @datares, @Id, @address, @rCount, @sCount
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC InsertOrUpdateConData @startTime, @endTime, @datares, @Id, @address, @rCount, @sCount
UPDATE DataRec
SET EndTime = @endTime
WHERE Id = @pId AND Type = 'ConData' AND Int1 = @Id
-- Fetch the next conversation
FETCH NEXT FROM testCursor INTO
@startTime, @endTime, @datares, @Id, @address, @rCount, @sCount
END
CLOSE testCursor
DEALLOCATE testCursor
How can I reduce the amount of time required for this query to execute?
来源:https://stackoverflow.com/questions/55334261/cursor-in-sql-server-taking-too-much-time-for-35-million-records