/microsoft-sql-admin:improve-query-performance [QueryText or ShowplanFilePath]from Microsoft SQL administration
Optimizes, tunes, and speeds up SQL queries using execution plans, rewrites, indexes, and hints. Default skill for generic performance requests.
Ships in Microsoft SQL administration. 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.
Use this skill when the user wants to optimize, tune, or improve a SQL query's performance. Start a bounded first look for generic requests. Follow an explicit request for plans, rewrites, indexes or hints directly; full analysis requires that broader scope.
resource-usage-* or health-audit-* skillsmicrosoft-sql-admin plugin (mssql_list_database_connections, mssql_connect_database, then mssql_execute_query); any other SQL tool the agent host provides for running T-SQL and reading result sets; sqlcmd from a terminal. If using the SET-based scripts below instead of a dedicated plan tool, the host must interpret GO batch separators in one session (or explicitly preserve that session between calls), because SET SHOWPLAN_XML and SET STATISTICS XML only apply to the session that sets them. Plan, rewrite, index, and hint analysis are read-only by default; anything that executes the query, or creates objects, runs only with the user's permission. If the SQL Tools MCP server is not connected or lists no connections, follow the sql-tools-mcp-setup skill, which asks before installing or configuring anything. If no SQL tool can be made available, give the user the T-SQL to run and ask for the results..sqlplan or .xml showplan file.With SQL Tools MCP, do not send GO separators to mssql_execute_query, and do not rely on session SET options surviving a second tool call. That tool accepts raw T-SQL, not a sqlcmd script. Prefer getEstimatedShowplanForQuery for estimated-plan retrieval; it is in the server's default tool set, so when it is missing the host has filtered the tools or runs an older build. Use getShowplanWithExecutionStatsForQuery only with explicit authorization to execute the workload. Call a plan tool once per plan and reuse its result; do not loop re-running the statement through mssql_execute_query to compare variants. Otherwise use supplied plan XML or a verified same-session sqlcmd capture. Keep estimated-plan capture separate from execution.
For a generic request such as "make this query faster", inspect the query text and supplied evidence immediately. State at most three concrete observations and which are hypotheses. Do not stop at a scope menu. Reuse a matching supplied plan, preserving actual counters; otherwise obtain an estimated plan only when a suitable connected capability and compilation permission are available. With only a plan, analyze that capture directly. An evidence-only task stays evidence-only.
Check that a supplied capture belongs to the query before using its counters.
If correspondence is uncertain, disclose it; do not attach unrelated runtime
evidence to the query. Reuse an initialized connection. Obtain at most one matching
estimated capture when needed; do not compile again just to obtain its cost.
RewriteQuery, when present, returns workflow instructions rather than rewritten
SQL. Follow those instructions only within the requested rewrite scope.
Use available compact metadata/warning tools first when they answer the question; inspect the relevant XML when they do not. For tools absent from the active catalog, use the portable procedures in the specialized skills. Do not assume that a model-visible local file is reachable by a remote MCP process.
Do not infer the chosen access path from query syntax alone. Some conversions, including date extraction from supported datetime types, can produce an optimizer range seek. A half-open range is a useful candidate, but confirm the data type, access and residual predicates, rows read, and matching plan before saying the original must scan or the rewrite is faster. Without that evidence, label it a hypothesis and avoid universal rules about functions on indexed columns.
Quick triage ends with the strongest supported finding, remaining uncertainty, and one useful next investigation. It does not automatically rewrite the query, try every hint, create indexes or execute the workload. Estimated compilation is not workload execution, but may take locks or trigger statistics work; respect stricter requested boundaries.
An explicit plan-only, rewrite-only, index-only or hint-only request goes directly to that workflow. For explicitly requested full analysis, continue through the applicable steps without requesting the same scope again. User authorization for an investigation does not authorize workload execution or schema changes. When execution has already been explicitly authorized, honor that scope instead of asking repeatedly; reject ineligible execution even if a tool is available.
If the user provides a .sqlplan or .xml file path, read that file directly.
Otherwise, generate one (see the plan-analysis skill for details):
sqlcmd or a verified batch-aware host, send SET SHOWPLAN_XML ON;, the query, and SET SHOWPLAN_XML OFF; in one session, each in its own batch separated by GO; the result is the showplan XML. Never send the query in a separate request on the assumption that SHOWPLAN_XML is still on — if the tool does not keep the session, the query would execute.SET STATISTICS XML ON; and SET STATISTICS XML OFF;. Require explicit authorization for executing the query; reuse authorization already given for this scope, as it will run against the database and may be long-running or resource-intensive..sqlplan file and tell the user the path.Follow the plan-analysis skill workflow. In summary:
plan-analysis bottleneck procedure to identify cost-dominant subtrees covering ≥80% of total cost.
See the plan-analysis skill for full details on choosing the right procedure and report template.
Reuse existing actual statistics. Offer runtime capture only when the evidence is estimated, eligible query text is available and the request permits that next step. Do not offer execution for a supplied-plan-only or evidence-only task. When delegated, preserve the caller's output format and requested depth.
Follow the query-rewrite skill workflow. In summary:
query-rewrite procedure on the original query text: apply its rewrite strategies, keep only rewrites whose estimated cost (from SET SHOWPLAN_XML) is lower than the baseline and that are not NotEquivalent, and rank them by cost.query-rewrite skill's follow-up rules: select an accepted candidate not previously selected as the primary or a follow-up recommendation, even if it was already listed in the report. Preserve its cost and equivalence status. Do not rerun the rewrite strategies merely to select an existing candidate. If none remain, say so; rerun only when the user requests further exploration. A changed query or database context requires a new investigation.See the query-rewrite skill for full details and report template.
Use the LLM (no dedicated tool) to suggest index improvements based on:
<MissingIndexes> elements)Provide CREATE INDEX statements with equality columns, inequality columns, and included columns.
Offer evaluation: after presenting the candidate CREATE INDEX statements, ask the user whether they want you to measure the estimated plan-cost impact of the candidates on this query (for example: "Would you like me to measure the plan-cost impact of these candidate indexes? This creates the indexes, compares the estimated plan, then drops them. Run it on a non-production copy if possible."). Only proceed if the user accepts. Do not run it automatically.
When the user accepts:
ONLINE = ON where the edition supports it.CREATE NONCLUSTERED INDEX [eval_IX_Orders_CustomerID] ON dbo.Orders (CustomerID) INCLUDE (OrderDate);.SET SHOWPLAN_XML ON (see Step 2). Read StatementSubTreeCost, check whether the plan uses the new indexes, and report the cost delta vs the baseline.DROP INDEX [eval_IX_Orders_CustomerID] ON dbo.Orders;), and confirm with SELECT name FROM sys.indexes WHERE name LIKE N'eval[_]%'; that none remain.If the user declines, or any step fails (for example, insufficient permissions), drop anything you created, say the estimate was not produced, and keep the recommendations based on the plan analysis. For a broader workload-based evaluation, suggest the documented Database Engine Tuning Advisor on SQL Server, or automatic tuning index recommendations on Azure SQL Database.
Follow the query-hint-tuning skill workflow. In summary:
SET SHOWPLAN_XML ON) for the query with each candidate OPTION (<hint>) and compare QueryPlanHash and StatementSubTreeCost with the baseline to find hints that produce distinct plans. Report the best hint by estimated cost.SET STATISTICS XML ON to measure actual runtime metrics (elapsed time, CPU time, logical reads).See the query-hint-tuning skill for full details, hint selection guide, and report template.
Present the final report using this template:
## Query Tuning Report
### Section 1: Baseline Performance Metrics
- Statement: (first 200 chars of query)
- Total Estimated Cost: {{cost}}
- DOP: {{degree of parallelism}}
- Key Bottlenecks: {{bottleneck summary from Step 2}}
### Section 2: Query Rewrite Recommendations
```sql
{{REWRITTEN_QUERY or "No rewrite found"}}
{{List each transformation applied and its rationale}}
{{CREATE INDEX statements with rationale}}
{{Table showing each hint tested, plan hash, estimated cost, elapsed time, CPU time}} {{List any hints that produced duplicate plans (same plan hash as baseline)}}
{{If Phase 2 was not run, omit elapsed-time and CPU recommendations and state: "Runtime performance not measured — Phase 1 results are tentative."}}
{{If Phase 2 ran but no hint met its configured minimum percentage gain, report that no hint met the configured elapsed-time improvement threshold over baseline, and include that threshold (default 50%). Only present runtime-qualified recommendations under the query-hint-tuning threshold rule.}}
{{2-3 sentence summary of findings and top recommendation}}
## Important Rules
1. **Base recommendations on the plan-cost comparisons from those procedures, not on ad-hoc opinion.** Do not provide your own hints or rewrites without first running the `query-hint-tuning` or `query-rewrite` procedure and comparing plan costs, and label estimated plan costs as estimates, never as measured results.
2. **Apply the conditional scope gate consistently.** Ask Step 1 only when the request is genuinely ambiguous. When the user explicitly selects or clearly implies an analysis path, proceed with that path without a redundant menu.
3. **Run only the explicitly selected or clearly implied scope.** Do not silently expand it. For a bounded supplied-evidence task, analyze only that evidence and identify anything that cannot be established from it.
4. **State which path you followed** in the final report so the user understands what was explored.
5. **Report the time each step took** so users understand the time investment.
6. **Add a disclaimer** asking the user to verify generated results before applying to production.
## Related Skills
- `missing-index-analysis` — DMV-based missing index analysis for a database or table
- `slow-query-investigation` — Broader diagnostic workflow for intermittent slow queries from Query Store data
- `investigate-statistics-issues` — Stale statistics can cause bad cardinality estimates and suboptimal plans
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub microsoft/microsoft-sql --plugin microsoft-sql-admin47 skills in this plugin
/active-connectionsUse when the user asks who is connected, which sessions are idle or hold the oldest open transaction, or how connections split by login or app. If a client can't connect, use `connectivity-troubleshoot`./always-encrypted-columnsUse when the user asks which plaintext columns can or should use Always Encrypted, or why a column is disqualified. Eligibility only: not for listing already-encrypted columns or their keys./authentication-diagnosticsUse when a login fails (18456, 'Login failed for user'), a user can't open a database, a permission is denied, or an Entra or managed identity login fails. For network errors, use `connectivity-troubleshoot`.Other plugins' skills for the same job