Cursor in SQL Server taking too much time for 35 million records

霸气de小男生 提交于 2019-12-24 06:11:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!