
Darling Data: SQL Server Troubleshooting Scripts
Navigatory
Installing
To install every procedure in the list above except sp_QueryStoreCleanup, run Install-All/DarlingData.sql. To install one procedure, run the .sql file in its folder. sp_QueryStoreCleanup is not in the bundle because it removes queries from Query Store. Install it from its own folder.
The monthly releases are the stable way to install. To try a fix before the next release, download DarlingData.sql from the dev-latest pre-release.
Utility scripts
These folders each have their own README:
Other scripts
These folders hold plain SQL scripts and have no README. Each file has a comment at the top.
- Alerts: Create SQL Agent alerts for severity 19 to 25 errors and for errors 823, 824 and 825. Set your operator name in the script first
- Create Long IN List: A demo procedure that builds a very long IN list, to show how long IN lists hurt query performance
- Dynamic SQL Logging: A procedure that logs CPU time, elapsed time and reads for dynamic SQL to a table
- Inline Financial Functions: Inline table-valued functions for common financial calculations, such as Payment and FutureValue
- SQL Debug Example: An example procedure that shows one way to add debug output to a stored procedure
- Stack Column Store Database: A personal demo build script that makes a columnstore copy of the Stack Overflow database. It has hard-coded file paths and is not meant to run as is
Who are these scripts for?
You need to troubleshoot performance problems with SQL Server, and you need to do it now.
You don't have time to track down a bunch of DMVs, figure out Extended Events, wrestle with terrible SSMS interfaces, or learn XML.
These scripts aren't a replacement for a mature monitoring tool, but they do a good job of capturing important issues and reporting on existing diagnostic data
Training and consulting
I teach database teams to work with AI agents on their own databases, in a one-day workshop, a video course, and one-on-one for a day. I also do SQL Server performance consulting.
Support
Right now, all support and Q&A is handled on GitHub. Please be patient; it's just me over here answering questions, fixing bugs, and adding new features.
As far as compatibility goes, they're only guaranted to work on Microsoft-supported SQL Server versions.
Older versions are either missing too much information, or simply aren't compatible (Hello, Extended Events. Hello, Query Store) with the intent of the script.
If you have questions about performance tuning, or SQL Server in general, you'll wanna hit a Q&A site:
Back to top
Pressure Detector
Is your client/server relationship on the rocks? Are queries timing out, dragging along, or causing CPU fans to spin out of control?
All you need to do is hit F5 to get information about:
- Wait stats since startup
- Database file size, stall, and activity
- tempdb configuration details
- Memory consumers
- Low memory indicators
- Memory configuration and allocation
- Current query memory grants, along with other execution details
- CPU configuration and retained utilization details
- Thread count and current usage
- Any current THREADPOOL waits (best observed with the DAC)
- Currently executing queries, along with other execution details
- Blocking chains with lead blockers (@troubleshoot_blocking = 1)
- Optional logging to permanent tables (@log_to_table = 1)
For a video walkthrough of the script and the results, head over here.
Current valid parameter details:
| parameter_name |
data_type |
description |
valid_inputs |
defaults |
| @what_to_check |
varchar |
areas to check for pressure |
"all", "cpu", and "memory" |
all |
| @skip_queries |
bit |
if you want to skip looking at running queries |
0 or 1 |
0 |
| @skip_plan_xml |
bit |
if you want to skip getting plan XML |
0 or 1 |
0 |
| @minimum_disk_latency_ms |
smallint |
low bound for reporting disk latency |
a reasonable number of milliseconds for disk latency |
100 |
| @cpu_utilization_threshold |
smallint |
low bound for reporting high cpu utilization |
a reasonable cpu utilization percentage |
50 |
| @skip_waits |
bit |
skips waits when you do not need them on every run |
0 or 1 |
0 |
| @skip_perfmon |
bit |
skips perfmon counters when you do not need them on every run |
0 or 1 |
0 |
| @sample_seconds |
tinyint |
take a sample of your server's metrics |
a valid tinyint: 0-255 |
0 |
| @log_to_table |
bit |
enable logging to permanent tables instead of returning results. Ignores @what_to_check, @sample_seconds and the @skip_* parameters, and logs all CPU utilization (treats @cpu_utilization_threshold as 0) |
0 or 1 |
0 |
| @log_database_name |
sysname |
database to store logging tables |
valid database name |
NULL (current database) |
| @log_schema_name |
sysname |
schema to store logging tables |
valid schema name |
NULL (dbo) |
| @log_table_name_prefix |
sysname |
prefix for all logging tables |
any valid identifier, 112 characters or fewer |
'PressureDetector' |
| @log_retention_days |
integer |
Number of days to keep logs, 0 = keep indefinitely |
integer |
30 |
| @troubleshoot_blocking |
bit |
show blocking chains instead of pressure analysis |
0 or 1 |
0 |
| @help |
bit |
how you got here |
0 or 1 |
0 |
| @debug |
bit |
prints dynamic sql, displays parameter and variable values, and table contents |
0 or 1 |
0 |
| @version |
varchar |
OUTPUT; for support |
none |
none; OUTPUT |
| @version_date |
datetime |
OUTPUT; for support |
none |
none; OUTPUT |
Back to top
Human Events
Extended Events are hard. You don't know which ones to use, when to use them, or how to get useful information out of them.
This procedure is designed to make them easier for you, by creating event sessions to help you troubleshoot common scenarios:
- Blocking: blocked process report
- Query performance: query execution metrics and actual execution plans
- Compiles: catch query compilations
- Recompiles: catch query recompilations
- Wait Stats: server wait stats, broken down by query and database
The default behavior is to run a session for a set period of time to capture information, but you can also set sessions up to log data to permanent tables.
Requires SQL Server 2012 SP4 or newer (the query_hash_signed action must exist). The blocking event needs the blocked process threshold option set.
For execution examples, run it with @help = 1.
If you set up sessions to capture long term data, you'll need an agent job set up to poll them. You can find an example of that here: Examples
Misuse of this procedure can harm performance. Be very careful about introducing observer overhead, especially when gathering query plans. Be even more careful when setting up permanent sessions!
More resources:
Current valid parameter details:
| parameter |
name |
description |
valid_inputs |
defaults |
| @event_type |
sysname |
used to pick which session you want to run |
"blocking", "query", "waits", "recompiles", "compiles" and certain variations on those words |
"query" |
| @query_duration_ms |
decimal |
(>=) used to set a minimum query duration (milliseconds) to collect data for; 0 removes the floor and collects every duration |
a decimal; fractional milliseconds allowed (e.g. 0.5 = 500 microseconds), 0 captures all durations |
500 (ms) |
| @query_sort_order |
nvarchar |
when you use the "query" event, lets you choose which metrics to sort results by |
"cpu", "reads", "writes", "duration", "memory", or "spills" (any of which you can prefix with "avg" to sort by averages, e.g. "avg cpu"), or "event_time" |
"cpu" |
| @skip_plans |
bit |
when you use the "query" event, lets you skip collecting actual execution plans |
1 or 0 |
0 |
| @keep_prepare_rpc |
bit |
when you use the "query" event, adds a result set of the prepared-statement RPC calls (sp_prepare, sp_prepexec, sp_execute, sp_unprepare, anything matching sp_cursor%, and sp_describe_undeclared_parameters) that are normally filtered out. Lower @query_duration_ms (to 0 or a fractional value) to get rows back. Shows whether a driver is preparing and reusing statements, e.g. pyodbc's fast_executemany |
1 or 0 |
0 |