You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -331,6 +332,61 @@ It's designed for query tuners, so it includes things like memory grants, degree
331
332
[*Back to top*](#header1)
332
333
333
334
335
+
## sp_kill: Emergency Session Killer
336
+
337
+
When the server is on fire and you're about to restart it out of desperation, run sp_kill instead. It collects all running session data, recommends which queries to kill (with reasons), optionally executes the kills for you, frees their plan cache entries, and logs everything.
338
+
339
+
By default, sp_kill runs in read-only mode - it just shows you what's happening and suggests what to kill. You have to explicitly opt in to actually killing sessions.
*@ExecuteKills = 'Y' or 'N' (default 'N') - whether to actually kill, or just show recommendations. When set to 'Y', you must also specify at least one targeting filter to prevent accidentally killing everything.
362
+
*@SPID - target a specific session ID.
363
+
*@LoginName - kill sessions belonging to this login.
364
+
*@AppName - kill sessions from this application (supports LIKE wildcards).
365
+
*@DatabaseName - kill sessions using this database.
366
+
*@HostName - kill sessions from this host.
367
+
*@LeadBlockers = 'Y' - kill only lead blockers (sessions blocking others but not blocked themselves).
368
+
*@ReadOnly = 'Y' - only kill read-only queries (SELECTs with no writes).
369
+
*@SPIDState - 'S' for sleeping sessions only, 'R' for running only, empty string for both (default).
370
+
*@HasOpenTran = 'Y' - only target sessions with open transactions. Combine with @SPIDState = 'S' to catch sleeping sessions with forgotten transactions.
371
+
*@RequestsOlderThanMinutes - only target sessions whose last request started at least this many minutes ago.
372
+
*@OmitLogin - exclude sessions belonging to this login from kill recommendations.
373
+
*@OrderBy - sort order for kill recommendations: duration (default), cpu, reads, writes, tempdb, transactions. Useful when you want to kill the worst offenders one by one.
374
+
375
+
Safety features:
376
+
377
+
* Never kills system sessions (session_id <= 50) or its own session.
378
+
* Never kills sessions that are already rolling back.
379
+
* Before each kill, verifies the session still has the same query it had when captured. If the query changed (because the original finished and a new one started on the same session ID), the kill is skipped.
380
+
* Frees the plan cache entry for each killed query via DBCC FREEPROCCACHE.
381
+
* When @ExecuteKills = 'Y', shows progress messages like "Killing query 3 of 12 (SPID 78)..." so you can follow along.
382
+
383
+
Output table logging:
384
+
385
+
*@OutputDatabaseName, @OutputSchemaName, @OutputTableName - pass all three to log results to a persistent table. The table is created automatically if it doesn't exist, and each run appends new rows. Includes who ran sp_kill, when, what parameters they used, what sessions were running, which ones were killed, and any errors.
0 commit comments