/sqlserver-tune-a-query:tune-a-queryfrom SQL Server: Tune A Query
Tunes slow SQL Server queries and stored procedures by finding, analyzing, and reproducing them on a database copy. Use when someone names a slow query, query_id, stored procedure, or slow screen/report.
Ships in SQL Server: Tune A Query. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Do the steps in order. Do not write a rewrite before step 4.
Steps 3 to 5 run queries. Run them on a copy of the database, never on production. The copy is a restored or cloned database on a server that no user depends on. If you do not know which server is the copy, ask before you run anything.
Steps 1 and 2, and getting the repro script in step 3, only read. On production, run only the sources that those steps name. Do not install anything on production, and do not run the slow query there.
This skill is for queries that read data. If the slow statement changes data (INSERT, UPDATE, DELETE or MERGE), ask the user how to test it on the copy.
get_top_queries_by_cpu, get_top_procedures_by_cpu, get_query_store_top and get_query_trend. Tool names change between versions, so read the server's tool list first.sp_QuickieStore for Query Store data, or sp_QuickieCache for the plan cache. Both are in Erik Darling's DarlingData repository.sys.query_store_query, sys.query_store_plan and sys.query_store_runtime_stats.analyze_query_plan on the Performance Monitor MCP server.get_query_repro_script, use it. If not, use Copy Repro Script in Performance Monitor, or sp_QueryReproBuilder from DarlingData.sp_executesql with parameters. Do not use local variables, because SQL Server cannot see the value of a local variable when it builds the plan.If dbo.CompareQueryVariants exists on the copy, use it. Put the variants in #variants (variant_id, variant_name, variant_sql, with the lowest variant_id for the original) and execute the procedure. Erik Darling's AI for Database Teams course includes it.
If the procedure does not exist, do the comparison yourself. Run the whole comparison as one script on one connection. A temp table is gone when its connection closes.
sys.dm_exec_describe_first_result_set gives the columns. It cannot describe a query that uses a temp table. In that case, write the column list yourself.INSERT ... EXECUTE, so that the text of the variant runs unchanged. Empty the table of the variant before each run. This run also loads the data into memory, so every timed run starts warm.EXCEPT in both directions, and then compare a count of each distinct row, because EXCEPT ignores duplicate rows. Compare string columns with a binary collation (COLLATE Latin1_General_100_BIN2), and compare their DATALENGTH. A temp table usually ignores case, and = ignores trailing spaces.cpu_time from sys.dm_exec_requests for your own session before and after the run. Read SYSDATETIME() at the same two points. Put both readings and the run in one batch. cpu_time counts the current request, so a reading in another batch starts from zero.For the comparison:
sp_executesql with the same text that the comparison runs. Then compare with the slow values. sp_executesql reuses the plan for the same text with other values, so every plan is built for the compiled values, as on production. Do this run in the same way as the comparison: the same client, the same database, and the same text for each variant. Change only the values. SQL Server reuses a plan only for the same text and the same SET options. With dbo.CompareQueryVariants, write each variant_sql as an sp_executesql call with the values in it. Execute the procedure once with the compiled values and @runs = 1, and then again with the slow values. If you skip this, the original keeps the plan from step 3 and each variant builds a new plan for the slow values. Then every variant looks faster, even a variant that changes only white space.DBCC DROPCLEANBUFFERS, DBCC FREEPROCCACHE) on a server that other people or other agents use.ORDER BY, check that each variant keeps it.TOP with an ORDER BY that has ties can return different rows on each run. Then a correct variant does not match. If a variant does not match, check whether the rows that differ are tied in the ORDER BY.dbo.CompareQueryVariants, NO in results_match means that the results do not match, and error means that the variant failed. Its error_message says why.What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub erikdarlingdata/claude-plugins --plugin sqlserver-tune-a-queryOther plugins' skills for the same job