{"id":78948,"date":"2026-11-10T19:45:19","date_gmt":"2026-11-10T19:45:19","guid":{"rendered":"http:\/\/bangla.sitestree.com\/?p=78948"},"modified":"2026-11-10T19:56:08","modified_gmt":"2026-11-10T19:56:08","slug":"rewritten-sql-server-69247","status":"publish","type":"post","link":"http:\/\/bangla.sitestree.com\/?p=78948","title":{"rendered":"Improving SQL Server Cursor Performance: Practical Options and Set-Based Alternatives"},"content":{"rendered":"<p>Cursors can become costly when they fetch many records and run additional queries inside a loop. Optimization therefore begins by asking whether row-by-row work is necessary at all.<\/p>\n<h2>Reduce the input<\/h2>\n<p>Filter rows before declaring the cursor, select only required columns, and confirm that the query uses appropriate indexes.<\/p>\n<h2>Choose options deliberately<\/h2>\n<p>A LOCAL FAST_FORWARD cursor is often suitable for a forward-only, read-only iteration. Static, keyset, dynamic, and updatable cursors have different semantics and trade-offs, so measure rather than assuming one type is always best.<\/p>\n<h2>Prefer set-based changes<\/h2>\n<pre><code>UPDATE p\nSET p.IsActive = 0\nFROM dbo.Products AS p\nWHERE p.DiscontinuedDate &lt; CONVERT(date, SYSDATETIME())\n  AND p.IsActive = 1;<\/code><\/pre>\n<h2>Avoid per-row network work<\/h2>\n<p>Do not issue a separate database round trip for each row when a bulk operation can produce the same result. If an external API must be called, consider queue-based processing and explicit retry logic.<\/p>\n<h2>Cleanup and benchmarking<\/h2>\n<p>Close and deallocate cursors, avoid long locks, and compare logical reads, CPU, duration, and contention on a representative test dataset.<\/p>\n<hr\/>\n<p><strong>AI disclosure and reuse:<\/strong> This AI-generated article is rewritten from <a href=\"https:\/\/bangla.sitestree.com\/?p=69247\">an existing Open Education post (ID 69247)<\/a>. Additional explanations, code examples, and version notes may have been added. You may reuse the newly generated article text with or without attribution. Third-party trademarks and linked works retain their own rights. <strong>AI can make mistakes.<\/strong> Check examples, permissions, supported SQL Server versions, and documentation before use in any live system.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cursors can become costly when they fetch many records and run additional queries inside a loop. Optimization therefore begins by asking whether row-by-row work is necessary at all.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,182],"tags":[466],"class_list":["post-78948","post","type-post","status-publish","format-standard","hentry","category-information-technology","category---blog","tag-sql-server","item-wrap"],"jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":69249,"url":"http:\/\/bangla.sitestree.com\/?p=69249","url_meta":{"origin":78948,"position":0},"title":"What are cursors? #38","author":"Author-Check- Article-or-Video","date":"August 15, 2021","format":false,"excerpt":"Cursors are server side database objects that are used by applications to apply operations on the database table data on a row-by-row basis. The operations may vary from one row to another row dynamically based on the requirements (business logic) and also multiple operations can be performed on the same\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78947,"url":"http:\/\/bangla.sitestree.com\/?p=78947","url_meta":{"origin":78948,"position":1},"title":"SQL Server Cursors: How They Work and a Complete T-SQL Example","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"A cursor provides row-by-row navigation through a query result. It is helpful when a task genuinely needs sequential procedural work, but should not replace set-based SQL without a reason.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":69247,"url":"http:\/\/bangla.sitestree.com\/?p=69247","url_meta":{"origin":78948,"position":2},"title":"How to optimize SQL Server Cursors #38","author":"Author-Check- Article-or-Video","date":"August 15, 2021","format":false,"excerpt":"Optimize Cursors Avoid using SQL Server cursors whenever possible Always close SQL Server cursors when result sets are not needed Deallocate SQL Server cursors when the data structures comprising the cursors are not needed Reduce the number of records to process in the cursor Only use the required columns in\u2026","rel":"","context":"In &quot;FromSitesTree.com&quot;","block_context":{"text":"FromSitesTree.com","link":"http:\/\/bangla.sitestree.com\/?cat=1917"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78937,"url":"http:\/\/bangla.sitestree.com\/?p=78937","url_meta":{"origin":78948,"position":3},"title":"SQL Server Administration Study Notes: Performance, Cursors, Indexes, and Recovery","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"This study guide brings together major SQL Server administration concepts: measuring performance, using indexes, diagnosing slow queries, avoiding inefficient cursors, planning scalability, and protecting recoverability.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":78926,"url":"http:\/\/bangla.sitestree.com\/?p=78926","url_meta":{"origin":78948,"position":4},"title":"Learning T-SQL: Dynamic Queries, Stored Procedures, and Cursors","author":"Sayed","date":"November 10, 2026","format":false,"excerpt":"T-SQL extends SQL with variables, conditional logic, procedures, and server-side control flow. This hands-on tutorial connects these features to a simple product database.","rel":"","context":"In &quot;Information Technology&quot;","block_context":{"text":"Information Technology","link":"http:\/\/bangla.sitestree.com\/?cat=20"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":75826,"url":"http:\/\/bangla.sitestree.com\/?p=75826","url_meta":{"origin":78948,"position":5},"title":"Some Topics to Know and Understand to prepare for a DBMS related Job Interview","author":"Sayed","date":"June 25, 2023","format":false,"excerpt":"Some Topics to Know and Understand to prepare for a DBMS related Job Interview ACID ACID: https:\/\/www.geeksforgeeks.org\/acid-properties-in-dbms Database Normalization 1NF: No repeating groups possible for a cell, PK identified, dependencies mapped 2NF: No Partial Dependence. Non key attributes must have to depend on the full key 3NF: No transitive dependency.\u2026","rel":"","context":"In &quot;\u09ac\u09cd\u09b2\u0997 \u0964 Blog&quot;","block_context":{"text":"\u09ac\u09cd\u09b2\u0997 \u0964 Blog","link":"http:\/\/bangla.sitestree.com\/?cat=182"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=78948"}],"version-history":[{"count":1,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78948\/revisions"}],"predecessor-version":[{"id":79010,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=\/wp\/v2\/posts\/78948\/revisions\/79010"}],"wp:attachment":[{"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=78948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=78948"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/bangla.sitestree.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=78948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}