Skip to content

Commit d4dc7e8

Browse files
authored
Merge pull request #3865 from BrentOzarULTD/3864_sp_kill
Add sp_kill stored procedure (#3864)
2 parents 0bea51b + 8795706 commit d4dc7e8

3 files changed

Lines changed: 940 additions & 5 deletions

File tree

README.md

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@ Navigation
1919
- [sp_BlitzFirst: Real-Time Performance Advice](#sp_blitzfirst-real-time-performance-advice)
2020
- [sp_BlitzIndex: Tune Your Indexes](#sp_blitzindex-tune-your-indexes)
2121
- [Advanced sp_BlitzIndex Parameters](#advanced-sp_blitzindex-parameters)
22-
- Performance Tuning:
23-
- [sp_BlitzLock: Deadlock Analysis](#sp_blitzlock-deadlock-analysis)
24-
- [sp_BlitzWho: What Queries are Running Now](#sp_blitzwho-what-queries-are-running-now)
25-
- [sp_BlitzAnalysis: Query sp_BlitzFirst output tables](#sp_blitzanalysis-query-sp_BlitzFirst-output-tables)
22+
- Performance Tuning:
23+
- [sp_BlitzLock: Deadlock Analysis](#sp_blitzlock-deadlock-analysis)
24+
- [sp_BlitzWho: What Queries are Running Now](#sp_blitzwho-what-queries-are-running-now)
25+
- [sp_kill: Emergency Session Killer](#sp_kill-emergency-session-killer)
26+
- [sp_BlitzAnalysis: Query sp_BlitzFirst output tables](#sp_blitzanalysis-query-sp_BlitzFirst-output-tables)
2627
- Backups and Restores:
2728
- [sp_BlitzBackups: How Much Data Could You Lose](#sp_blitzbackups-how-much-data-could-you-lose)
2829
- [sp_DatabaseRestore: Easier Multi-File Restores](#sp_databaserestore-easier-multi-file-restores)
@@ -331,6 +332,61 @@ It's designed for query tuners, so it includes things like memory grants, degree
331332
[*Back to top*](#header1)
332333

333334

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.
340+
341+
```tsql
342+
/* Just show me what's running - no killing: */
343+
EXEC sp_kill;
344+
345+
/* Kill all lead blockers: */
346+
EXEC sp_kill @LeadBlockers = 'Y', @ExecuteKills = 'Y';
347+
348+
/* Kill a specific session: */
349+
EXEC sp_kill @SPID = 55, @ExecuteKills = 'Y';
350+
351+
/* Kill sleeping sessions with open transactions idle for 5+ minutes: */
352+
EXEC sp_kill @HasOpenTran = 'Y', @SPIDState = 'S',
353+
@RequestsOlderThanMinutes = 5, @ExecuteKills = 'Y';
354+
355+
/* Show what we'd kill for a login, sorted by CPU: */
356+
EXEC sp_kill @LoginName = 'DOMAIN\TroublesomeUser', @OrderBy = 'cpu';
357+
```
358+
359+
Parameters for targeting sessions:
360+
361+
* @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.
386+
387+
[*Back to top*](#header1)
388+
389+
334390
## sp_BlitzAnalysis: Query sp_BlitzFirst output tables
335391

336392
Retrieves data from the output tables where you are storing your sp_BlitzFirst output.

Uninstall.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ SELECT 'sp_BlitzQueryStore' as ProcedureName UNION
2828
SELECT 'sp_BlitzWho' as ProcedureName UNION
2929
SELECT 'sp_DatabaseRestore' as ProcedureName UNION
3030
SELECT 'sp_foreachdb' as ProcedureName UNION
31-
SELECT 'sp_ineachdb' as ProcedureName
31+
SELECT 'sp_ineachdb' as ProcedureName UNION
32+
SELECT 'sp_kill' as ProcedureName
3233

3334
--End Variables
3435

0 commit comments

Comments
 (0)