<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gunjit Chhatwal</title>
    <description>The latest articles on DEV Community by Gunjit Chhatwal (@gunjitchhatwal).</description>
    <link>https://dev.to/gunjitchhatwal</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4235817%2F355d476c-a446-49d3-89a1-b3d6b8dc3d94.png</url>
      <title>DEV Community: Gunjit Chhatwal</title>
      <link>https://dev.to/gunjitchhatwal</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gunjitchhatwal"/>
    <language>en</language>
    <item>
      <title>Safely Running Long-Running Index Operations in SQL Server</title>
      <dc:creator>Gunjit Chhatwal</dc:creator>
      <pubDate>Wed, 11 Nov 2026 04:51:09 +0000</pubDate>
      <link>https://dev.to/gunjitchhatwal/safely-running-long-running-index-operations-in-sql-server-3b40</link>
      <guid>https://dev.to/gunjitchhatwal/safely-running-long-running-index-operations-in-sql-server-3b40</guid>
      <description>&lt;p&gt;Creating or rebuilding an index on a large, actively used table is not merely a schema change. It is a production operation that can consume significant CPU, I/O, transaction log, and storage resources, and it can introduce unexpected blocking even when the operation uses &lt;code&gt;ONLINE = ON&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This article explains a particularly subtle failure mode: an online index operation holds a schema-stability lock while an automatic statistics update waits for a schema-modification lock. SQL Server's lock queue can then place otherwise compatible application queries behind the waiting schema-modification request, creating a &lt;strong&gt;lock convoy&lt;/strong&gt; and widespread query timeouts.&lt;/p&gt;

&lt;p&gt;This is one possible blocking pattern, not the only cause of blocking during an online index operation.&lt;/p&gt;

&lt;p&gt;The broader lesson is simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Treat long-running index changes as controlled operational events. Estimate them, schedule them, monitor them, make them interruptible where possible, and define the recovery path before execution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why an Online Index Operation Can Still Block Queries
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;ONLINE = ON&lt;/code&gt; reduces blocking, but it does not mean "no locks." An online index operation still acquires locks during its lifecycle, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A long-lived schema-stability (&lt;code&gt;Sch-S&lt;/code&gt;) lock during much of the build.&lt;/li&gt;
&lt;li&gt;A short table-level shared (&lt;code&gt;S&lt;/code&gt;) lock during preparation and a short schema-modification (&lt;code&gt;Sch-M&lt;/code&gt;) lock during the final metadata switch.&lt;/li&gt;
&lt;li&gt;Intent locks on the source and target structures while the new index is built and maintained.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;Sch-S&lt;/code&gt; locks are compatible with other &lt;code&gt;Sch-S&lt;/code&gt; locks. Under normal conditions, queries can compile and execute while the index is being built.&lt;/p&gt;

&lt;p&gt;The problem begins when another operation requests an incompatible &lt;code&gt;Sch-M&lt;/code&gt; lock.&lt;/p&gt;

&lt;p&gt;The initial &lt;code&gt;S&lt;/code&gt; lock can also wait behind an open transaction that modified the table. In that case, the waiting index operation can delay new writers before the build has fully started.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Lock-Convoy Failure Mode
&lt;/h2&gt;

&lt;p&gt;Consider this sequence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Online index operation
  Holds: Sch-S on the target table
       |
       +-- blocks --&amp;gt; Statistics update
                       Wants: Sch-M
                            |
                            +-- queues ahead of --&amp;gt; New statements
                                                    Want: Sch-S
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The new queries need &lt;code&gt;Sch-S&lt;/code&gt;, which is compatible with the index operation's existing &lt;code&gt;Sch-S&lt;/code&gt;. However, a waiting &lt;code&gt;Sch-M&lt;/code&gt; request is already in the lock queue.&lt;/p&gt;

&lt;p&gt;A lock request is generally not granted ahead of an earlier incompatible waiter, even when it is compatible with the locks currently granted. This behavior prevents an exclusive waiter from starving indefinitely, but it can create a convoy.&lt;/p&gt;

&lt;p&gt;The result is a convoy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The index operation holds &lt;code&gt;Sch-S&lt;/code&gt; for an extended period.&lt;/li&gt;
&lt;li&gt;A statistics update requests &lt;code&gt;Sch-M&lt;/code&gt; and must wait.&lt;/li&gt;
&lt;li&gt;New statements touching the table request &lt;code&gt;Sch-S&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Those queries queue behind the pending &lt;code&gt;Sch-M&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Application requests accumulate and eventually reach their command timeout.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This behavior can make a database appear intermittently unavailable even though the index operation is running with &lt;code&gt;ONLINE = ON&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Automatic Statistics Updates Can Trigger the Convoy
&lt;/h2&gt;

&lt;p&gt;On an active table, inserts, updates, and deletes increase statistics modification counters. During a long index operation, one or more statistics objects may cross the automatic-update threshold.&lt;/p&gt;

&lt;p&gt;When a compilation detects stale statistics, SQL Server may attempt to update them. In some engine versions and execution paths, the update can request &lt;code&gt;Sch-M&lt;/code&gt;. If an online index operation is still holding &lt;code&gt;Sch-S&lt;/code&gt;, the statistics update waits and can become the head waiter in the blocking queue.&lt;/p&gt;

&lt;p&gt;This explains why problems may not begin immediately:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The index operation starts and queries initially continue normally.&lt;/li&gt;
&lt;li&gt;Data modifications accumulate.&lt;/li&gt;
&lt;li&gt;A statistics object becomes stale.&lt;/li&gt;
&lt;li&gt;A query compilation triggers the statistics update.&lt;/li&gt;
&lt;li&gt;The waiting &lt;code&gt;Sch-M&lt;/code&gt; request creates the convoy.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The delay between the start of the index operation and the first visible query failures can make the relationship difficult to identify. Confirm the mechanism rather than assuming it: look for an &lt;code&gt;LCK_M_SCH_M&lt;/code&gt; waiter associated with statistics activity, blocked by the index session, followed by &lt;code&gt;Sch-S&lt;/code&gt; waiters.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Recompilation Amplifies the Impact
&lt;/h2&gt;

&lt;p&gt;Queries acquire schema-stability locks on referenced objects while compiling and executing. A queued &lt;code&gt;Sch-M&lt;/code&gt; request can therefore delay all new statements touching the table, including statements that reuse cached plans.&lt;/p&gt;

&lt;p&gt;Frequent recompilation can still amplify the impact because it adds compilation work and more lock requests during an already congested period.&lt;/p&gt;

&lt;p&gt;Common causes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;OPTION (RECOMPILE)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Stored procedures declared &lt;code&gt;WITH RECOMPILE&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Plan-cache eviction&lt;/li&gt;
&lt;li&gt;Frequent ad hoc queries&lt;/li&gt;
&lt;li&gt;Schema changes that invalidate plans&lt;/li&gt;
&lt;li&gt;Statistics changes that trigger recompilation&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A cached plan avoids repeated optimization work, but it does not allow execution to bypass the schema-lock queue. A query using &lt;code&gt;OPTION (RECOMPILE)&lt;/code&gt; adds compilation on every execution, increasing pressure and the number of waiting requests during a convoy.&lt;/p&gt;

&lt;p&gt;Recompilation is not inherently wrong, but it must be considered when planning maintenance against hot tables.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why the Symptoms May Be Intermittent
&lt;/h2&gt;

&lt;p&gt;Temporary relief can occur when the waiting &lt;code&gt;Sch-M&lt;/code&gt; request is cancelled by a client timeout, a configured lock timeout, or an explicit operator action. Removing that request allows the queue to drain.&lt;/p&gt;

&lt;p&gt;When that happens:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The queued &lt;code&gt;Sch-S&lt;/code&gt; requests can be granted.&lt;/li&gt;
&lt;li&gt;A burst of queries completes.&lt;/li&gt;
&lt;li&gt;A later compilation detects stale statistics again.&lt;/li&gt;
&lt;li&gt;Another statistics update requests &lt;code&gt;Sch-M&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The convoy returns.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This produces a repeating pattern:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Blocking -&amp;gt; temporary relief -&amp;gt; blocking -&amp;gt; temporary relief
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Intermittent success does not prove that the index operation is healthy. It may indicate that the head waiter is repeatedly cancelled and requested again.&lt;/p&gt;

&lt;h2&gt;
  
  
  Risk Factors
&lt;/h2&gt;

&lt;p&gt;The likelihood and impact of this pattern increase when several of the following are true:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Risk factor&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Very large table or index&lt;/td&gt;
&lt;td&gt;Extends the period during which schema locks and resource pressure exist&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;High write rate&lt;/td&gt;
&lt;td&gt;Makes statistics stale more quickly and increases index-maintenance overhead&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frequent recompilation&lt;/td&gt;
&lt;td&gt;Adds compilation work and more requests during the vulnerable window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tight application timeouts&lt;/td&gt;
&lt;td&gt;Converts short blocking into visible request failures quickly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Limited transaction-log capacity&lt;/td&gt;
&lt;td&gt;Can slow or fail the operation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Insufficient free storage&lt;/td&gt;
&lt;td&gt;Online and resumable operations may temporarily require both old and new index structures&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;High CPU or I/O utilization&lt;/td&gt;
&lt;td&gt;Extends build duration and affects foreground workload&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fixed client or tool timeout&lt;/td&gt;
&lt;td&gt;May terminate the operation after hours of work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;No tested abort procedure&lt;/td&gt;
&lt;td&gt;Increases recovery time when application health degrades&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Plan the Change Before Running It
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Measure the Target
&lt;/h3&gt;

&lt;p&gt;Collect at least:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Table and index size&lt;/li&gt;
&lt;li&gt;Row count&lt;/li&gt;
&lt;li&gt;Current write rate&lt;/li&gt;
&lt;li&gt;Peak and off-peak workload&lt;/li&gt;
&lt;li&gt;Available database and temporary storage&lt;/li&gt;
&lt;li&gt;Existing index definition&lt;/li&gt;
&lt;li&gt;Edition and engine-version support for online and resumable operations&lt;/li&gt;
&lt;li&gt;Temporary-database capacity when using options that require it&lt;/li&gt;
&lt;li&gt;Planned &lt;code&gt;MAXDOP&lt;/code&gt; and its effect on duration and resource use&lt;/li&gt;
&lt;li&gt;Recovery model and transaction-log backup throughput&lt;/li&gt;
&lt;li&gt;Replication, availability, or disaster-recovery constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not estimate duration solely from a development or staging database. Index build time does not scale linearly across different hardware, data distributions, workload levels, and log configurations.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Establish Success and Abort Thresholds
&lt;/h3&gt;

&lt;p&gt;Define measurable thresholds before execution, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Maximum acceptable blocked-query count&lt;/li&gt;
&lt;li&gt;Maximum lock-wait duration&lt;/li&gt;
&lt;li&gt;Maximum application error or timeout rate&lt;/li&gt;
&lt;li&gt;CPU, I/O, and log-utilization limits&lt;/li&gt;
&lt;li&gt;Maximum operation duration&lt;/li&gt;
&lt;li&gt;Minimum free-storage reserve&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Choose the Execution Model
&lt;/h3&gt;

&lt;p&gt;Use the simplest model that provides enough safety:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Model&lt;/th&gt;
&lt;th&gt;Appropriate when&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Application schema-migration step&lt;/td&gt;
&lt;td&gt;The operation is small, predictable, and the tool's command timeout exceeds the expected duration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Extended-timeout maintenance command&lt;/td&gt;
&lt;td&gt;The operation is long but can safely remain attached to its initiating tool&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Separate maintenance job&lt;/td&gt;
&lt;td&gt;The operation must run independently or in a low-traffic window&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Resumable index operation&lt;/td&gt;
&lt;td&gt;The work must be divided into bounded windows or survive process restarts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scheduled maintenance framework&lt;/td&gt;
&lt;td&gt;Many databases require auditable, scheduled, retryable execution&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Long-running data and index operations should generally be separated from fast schema changes. A routine application rollout should not depend on a multi-hour physical data operation finishing in the same execution window.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Test with Production-Like Conditions
&lt;/h3&gt;

&lt;p&gt;A useful rehearsal should include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Representative data volume and distribution&lt;/li&gt;
&lt;li&gt;Concurrent reads and writes&lt;/li&gt;
&lt;li&gt;Queries that commonly recompile&lt;/li&gt;
&lt;li&gt;Monitoring of lock waits and blocking chains&lt;/li&gt;
&lt;li&gt;An intentional pause or abort&lt;/li&gt;
&lt;li&gt;Validation of retry or resume behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal is not only to prove that the index can be created. It is to prove that the system remains operable while it is being created.&lt;/p&gt;

&lt;h2&gt;
  
  
  Execution Safeguards
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Run During the Lowest Practical Traffic Window
&lt;/h3&gt;

&lt;p&gt;Lower traffic reduces:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Foreground workload contention&lt;/li&gt;
&lt;li&gt;Data changes that must be reflected in the new index&lt;/li&gt;
&lt;li&gt;Transaction-log generation&lt;/li&gt;
&lt;li&gt;The probability of statistics becoming stale during the operation&lt;/li&gt;
&lt;li&gt;The number of queries affected by an unexpected blocking chain&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An online operation should still be scheduled thoughtfully. "Online" is a concurrency feature, not a maintenance-window substitute.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Low-Priority Lock Waiting
&lt;/h3&gt;

&lt;p&gt;For supported operations, use &lt;code&gt;WAIT_AT_LOW_PRIORITY&lt;/code&gt; so the index operation's own lock requests do not indefinitely block normal workload. Test all syntax against the deployed engine version and edition; support differs between &lt;code&gt;CREATE INDEX&lt;/code&gt; and &lt;code&gt;ALTER INDEX ... REBUILD&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;IX_Example&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;LargeTable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;REBUILD&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="no"&gt;ONLINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="no"&gt;WAIT_AT_LOW_PRIORITY&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="no"&gt;MAX_DURATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="no"&gt;MINUTES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="no"&gt;ABORT_AFTER_WAIT&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;SELF&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ABORT_AFTER_WAIT = SELF&lt;/code&gt; favors workload availability by cancelling the index operation if it cannot obtain the required lock within the specified period. For a non-resumable operation, cancellation discards completed build work and rollback can take time. &lt;code&gt;ABORT_AFTER_WAIT = BLOCKERS&lt;/code&gt; can terminate user sessions and should be used only with explicit operational approval.&lt;/p&gt;

&lt;p&gt;Choose the duration based on the workload's latency objectives. Do not copy a large value without understanding how long the operation may remain pending.&lt;/p&gt;

&lt;p&gt;This option reduces blocking caused by the index operation's own lock requests. It does not prevent a separate statistics-update &lt;code&gt;Sch-M&lt;/code&gt; request from queueing while the build holds &lt;code&gt;Sch-S&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Consider Resumable Index Operations
&lt;/h3&gt;

&lt;p&gt;Where supported, resumable operations reduce the cost of interruption:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;IX_Example&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;LargeTable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;KeyColumn&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WITH&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="no"&gt;ONLINE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;RESUMABLE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;MAX_DURATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt; &lt;span class="no"&gt;MINUTES&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the operation reaches &lt;code&gt;MAX_DURATION&lt;/code&gt;, it pauses rather than discarding all progress. It can later be resumed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;IX_Example&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;LargeTable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;RESUME&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;MAX_DURATION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;120&lt;/span&gt; &lt;span class="no"&gt;MINUTES&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inspect resumable operations with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;index_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;state_desc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;percent_complete&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;page_count&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index_resumable_operations&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Resumability introduces operational responsibilities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A paused operation consumes storage.&lt;/li&gt;
&lt;li&gt;Writes may need to maintain both index structures.&lt;/li&gt;
&lt;li&gt;Operations requiring a table-level exclusive lock, such as &lt;code&gt;TRUNCATE TABLE&lt;/code&gt;, some &lt;code&gt;ALTER TABLE&lt;/code&gt; statements, and offline index rebuilds, cannot proceed while the index is paused.&lt;/li&gt;
&lt;li&gt;Automation must detect and resume, complete, or abort abandoned operations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Resumability does not prevent the lock-convoy pattern while the operation is actively running. Enable it only when someone owns monitoring and cleanup for paused work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Control Automatic Statistics Updates Deliberately
&lt;/h3&gt;

&lt;p&gt;For a verified lock-convoy risk, temporarily disabling automatic statistics updates can prevent a statistics update from requesting &lt;code&gt;Sch-M&lt;/code&gt; during the vulnerable period. Treat this as a last-resort mitigation after scheduling, low-priority waiting, resumability, and active monitoring have been considered.&lt;/p&gt;

&lt;p&gt;Prefer the narrowest effective scope. Inspect the current table-level settings before changing them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;EXEC&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sp_autostats&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'dbo.LargeTable'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If testing confirms that table-scoped suppression addresses the risk, record the existing state, disable updates for the target table, and restore the recorded state afterward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;EXEC&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sp_autostats&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'dbo.LargeTable'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'OFF'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Run the controlled index operation here.&lt;/span&gt;

&lt;span class="c1"&gt;-- Restore each statistic to its previously recorded setting.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a database-level change is required, first record &lt;code&gt;is_auto_update_stats_on&lt;/code&gt; from &lt;code&gt;sys.databases&lt;/code&gt; and restore that original value rather than hard-coding &lt;code&gt;ON&lt;/code&gt;. A database-level change affects every table; stale statistics can accumulate and query quality can degrade. Switching to asynchronous automatic statistics does not eliminate this risk because the statistics update can still request the incompatible lock.&lt;/p&gt;

&lt;p&gt;Use this mitigation only when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The lock-convoy mechanism has been validated for the workload.&lt;/li&gt;
&lt;li&gt;The maintenance window is bounded.&lt;/li&gt;
&lt;li&gt;The setting change is monitored.&lt;/li&gt;
&lt;li&gt;Re-enabling is part of the mandatory cleanup path.&lt;/li&gt;
&lt;li&gt;Critical statistics are updated manually afterward when appropriate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After the index operation has finished, update other affected statistics when appropriate. The newly created index already receives index statistics during creation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="no"&gt;STATISTICS&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;LargeTable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The appropriate sampling level depends on table size, workload, and maintenance budget. A full scan can itself be expensive.&lt;/p&gt;

&lt;p&gt;Ensure cleanup runs even after cancellation, timeout, or failure, and alert if any temporary statistics configuration remains in place.&lt;/p&gt;

&lt;h3&gt;
  
  
  Avoid Unnecessary Recompilation During the Window
&lt;/h3&gt;

&lt;p&gt;If application behavior permits, reduce avoidable compilations during maintenance:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reassess unconditional &lt;code&gt;OPTION (RECOMPILE)&lt;/code&gt; usage.&lt;/li&gt;
&lt;li&gt;Avoid unnecessary plan-cache clearing.&lt;/li&gt;
&lt;li&gt;Do not combine unrelated schema changes with the index operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not remove recompilation hints blindly. They may exist to solve parameter-sensitivity or plan-quality problems. Any query change requires independent performance validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Monitoring During the Operation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Find Active Requests and Blockers
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;command&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blocking_session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;percent_complete&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cpu_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total_elapsed_time&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_exec_requests&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
&lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_exec_sessions&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;database_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DB_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_user_process&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="nd"&gt;@@SPID&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total_elapsed_time&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;percent_complete&lt;/code&gt; is populated only for some commands and may remain zero for an index operation.&lt;/p&gt;

&lt;p&gt;Look specifically for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;LCK_M_SCH_M&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;LCK_M_SCH_S&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;A growing number of blocked sessions&lt;/li&gt;
&lt;li&gt;A head blocker associated with statistics or index maintenance&lt;/li&gt;
&lt;li&gt;Increasing application timeout rates&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Inspect Locks on the Target Object
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_associated_entity_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_tran_locks&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;tl&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_database_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DB_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'OBJECT'&lt;/span&gt;
    &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_associated_entity_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OBJECT_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'dbo.LargeTable'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_associated_entity_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tl&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_session_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This DMV does not expose queue order. Correlate it with &lt;code&gt;sys.dm_exec_requests&lt;/code&gt;, &lt;code&gt;sys.dm_os_waiting_tasks&lt;/code&gt;, and SQL text when deeper investigation is required. Capture evidence before terminating an operation, provided doing so does not delay recovery.&lt;/p&gt;

&lt;h3&gt;
  
  
  Monitor Resource Health
&lt;/h3&gt;

&lt;p&gt;At minimum, track:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;CPU&lt;/li&gt;
&lt;li&gt;Data-file and log-file I/O latency&lt;/li&gt;
&lt;li&gt;Transaction-log usage and growth&lt;/li&gt;
&lt;li&gt;Database and temporary-storage free space&lt;/li&gt;
&lt;li&gt;Query latency and timeout rate&lt;/li&gt;
&lt;li&gt;Deadlocks&lt;/li&gt;
&lt;li&gt;Blocking duration&lt;/li&gt;
&lt;li&gt;Index-operation progress&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Database health and application health must be observed together. A database command can be progressing normally while the application is already outside its service objectives.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abort and Recovery
&lt;/h2&gt;

&lt;p&gt;Stopping a non-resumable index operation can require a lengthy rollback. The database may remain under resource pressure while that rollback proceeds.&lt;/p&gt;

&lt;p&gt;If a verified statistics-related &lt;code&gt;LCK_M_SCH_M&lt;/code&gt; waiter is causing the convoy, cancelling that waiting session may drain the queue while preserving the index build. Confirm the session's identity and SQL text first; cancelling it also cancels the triggering query.&lt;/p&gt;

&lt;p&gt;Before execution, document:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to identify the index session.&lt;/li&gt;
&lt;li&gt;The exact command used to stop or pause it.&lt;/li&gt;
&lt;li&gt;Whether progress will be preserved.&lt;/li&gt;
&lt;li&gt;How rollback progress will be monitored.&lt;/li&gt;
&lt;li&gt;Which temporary database settings must be restored.&lt;/li&gt;
&lt;li&gt;How statistics and index state will be validated afterward.&lt;/li&gt;
&lt;li&gt;When the operation may be retried.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For a resumable operation, prefer &lt;code&gt;PAUSE&lt;/code&gt; when the goal is to preserve progress:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;IX_Example&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;LargeTable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;PAUSE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the partial operation must be discarded:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;IX_Example&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;LargeTable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="no"&gt;ABORT&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ABORT&lt;/code&gt; discards saved progress and may take time. Confirm the supported syntax and behavior for the deployed engine version before the maintenance window.&lt;/p&gt;

&lt;h2&gt;
  
  
  Post-Change Validation
&lt;/h2&gt;

&lt;p&gt;Do not declare success when the DDL statement returns. Verify:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The intended index exists and is enabled.&lt;/li&gt;
&lt;li&gt;The index definition matches the approved design.&lt;/li&gt;
&lt;li&gt;No resumable operation remains paused unexpectedly.&lt;/li&gt;
&lt;li&gt;Temporary database settings have been restored.&lt;/li&gt;
&lt;li&gt;Statistics are sufficiently current.&lt;/li&gt;
&lt;li&gt;Blocking and timeout rates have returned to baseline.&lt;/li&gt;
&lt;li&gt;Query plans use the new index where expected.&lt;/li&gt;
&lt;li&gt;Write latency has not regressed unexpectedly.&lt;/li&gt;
&lt;li&gt;Data and log files have adequate free space.&lt;/li&gt;
&lt;li&gt;The maintenance record reflects the final state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example checks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;type_desc&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_disabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;is_hypothetical&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;has_filter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;filter_definition&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;indexes&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OBJECT_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'dbo.LargeTable'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;statistic_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_updated&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;modification_counter&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stats&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="k"&gt;OUTER&lt;/span&gt; &lt;span class="no"&gt;APPLY&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dm_db_stats_properties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;stats_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OBJECT_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'dbo.LargeTable'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;sp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last_updated&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Recommended Operational Checklist
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Before
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Measure table, index, log, and storage requirements.&lt;/li&gt;
&lt;li&gt;[ ] Estimate duration using representative data and workload.&lt;/li&gt;
&lt;li&gt;[ ] Confirm online and resumable feature support.&lt;/li&gt;
&lt;li&gt;[ ] Identify queries that frequently compile or recompile.&lt;/li&gt;
&lt;li&gt;[ ] Select a low-traffic window.&lt;/li&gt;
&lt;li&gt;[ ] Define success, pause, and abort thresholds.&lt;/li&gt;
&lt;li&gt;[ ] Prepare monitoring queries and dashboards.&lt;/li&gt;
&lt;li&gt;[ ] Test pause, resume, abort, and cleanup behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  During
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Monitor application latency and error rate.&lt;/li&gt;
&lt;li&gt;[ ] Monitor &lt;code&gt;Sch-S&lt;/code&gt; and &lt;code&gt;Sch-M&lt;/code&gt; waits.&lt;/li&gt;
&lt;li&gt;[ ] Watch the blocking chain, not only the index session.&lt;/li&gt;
&lt;li&gt;[ ] Track CPU, I/O, storage, and transaction-log health.&lt;/li&gt;
&lt;li&gt;[ ] Pause or abort when a predefined threshold is crossed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  After
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;[ ] Confirm the final index definition and state.&lt;/li&gt;
&lt;li&gt;[ ] Restore temporary database settings.&lt;/li&gt;
&lt;li&gt;[ ] Enable statistics auto update as needed.&lt;/li&gt;
&lt;li&gt;[ ] Confirm no resumable operation remains paused.&lt;/li&gt;
&lt;li&gt;[ ] Validate critical query performance.&lt;/li&gt;
&lt;li&gt;[ ] Record actual duration, resource usage, and lessons learned.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Final Takeaways
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Online does not mean lock-free.&lt;/strong&gt; Online index operations still interact with schema locks and can affect production traffic.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A waiting &lt;code&gt;Sch-M&lt;/code&gt; request can create a lock convoy.&lt;/strong&gt; Later &lt;code&gt;Sch-S&lt;/code&gt; requests may queue behind it even though they are compatible with the lock currently being held.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic statistics can create a head waiter, and recompilation can add pressure.&lt;/strong&gt; All new statements need schema stability; frequent recompilation adds more work and requests during the convoy.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Large index changes need an operational design.&lt;/strong&gt; Maintenance windows, monitoring, abort criteria, and cleanup are part of the implementation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resumable operations reduce interruption cost but require lifecycle management.&lt;/strong&gt; Paused work must be visible, owned, and completed or aborted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Temporary configuration changes must be bounded and reversible.&lt;/strong&gt; If automatic statistics are disabled, restoring them is a mandatory recovery action.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Separate slow maintenance from routine application rollout when scale demands it.&lt;/strong&gt; Fast schema changes and multi-hour physical data operations have different operational requirements.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The safest index change is not the one with the most options in its &lt;code&gt;WITH&lt;/code&gt; clause. It is the one whose production behavior has been estimated, rehearsed, observed, and made recoverable.&lt;/p&gt;

</description>
      <category>database</category>
      <category>performance</category>
      <category>sql</category>
    </item>
    <item>
      <title>SQL Server Locks: A Performance and Deadlock Cheat Sheet</title>
      <dc:creator>Gunjit Chhatwal</dc:creator>
      <pubDate>Tue, 10 Nov 2026 04:08:34 +0000</pubDate>
      <link>https://dev.to/gunjitchhatwal/sql-server-locks-a-performance-and-deadlock-cheat-sheet-1am9</link>
      <guid>https://dev.to/gunjitchhatwal/sql-server-locks-a-performance-and-deadlock-cheat-sheet-1am9</guid>
      <description>&lt;p&gt;SQL Server uses locks to protect data while concurrent sessions read and modify it. Locks preserve transaction correctness, but they also create waiting, blocking, and deadlocks. When evaluating a slow query, ask: "Is it waiting for another transaction, and what resource does it need?"&lt;/p&gt;

&lt;p&gt;This cheat sheet explains the lock types you are most likely to encounter, how they affect query execution, and how to diagnose locking problems quickly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Locks are not latches.&lt;/strong&gt; Locks protect logical data and transaction consistency. Latches are lightweight internal synchronization mechanisms that protect in-memory structures such as data pages. A lock problem commonly appears as an &lt;code&gt;LCK_M_*&lt;/code&gt; wait; a latch problem uses waits such as &lt;code&gt;PAGELATCH_*&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The basic locking flow
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F23ncwbqodmyoj7pbbuhj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F23ncwbqodmyoj7pbbuhj.png" alt=" " width="800" height="191"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A lock request is granted when compatible with locks already held on the resource. Otherwise, the session waits. Lock duration depends on its mode, isolation level, statement, and transaction scope. A common issue is an application modifying data and then remaining idle before committing; the statement finished, but its locks remain.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lock modes you should recognize
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Lock&lt;/th&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;Typical purpose&lt;/th&gt;
&lt;th&gt;Performance implication&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;S&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Shared&lt;/td&gt;
&lt;td&gt;Reading data&lt;/td&gt;
&lt;td&gt;Compatible with other shared locks, but blocked by exclusive locks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;U&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Update&lt;/td&gt;
&lt;td&gt;Reading a row before updating it&lt;/td&gt;
&lt;td&gt;Reduces conversion deadlocks; only one update lock can target a resource&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Exclusive&lt;/td&gt;
&lt;td&gt;Inserting, updating, or deleting data&lt;/td&gt;
&lt;td&gt;Blocks both readers and writers under locking-based isolation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;IS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Intent Shared&lt;/td&gt;
&lt;td&gt;Signals shared locks at a lower level&lt;/td&gt;
&lt;td&gt;Enables efficient compatibility checks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;IX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Intent Exclusive&lt;/td&gt;
&lt;td&gt;Signals exclusive or update locks below&lt;/td&gt;
&lt;td&gt;Common while rows or pages are modified&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SIX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Shared with Intent Exclusive&lt;/td&gt;
&lt;td&gt;Reads the larger resource while modifying lower-level resources&lt;/td&gt;
&lt;td&gt;Restrictive; can create broad blocking&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Sch-S&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Schema Stability&lt;/td&gt;
&lt;td&gt;Compiling and executing queries&lt;/td&gt;
&lt;td&gt;Compatible with normal data access, but conflicts with schema modification&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Sch-M&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Schema Modification&lt;/td&gt;
&lt;td&gt;DDL and some metadata changes&lt;/td&gt;
&lt;td&gt;Highly restrictive; can block nearly all access to the object&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;BU&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Bulk Update&lt;/td&gt;
&lt;td&gt;Bulk-loading data under qualifying options&lt;/td&gt;
&lt;td&gt;Allows optimized bulk operations but restricts competing access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Range*&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Key-range locks&lt;/td&gt;
&lt;td&gt;Protects index ranges under &lt;code&gt;SERIALIZABLE&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Prevents phantom rows but can lock gaps and increase blocking&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Shared, update, and exclusive locks
&lt;/h3&gt;

&lt;p&gt;A shared lock allows concurrent readers. An exclusive lock prevents another session from reading or changing the resource under conventional &lt;code&gt;READ COMMITTED&lt;/code&gt; locking. Update locks are an intermediate mode used when reading data that may be changed. They help avoid two sessions acquiring shared locks and both attempting to convert them to exclusive locks.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgfif88809u5pj4ghmf61.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgfif88809u5pj4ghmf61.png" alt=" " width="800" height="537"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Intent locks
&lt;/h3&gt;

&lt;p&gt;Intent locks describe activity lower in the lock hierarchy. An &lt;code&gt;IX&lt;/code&gt; table lock usually means exclusive locks exist, or will exist, on pages, keys, or rows. It does not mean the whole table is exclusively locked. Intent modes quickly reveal whether a table-level request conflicts with lower-level activity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Schema locks
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Sch-S&lt;/code&gt; locks occur during query compilation and execution, even with &lt;code&gt;NOLOCK&lt;/code&gt;. &lt;code&gt;Sch-M&lt;/code&gt; locks occur during schema changes and conflict with &lt;code&gt;Sch-S&lt;/code&gt;. A long query can delay DDL, while schema modification can block ordinary queries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Key-range locks
&lt;/h3&gt;

&lt;p&gt;Under &lt;code&gt;SERIALIZABLE&lt;/code&gt;, SQL Server may lock index keys and gaps between them. This prevents inserts that would newly satisfy an earlier predicate. Key-range locks provide strong consistency but can greatly expand the protected area, especially without supporting indexes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lock granularity: what is being locked?
&lt;/h2&gt;

&lt;p&gt;SQL Server can lock resources at several levels:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcs40421hqpzcmopnyxz4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fcs40421hqpzcmopnyxz4.png" alt=" " width="720" height="844"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;KEY&lt;/strong&gt; locks protect index entries. Even queries described as "row locking" often show &lt;code&gt;KEY&lt;/code&gt; resources.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;RID&lt;/strong&gt; locks protect rows in a heap, which is a table without a clustered index.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PAGE&lt;/strong&gt; locks protect an 8 KB data or index page.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OBJECT&lt;/strong&gt; locks protect a table or similar object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HoBT&lt;/strong&gt; locks protect a heap or B-tree, including an individual partition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;METADATA&lt;/strong&gt;, &lt;strong&gt;DATABASE&lt;/strong&gt;, and &lt;strong&gt;ALLOCATION_UNIT&lt;/strong&gt; locks protect broader internal resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fine-grained locks improve concurrency but consume memory. Coarser locks use fewer resources but block more operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lock escalation
&lt;/h2&gt;

&lt;p&gt;Lock escalation replaces many fine-grained locks with a coarser lock. SQL Server attempts escalation when a single statement acquires at least roughly 5,000 locks on one table or index reference, and it can also escalate under lock-memory pressure. Escalation is normally to a table lock; a partitioned table can escalate to a partition when &lt;code&gt;LOCK_ESCALATION = AUTO&lt;/code&gt;. SQL Server does not escalate row or key locks to page locks.&lt;/p&gt;

&lt;p&gt;If another session holds an incompatible lock that prevents escalation, the statement continues with fine-grained locks and SQL Server retries later. Escalation can turn a large modification into a whole-table blocker. Reduce its likelihood with shorter transactions, batched changes, and indexes that prevent unnecessary scans. Disabling escalation can exchange blocking for excessive lock memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Compatibility at a glance
&lt;/h2&gt;

&lt;p&gt;The full compatibility matrix is extensive, but this reduced view covers common investigations:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Held \ Requested&lt;/th&gt;
&lt;th&gt;&lt;code&gt;S&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;U&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;X&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;IS&lt;/code&gt;&lt;/th&gt;
&lt;th&gt;&lt;code&gt;IX&lt;/code&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;S&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;U&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;X&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;IS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;IX&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Compatibility alone does not explain every wait. Locks may cover different resources, and a request can wait behind another waiter because SQL Server generally preserves queue fairness.&lt;/p&gt;

&lt;h2&gt;
  
  
  How isolation levels change locking
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Isolation level&lt;/th&gt;
&lt;th&gt;Important locking behavior&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;READ UNCOMMITTED&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Allows dirty reads and usually avoids shared data locks; rows can be missing, duplicated, or inconsistent&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;READ COMMITTED&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;SQL Server default; shared row/page locks are generally released as data is read, allowing later rereads to change&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;REPEATABLE READ&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Holds shared locks until transaction end, preventing changed or deleted rereads&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SERIALIZABLE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Adds key-range protection and prevents phantoms; highest blocking risk&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SNAPSHOT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Uses row versions for reads; update conflicts are possible; writes still use locks&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;READ COMMITTED SNAPSHOT&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Changes &lt;code&gt;READ COMMITTED&lt;/code&gt; reads to use row versions; writers still lock&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;SNAPSHOT&lt;/code&gt; requires &lt;code&gt;ALLOW_SNAPSHOT_ISOLATION&lt;/code&gt; to be enabled. Azure SQL Database commonly enables &lt;code&gt;READ_COMMITTED_SNAPSHOT&lt;/code&gt; by default, while boxed SQL Server does not. Row versioning reduces reader-writer blocking but does not eliminate writer-writer blocking, schema conflicts, or poor transaction design. It also adds version-store usage, and competing snapshot updates can fail with error 3960.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;NOLOCK&lt;/code&gt; is not a performance fix.&lt;/strong&gt; It uses &lt;code&gt;READ UNCOMMITTED&lt;/code&gt;, permitting dirty, missing, or duplicated rows, and it still acquires schema-stability locks. Use it only when those correctness risks are explicitly acceptable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Blocking versus deadlocks
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Blocking&lt;/strong&gt; is expected synchronization: one session waits until another releases an incompatible lock. &lt;strong&gt;A deadlock&lt;/strong&gt; is a cycle in which sessions wait for one another and none can proceed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgdgb58weoosod1udtgdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fgdgb58weoosod1udtgdl.png" alt=" " width="799" height="117"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SQL Server detects the cycle and chooses a victim, which receives error 1205 and is rolled back. Applications should use bounded retries for idempotent operations, but retries do not fix recurring deadlocks.&lt;/p&gt;

&lt;p&gt;Typical fixes include consistent object access order, shorter transactions, indexes that reduce scanned rows, no user interaction inside transactions, and smaller batches. The deadlock graph identifies participating processes, resources, lock modes, and statements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reproducible locking labs
&lt;/h2&gt;

&lt;p&gt;Run these examples only on a development SQL Server where you can create a disposable database. They were validated on SQL Server 2022 Developer Edition. Open two query windows and label them &lt;strong&gt;Session A&lt;/strong&gt; and &lt;strong&gt;Session B&lt;/strong&gt;. Run this setup once; &lt;code&gt;DROP TABLE IF EXISTS&lt;/code&gt; requires SQL Server 2016 or later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;USE&lt;/span&gt; &lt;span class="n"&gt;master&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;GO&lt;/span&gt;

&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="no"&gt;DB_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'LockLab'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
    &lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;DATABASE&lt;/span&gt; &lt;span class="nc"&gt;LockLab&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;GO&lt;/span&gt;

&lt;span class="no"&gt;USE&lt;/span&gt; &lt;span class="nc"&gt;LockLab&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;GO&lt;/span&gt;

&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
        &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="nc"&gt;PK_LockDemo&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;AccountName&lt;/span&gt; &lt;span class="kt"&gt;varchar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nc"&gt;ReferenceCode&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;INDEX&lt;/span&gt; &lt;span class="nc"&gt;IX_LockDemo_ReferenceCode&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ReferenceCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ReferenceCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Alpha'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Beta'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Gamma'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_read_committed_snapshot_on&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;snapshot_isolation_state_desc&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;databases&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DB_NAME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lab 1: an exclusive lock blocks a reader
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;Session A&lt;/strong&gt;, update a row but leave the transaction open:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;BEGIN&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Leave this transaction open while running Session B.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;Session B&lt;/strong&gt;, run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;READCOMMITTEDLOCK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;READCOMMITTEDLOCK&lt;/code&gt; makes the example deterministic even when &lt;code&gt;READ_COMMITTED_SNAPSHOT&lt;/code&gt; is enabled. Session B waits with &lt;code&gt;LCK_M_S&lt;/code&gt; because Session A holds an exclusive key lock. In a third window, run the diagnostic queries in the next section to see the waiter and blocker. Release the lock in Session A:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ROLLBACK&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Session B then completes and returns the original balance of 100 because Session A rolled back. To demonstrate row versioning, remove &lt;code&gt;READCOMMITTEDLOCK&lt;/code&gt; on a database with &lt;code&gt;READ_COMMITTED_SNAPSHOT&lt;/code&gt; enabled; the reader can return the previously committed version without waiting.&lt;/p&gt;

&lt;h3&gt;
  
  
  Lab 2: create a two-row deadlock
&lt;/h3&gt;

&lt;p&gt;Run the following in &lt;strong&gt;Session A&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;XACT_ABORT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;WAITFOR&lt;/span&gt; &lt;span class="no"&gt;DELAY&lt;/span&gt; &lt;span class="s1"&gt;'00:00:15'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;COMMIT&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start this in &lt;strong&gt;Session B&lt;/strong&gt; within 15 seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;XACT_ABORT&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="no"&gt;WAITFOR&lt;/span&gt; &lt;span class="no"&gt;DELAY&lt;/span&gt; &lt;span class="s1"&gt;'00:00:15'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;COMMIT&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each session owns an exclusive lock needed by the other. SQL Server detects the cycle and terminates one session with error 1205; either session can be the victim. &lt;code&gt;DEADLOCK_PRIORITY&lt;/code&gt; is considered first, followed by estimated rollback cost. The survivor commits. The primary fix is to access rows in the same order—for example, always update &lt;code&gt;Id = 1&lt;/code&gt; before &lt;code&gt;Id = 2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In a third window, inspect the newest graph from the built-in &lt;code&gt;system_health&lt;/code&gt; session. This requires &lt;code&gt;VIEW SERVER STATE&lt;/code&gt; and applies to SQL Server; Azure SQL Database exposes Extended Events through database-scoped targets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;QUOTED_IDENTIFIER&lt;/span&gt; &lt;span class="k"&gt;ON&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="k"&gt;WITH&lt;/span&gt; &lt;span class="nc"&gt;DeadlockEvents&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="nb"&gt;CAST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target_data&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="kt"&gt;xml&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;target_data&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_xe_session_targets&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;
    &lt;span class="k"&gt;INNER&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_xe_sessions&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
        &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;address&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;event_session_address&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'system_health'&lt;/span&gt;
      &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;target_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'ring_buffer'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;event_xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'(@timestamp)[1]'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'datetime2'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;event_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;event_xml&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'(data/value/deadlock)[1]'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;deadlock_graph&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nc"&gt;DeadlockEvents&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="no"&gt;APPLY&lt;/span&gt; &lt;span class="n"&gt;target_data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nodes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'//RingBufferTarget/event[@name="xml_deadlock_report"]'&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nf"&gt;events&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;event_xml&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;event_time&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Reset the data and connection setting in both windows before continuing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="no"&gt;XACT_STATE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;ROLLBACK&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="no"&gt;XACT_ABORT&lt;/span&gt; &lt;span class="no"&gt;OFF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Lab 3: a serializable range blocks an insert
&lt;/h3&gt;

&lt;p&gt;In &lt;strong&gt;Session A&lt;/strong&gt;, protect a range that currently contains one row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt; &lt;span class="no"&gt;ISOLATION&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;SERIALIZABLE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;BEGIN&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;ReferenceCode&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Leave the transaction open while running Session B.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;Session B&lt;/strong&gt;, attempt to insert into the protected range:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;AccountName&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Balance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ReferenceCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'Delta'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;400&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The insert waits on a range-insert lock. On SQL Server 2022 this appears as &lt;code&gt;LCK_M_RIn_NL&lt;/code&gt;; older versions and tools may display related range-insert names differently. Session A holds a &lt;code&gt;RangeS-S&lt;/code&gt; lock on the supporting index, protecting existing keys and gaps where qualifying rows could be inserted. Finish Session A:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;COMMIT&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt; &lt;span class="no"&gt;ISOLATION&lt;/span&gt; &lt;span class="no"&gt;LEVEL&lt;/span&gt; &lt;span class="no"&gt;READ&lt;/span&gt; &lt;span class="no"&gt;COMMITTED&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Session B can now insert the row. Remove it before repeating the lab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;DELETE&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;Id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These labs illustrate three different diagnoses: incompatible locks causing ordinary blocking, a circular wait causing a deadlock, and an isolation-level guarantee deliberately expanding the locked resource range.&lt;/p&gt;

&lt;p&gt;After completing the labs, clean up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="no"&gt;USE&lt;/span&gt; &lt;span class="nc"&gt;LockLab&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="nd"&gt;@@TRANCOUNT&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;ROLLBACK&lt;/span&gt; &lt;span class="k"&gt;TRANSACTION&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;DROP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="k"&gt;IF&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="n"&gt;dbo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LockDemo&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- Optional, from a window connected to master:&lt;/span&gt;
&lt;span class="c1"&gt;-- USE master;&lt;/span&gt;
&lt;span class="c1"&gt;-- DROP DATABASE LockLab;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Quick diagnostic queries
&lt;/h2&gt;

&lt;p&gt;The following server-scoped DMVs generally require &lt;code&gt;VIEW SERVER STATE&lt;/code&gt; (&lt;code&gt;VIEW SERVER PERFORMANCE STATE&lt;/code&gt; for some DMVs on SQL Server 2022+) or the corresponding Azure SQL database permission. &lt;code&gt;wait_time&lt;/code&gt; is reported in milliseconds.&lt;/p&gt;

&lt;p&gt;Find waiting requests and their blockers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blocking_session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_resource&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;DB_NAME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;database_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;database_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;current_sql&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_exec_requests&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="no"&gt;APPLY&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dm_exec_sql_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sql_handle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;blocking_session_id&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
   &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wait_type&lt;/span&gt; &lt;span class="k"&gt;LIKE&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'LCK_M_%'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inspect current locks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_type&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="no"&gt;OBJECT_NAME&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;object_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_database_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;object_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_mode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_status&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_tran_locks&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;partitions&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_type&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'KEY'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'PAGE'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'RID'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;N&lt;/span&gt;&lt;span class="s1"&gt;'HOBT'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
   &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hobt_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_associated_entity_id&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_database_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;DB_ID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request_session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;resource_type&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The head blocker is often sleeping and therefore absent from &lt;code&gt;sys.dm_exec_requests&lt;/code&gt;. Find its open transaction and most recent SQL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;login_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;host_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;program_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;open_transaction_count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transaction_begin_time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;txt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kt"&gt;text&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;most_recent_sql&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_exec_sessions&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_tran_session_transactions&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_tran_active_transactions&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transaction_id&lt;/span&gt;
&lt;span class="k"&gt;LEFT&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_exec_connections&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt;
&lt;span class="k"&gt;OUTER&lt;/span&gt; &lt;span class="no"&gt;APPLY&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;dm_exec_sql_text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;most_recent_sql_handle&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;txt&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;session_id&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;blocking_session_id&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;sys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dm_exec_requests&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;blocking_session_id&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common waits provide a fast clue:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Wait&lt;/th&gt;
&lt;th&gt;Usually means&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LCK_M_S&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Waiting for a shared lock, often a reader behind a writer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;LCK_M_U&lt;/code&gt; / &lt;code&gt;LCK_M_X&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Waiting to update or exclusively modify data&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LCK_M_SCH_M&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Waiting for a schema modification lock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;LCK_M_RS_S&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Waiting for a serializable range shared lock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;LCK_M_RIn_NL&lt;/code&gt; and related &lt;code&gt;LCK_M_RI*&lt;/code&gt; waits&lt;/td&gt;
&lt;td&gt;Waiting to insert into a protected key range; the exact name varies by engine version&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Use the results together. A granted lock is not automatically a problem; identify the waiter, head blocker, transaction age, and SQL. &lt;code&gt;sys.dm_os_waiting_tasks&lt;/code&gt; provides another resource-level view. For historical deadlocks, use &lt;code&gt;system_health&lt;/code&gt; or a dedicated Extended Events session capturing &lt;code&gt;xml_deadlock_report&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Performance review checklist
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Confirm waiting:&lt;/strong&gt; Look for &lt;code&gt;LCK_M_*&lt;/code&gt; waits rather than assuming high duration means blocking.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Find the head blocker:&lt;/strong&gt; Follow &lt;code&gt;blocking_session_id&lt;/code&gt; until reaching the session that is not itself blocked.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check transaction scope:&lt;/strong&gt; Determine whether the blocker has an old open transaction or is idle while holding locks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Identify the resource and mode:&lt;/strong&gt; Use &lt;code&gt;wait_resource&lt;/code&gt;, &lt;code&gt;sys.dm_tran_locks&lt;/code&gt;, and the execution plan.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Review access paths:&lt;/strong&gt; Scans lock more rows and hold locks longer than selective seeks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Check isolation requirements:&lt;/strong&gt; Use stronger isolation only when the business invariant requires it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Capture the deadlock graph:&lt;/strong&gt; Do not diagnose recurring deadlocks from the victim statement alone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix the cause, not the symptom:&lt;/strong&gt; Prefer indexing, consistent access order, batching, and shorter transactions over &lt;code&gt;NOLOCK&lt;/code&gt; or killing sessions.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Locks are necessary and waiting can be normal. Prolonged blocking usually reflects transaction scope, access paths, or concurrency design. Diagnose the wait chain, then reduce the locked resource set or the time locks are held.&lt;/p&gt;

</description>
      <category>sqlserver</category>
      <category>database</category>
      <category>performance</category>
    </item>
    <item>
      <title>Microsoft.Testing.Platform: How .NET Testing Evolved</title>
      <dc:creator>Gunjit Chhatwal</dc:creator>
      <pubDate>Mon, 09 Nov 2026 20:35:04 +0000</pubDate>
      <link>https://dev.to/gunjitchhatwal/microsofttestingplatform-how-net-testing-evolved-5652</link>
      <guid>https://dev.to/gunjitchhatwal/microsofttestingplatform-how-net-testing-evolved-5652</guid>
      <description>&lt;p&gt;For most .NET developers, running tests has long appeared simple: add MSTest, NUnit, or xUnit.net, write a few test methods, and run &lt;code&gt;dotnet test&lt;/code&gt;. Under that command, however, sits a test platform responsible for discovering tests, loading adapters, starting processes, collecting results, and integrating with editors and CI systems.&lt;/p&gt;

&lt;p&gt;That distinction matters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;test framework&lt;/strong&gt; defines how tests are written. Examples include MSTest, NUnit, xUnit.net, and TUnit.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;test platform&lt;/strong&gt; discovers and executes those tests and connects them to command-line tools, IDEs, and build pipelines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For many years, VSTest was the standard platform behind .NET testing. Microsoft.Testing.Platform (MTP) is its modern, lightweight alternative. It does not replace test frameworks; instead, it changes how their tests are hosted and executed.&lt;/p&gt;

&lt;h2&gt;
  
  
  How .NET testing reached this point
&lt;/h2&gt;

&lt;p&gt;The earliest .NET test runners were closely associated with their frameworks or development environments. Tests often ran through a framework-specific console runner or directly inside Visual Studio. This worked well when applications primarily targeted the .NET Framework and ran on Windows.&lt;/p&gt;

&lt;p&gt;VSTest brought valuable consistency. It gave Visual Studio, command-line tools, CI systems, and third-party frameworks a common execution platform. Test adapters allowed MSTest, NUnit, xUnit.net, and other frameworks to participate in the same workflow.&lt;/p&gt;

&lt;p&gt;The arrival of .NET Core in 2016 changed the environment. .NET became cross-platform, side-by-side versioned, container-friendly, and increasingly optimized for deployment models such as trimming and Native AOT. Test projects could now target many runtimes and architectures, while runners had to handle more combinations of SDKs, hosts, adapters, extensions, and process boundaries.&lt;/p&gt;

&lt;p&gt;VSTest continued to evolve, but its architecture came from a period when an external runner loading test adapters was the natural model. Supporting modern .NET form factors within that model added complexity. The result could be subtle differences between local, IDE, and CI execution, along with difficult assembly-loading or extension-discovery problems.&lt;/p&gt;

&lt;p&gt;Microsoft introduced MTP through the MSTest Runner work and expanded it into a platform supported by the major .NET test frameworks. In .NET 10, &lt;code&gt;dotnet test&lt;/code&gt; gained native MTP-aware orchestration, making the new platform feel increasingly like a first-class part of the SDK rather than a separate experiment.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8jrjxkbexmjjxv50oda4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F8jrjxkbexmjjxv50oda4.png" alt="evolution of testing in dotnet" width="602" height="183"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The architectural shift: the test project becomes an application
&lt;/h2&gt;

&lt;p&gt;The most important MTP idea is surprisingly simple: a test project is built as an executable test application.&lt;/p&gt;

&lt;p&gt;With the traditional VSTest model, an external runner discovers an adapter, loads the test assembly, and coordinates execution. With MTP, the platform and framework runner are embedded into the test project. The resulting application can participate in &lt;code&gt;dotnet test&lt;/code&gt;, but it can also be started with &lt;code&gt;dotnet run&lt;/code&gt; or executed directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdz4y4t0bxbyj63irbezb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdz4y4t0bxbyj63irbezb.png" alt="test framework architecture" width="800" height="643"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This design reduces the number of hidden decisions made at run time. Frameworks and extensions are normally registered at build time, so the application knows what it contains before execution begins. The same test artifact can run locally or in CI without depending on a separately installed console runner.&lt;/p&gt;

&lt;h2&gt;
  
  
  What improves with Microsoft.Testing.Platform?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. More deterministic execution
&lt;/h3&gt;

&lt;p&gt;MTP emphasizes compile-time registration instead of discovering arbitrary plugins through reflection at run time. This makes configuration errors visible earlier and reduces environment-dependent behavior. The test application carries its intended runner and extensions with it, improving the chance that the same build behaves consistently on a developer machine and a build agent.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. A smaller and more transparent runtime
&lt;/h3&gt;

&lt;p&gt;The MTP core is a single assembly with no dependencies beyond supported .NET runtimes. It avoids creating isolation mechanisms such as &lt;code&gt;AppDomain&lt;/code&gt; or &lt;code&gt;AssemblyLoadContext&lt;/code&gt; simply to coordinate a run, and it does not rely on custom assembly resolution for its core operation. That reduces interference between the platform and the framework executing the tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Executable-first workflows
&lt;/h3&gt;

&lt;p&gt;Because an MTP test project behaves like a normal .NET application, it works naturally with familiar application workflows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet &lt;span class="nb"&gt;test
&lt;/span&gt;dotnet run &lt;span class="nt"&gt;--project&lt;/span&gt; tests/MyApp.Tests
./tests/MyApp.Tests/bin/Release/net8.0/MyApp.Tests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful for containers, specialized devices, browser-like hosts, startup-project debugging, and environments where installing or locating an external runner is undesirable.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Better alignment with modern .NET
&lt;/h3&gt;

&lt;p&gt;MTP was designed for current and future .NET form factors, including trimming and Native AOT scenarios. Its hostable architecture also allows the test runtime to run inside different application types rather than assuming every test must be controlled by a conventional console host.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Modular capabilities instead of a large default surface
&lt;/h3&gt;

&lt;p&gt;The platform focuses on orchestration and adds capabilities through extensions. Available extensions cover TRX, HTML, JUnit, CTRF and CI-native reporting, code coverage, crash and hang dumps, retries, Hot Reload, OpenTelemetry, Microsoft Fakes, and more.&lt;/p&gt;

&lt;p&gt;When standard MSBuild integration is enabled, installing an extension package is generally enough for it to be detected and registered. Teams can therefore include only the capabilities they need instead of paying for every feature in every test process.&lt;/p&gt;

&lt;h3&gt;
  
  
  6. Framework choice remains open
&lt;/h3&gt;

&lt;p&gt;MTP is not limited to MSTest. MSTest, NUnit, xUnit.net, and TUnit all support it, while TUnit is built directly on top of MTP. This lets teams modernize test execution without necessarily rewriting their test suites or abandoning their preferred assertion and test-authoring model.&lt;/p&gt;

&lt;h2&gt;
  
  
  A practical before-and-after example
&lt;/h2&gt;

&lt;p&gt;Consider a small pricing service. The production code applies a percentage discount:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PriceCalculator&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="nf"&gt;ApplyDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;percentage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percentage&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="n"&gt;or&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ArgumentOutOfRangeException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;nameof&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;percentage&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;price&lt;/span&gt; &lt;span class="p"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt; &lt;span class="n"&gt;percentage&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Its MSTest test is intentionally ordinary:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.VisualStudio.TestTools.UnitTesting&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;TestClass&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PriceCalculatorTests&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;TestMethod&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ApplyDiscount_ReducesPrice&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;decimal&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PriceCalculator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ApplyDiscount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;200m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;15m&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;Assert&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AreEqual&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;170m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The test itself does not need to change when moving from VSTest to MTP. The difference is in how the test project is assembled and executed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Before: a conventional VSTest project
&lt;/h3&gt;

&lt;p&gt;A representative VSTest-based project explicitly combines the .NET SDK, the test platform package, the framework, and its adapter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Project&lt;/span&gt; &lt;span class="na"&gt;Sdk=&lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Sdk"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TargetFramework&amp;gt;&lt;/span&gt;net10.0&lt;span class="nt"&gt;&amp;lt;/TargetFramework&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;IsTestProject&amp;gt;&lt;/span&gt;true&lt;span class="nt"&gt;&amp;lt;/IsTestProject&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;IsPackable&amp;gt;&lt;/span&gt;false&lt;span class="nt"&gt;&amp;lt;/IsPackable&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;ItemGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include=&lt;/span&gt;&lt;span class="s"&gt;"Microsoft.NET.Test.Sdk"&lt;/span&gt; &lt;span class="na"&gt;Version=&lt;/span&gt;&lt;span class="s"&gt;"18.11.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include=&lt;/span&gt;&lt;span class="s"&gt;"MSTest.TestAdapter"&lt;/span&gt; &lt;span class="na"&gt;Version=&lt;/span&gt;&lt;span class="s"&gt;"4.1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include=&lt;/span&gt;&lt;span class="s"&gt;"MSTest.TestFramework"&lt;/span&gt; &lt;span class="na"&gt;Version=&lt;/span&gt;&lt;span class="s"&gt;"4.1.0"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/ItemGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The typical CI command asks VSTest to load a logger and a data collector:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;--configuration&lt;/span&gt; Release &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--logger&lt;/span&gt; trx &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--collect&lt;/span&gt; &lt;span class="s2"&gt;"Code Coverage;Format=cobertura"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a mature and widely supported workflow. However, the test artifact is primarily a library intended to be understood and launched by an external test platform. The runner must locate the adapter, interpret VSTest options, start a test host, and coordinate collection and reporting.&lt;/p&gt;

&lt;h3&gt;
  
  
  After: the same suite as an MTP application
&lt;/h3&gt;

&lt;p&gt;MSTest.Sdk bundles the recommended MSTest setup and uses MTP by default:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Project&lt;/span&gt; &lt;span class="na"&gt;Sdk=&lt;/span&gt;&lt;span class="s"&gt;"MSTest.Sdk/4.1.0"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;PropertyGroup&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;TargetFramework&amp;gt;&lt;/span&gt;net10.0&lt;span class="nt"&gt;&amp;lt;/TargetFramework&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/PropertyGroup&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Project&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For native MTP orchestration in .NET 10 or later, the repository selects the runner in &lt;code&gt;global.json&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"test"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"runner"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Microsoft.Testing.Platform"&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The equivalent CI command becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;--configuration&lt;/span&gt; Release &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--report-trx&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--coverage&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--coverage-output-format&lt;/span&gt; cobertura
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The options now describe capabilities owned by the test application: TRX reporting and code coverage. With the default MSTest.Sdk profile, the required Microsoft extensions are already included. If a team uses a minimal profile, it can add only the extension packages it wants.&lt;/p&gt;

&lt;p&gt;More importantly, the compiled test application is not limited to &lt;code&gt;dotnet test&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# Familiar solution or project orchestration&lt;/span&gt;
dotnet &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;--project&lt;/span&gt; tests/Pricing.Tests/Pricing.Tests.csproj

&lt;span class="c"&gt;# Launch it like another .NET application&lt;/span&gt;
dotnet run &lt;span class="nt"&gt;--project&lt;/span&gt; tests/Pricing.Tests/Pricing.Tests.csproj

&lt;span class="c"&gt;# Build once, then run the produced test executable&lt;/span&gt;
dotnet build tests/Pricing.Tests &lt;span class="nt"&gt;-c&lt;/span&gt; Release
./tests/Pricing.Tests/bin/Release/net10.0/Pricing.Tests
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows, the final command uses &lt;code&gt;Pricing.Tests.exe&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6tukr492v268ha62w6o7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F6tukr492v268ha62w6o7.png" alt="executable test artifacts" width="800" height="702"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  What improved in this example?
&lt;/h3&gt;

&lt;p&gt;First, the project configuration has fewer independently managed test packages. The SDK supplies a coherent runner, framework, and default extension profile. Second, the test output is self-describing: the runner and registered extensions travel with the build instead of being discovered from a machine-specific adapter setup. Third, local debugging and CI execution are closer because both launch the same test application. Finally, stricter defaults expose mistakes sooner—for example, MTP returns a non-success exit code when a test application executes zero tests, while VSTest normally treats that situation as successful.&lt;/p&gt;

&lt;p&gt;The improvement is primarily architectural and operational. A single &lt;code&gt;Assert.AreEqual&lt;/code&gt; is not inherently faster because it runs on MTP. The gains appear around startup composition, predictable extension loading, artifact portability, parallel module orchestration, diagnostics, and reduced differences between execution environments. Actual performance still depends on the number of projects, framework behavior, test setup, I/O, and enabled extensions, so teams should benchmark their own suites rather than rely on a universal speed claim.&lt;/p&gt;

&lt;h2&gt;
  
  
  Retrying individual tests in a test pipeline
&lt;/h2&gt;

&lt;p&gt;Consider a long-running test pipeline that exercises a deployed service, including authentication, data import, background processing, and persistence. Such pipelines are expensive to repeat because each test can involve network calls, external resources, and significant setup.&lt;/p&gt;

&lt;p&gt;Before the migration, a task-level retry was used when a test failed. If one test failed near the end of the run, the pipeline repeated the entire task—including tests that had already passed:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03ercwmcn4f48vb0bv4t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F03ercwmcn4f48vb0bv4t.png" alt="individual test retry" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;After moving the test project to MTP, the suite can add the retry extension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;PackageReference&lt;/span&gt; &lt;span class="na"&gt;Include=&lt;/span&gt;&lt;span class="s"&gt;"Microsoft.Testing.Extensions.Retry"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pipeline could then request up to two retries for failed tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--retry-failed-tests&lt;/span&gt; 2 &lt;span class="se"&gt;\&lt;/span&gt;
  &lt;span class="nt"&gt;--retry-failed-tests-max-percentage&lt;/span&gt; 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The percentage limit prevents a broadly broken suite from repeatedly retrying a large number of failures. Smaller suites can choose a different policy because one failed test might already exceed a 10% threshold.&lt;/p&gt;

&lt;p&gt;This changes the retry boundary from the pipeline task to the individual test. A transient failure no longer requires every successful test to run again, reducing wasted agent time and repeated calls to deployed resources. A pipeline can also merge the per-attempt TRX files into a final report, retaining the final outcome while identifying tests that failed initially and passed only on retry.&lt;/p&gt;

&lt;p&gt;Per-test retries should not be used to normalize flaky tests. A test that passes on retry is still a reliability signal that should be visible and investigated. The value of the MTP extension is that retry behavior becomes an explicit, configurable part of the test application instead of a coarse pipeline-level rerun.&lt;/p&gt;

&lt;p&gt;This improvement is specifically attributable to adopting the MTP retry extension. By contrast, building test projects once and reusing their artifacts was already possible with VSTest; MTP makes those artifacts directly executable and more self-describing, but it did not introduce the concept of prebuilt tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  MTP compared with the common alternatives
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Area&lt;/th&gt;
&lt;th&gt;VSTest&lt;/th&gt;
&lt;th&gt;Microsoft.Testing.Platform&lt;/th&gt;
&lt;th&gt;Framework-specific/custom runner&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Architecture&lt;/td&gt;
&lt;td&gt;External platform plus adapters&lt;/td&gt;
&lt;td&gt;Platform embedded in an executable test app&lt;/td&gt;
&lt;td&gt;Varies by framework or vendor&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ecosystem maturity&lt;/td&gt;
&lt;td&gt;Long-established and broadly integrated&lt;/td&gt;
&lt;td&gt;Rapidly growing, with major .NET frameworks supported&lt;/td&gt;
&lt;td&gt;Often strong within a narrower ecosystem&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Runtime behavior&lt;/td&gt;
&lt;td&gt;Compatibility-oriented and dynamic&lt;/td&gt;
&lt;td&gt;Explicit, modular, and build-time registered&lt;/td&gt;
&lt;td&gt;Depends on implementation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Modern deployment&lt;/td&gt;
&lt;td&gt;Limited for Native AOT and trimming scenarios&lt;/td&gt;
&lt;td&gt;Designed for modern .NET form factors&lt;/td&gt;
&lt;td&gt;Varies&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mixed-language testing&lt;/td&gt;
&lt;td&gt;Supports .NET and non-.NET adapter scenarios&lt;/td&gt;
&lt;td&gt;Focused on .NET&lt;/td&gt;
&lt;td&gt;Usually specialized&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Direct execution&lt;/td&gt;
&lt;td&gt;Normally driven by VSTest tooling&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;dotnet run&lt;/code&gt; or direct executable supported&lt;/td&gt;
&lt;td&gt;Often supported by dedicated commands&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CI migration cost&lt;/td&gt;
&lt;td&gt;Usually lowest for existing pipelines&lt;/td&gt;
&lt;td&gt;Requires checking arguments, reports, and extensions&lt;/td&gt;
&lt;td&gt;May require vendor-specific integration&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Tools such as ReSharper, Rider, NCrunch, and IDE test explorers should not always be viewed as competing platforms. They are developer experiences that may integrate with one or more underlying test technologies. The practical question is not only “Which button runs my tests?” but also “Which execution model produces and controls the test process?”&lt;/p&gt;

&lt;h2&gt;
  
  
  Should every project migrate now?
&lt;/h2&gt;

&lt;p&gt;Not automatically. VSTest remains a sound choice when a repository depends on mature integrations, custom VSTest adapters, existing &lt;code&gt;.runsettings&lt;/code&gt; behavior, or mixed-language test execution such as C++ and JavaScript alongside .NET. Some third-party tooling still has deeper VSTest integration.&lt;/p&gt;

&lt;p&gt;MTP is especially compelling for new .NET test projects, teams targeting Native AOT or trimming, containerized test artifacts, or repositories experiencing VSTest-specific loading and extension problems. It is also attractive when reproducibility and explicit composition are more important than maximum backward compatibility.&lt;/p&gt;

&lt;p&gt;Migration should be deliberate. Choose one platform consistently for a solution or run configuration, verify framework support, map command-line arguments, and replace VSTest loggers or collectors with the corresponding MTP extensions. Do not assume every VSTest switch has an identical MTP equivalent.&lt;/p&gt;

&lt;h2&gt;
  
  
  The larger improvement
&lt;/h2&gt;

&lt;p&gt;Microsoft.Testing.Platform is not merely a faster command or a new adapter. Its real contribution is making test execution look like the rest of modern .NET development: build an application with known dependencies, run the same artifact in different environments, and add capabilities through explicit packages.&lt;/p&gt;

&lt;p&gt;VSTest solved the earlier problem of unifying a fragmented testing ecosystem. MTP addresses the next problem: keeping that ecosystem predictable, portable, and extensible as .NET itself moves into more runtimes, deployment models, and development environments.&lt;/p&gt;

&lt;p&gt;For most teams, that means evolution rather than revolution. Existing test frameworks remain useful, &lt;code&gt;dotnet test&lt;/code&gt; remains familiar, and migration can happen project by project. What changes is the foundation underneath—and that foundation is now much closer to the application model .NET developers already understand.&lt;/p&gt;

</description>
      <category>testing</category>
      <category>dotnet</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
