CodexBloom - Programming Q&A Platform

SQL Server 2016: Performance issues with large result sets in stored procedure returning CURSOR

πŸ‘€ Views: 72 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-12
sql-server performance stored-procedures csharp

I'm experiencing significant performance issues when executing a stored procedure that returns a CURSOR with a large result set (over 100,000 rows) in SQL Server 2016. The stored procedure is designed to fetch data from multiple tables, aggregate some values, and return results for further processing in an application. However, when I try to fetch data from the cursor in my application, it takes an exceedingly long time, even leading to timeouts. Here's a simplified version of the stored procedure: ```sql CREATE PROCEDURE GetLargeDataSet AS BEGIN DECLARE @DataCursor CURSOR; SET @DataCursor = CURSOR FOR SELECT Column1, Column2, SUM(Column3) FROM LargeTable GROUP BY Column1, Column2; OPEN @DataCursor; -- Fetching rows to process END; ``` In my application, I execute the stored procedure and then start fetching results like this: ```csharp using (SqlConnection conn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand("GetLargeDataSet", conn); cmd.CommandType = CommandType.StoredProcedure; conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { // Process each row } } } ``` I've tried increasing the timeout settings on both the connection and the command objects, but that doesn’t seem to help. Additionally, I attempted to switch the cursor to a local scope and used `FORWARD_ONLY` and `FAST_FORWARD` options, but the performance improvement was minimal. Is there a better way to handle large result sets in SQL Server, or should I consider returning a different format like a table variable instead of a CURSOR? Any insights on this would be greatly appreciated! I recently upgraded to Csharp 3.9. Any advice would be much appreciated.