OPTIMIZATION OBJECTIVE
Set up a rigorous execution plan review for the nightly settlement query.
Optimization goal: execution plan analysis — read the plan the engine actually chose, operator by operator.
Establish why this query is expensive before changing anything. Every recommendation must trace to evidence from this query, this plan, or this schema — no generic database advice.
DATABASE CONTEXT
Platform: SQL Server.
- Use actual execution plans with SET STATISTICS IO, TIME ON — estimated plans hide spills and estimate-vs-actual gaps.
- Reason about the clustered index: every nonclustered index carries the clustering key, so a wide clustering key taxes every index.
- Check for parameter sniffing when performance varies by parameter: a plan compiled for unrepresentative values; consider OPTIMIZE FOR or RECOMPILE only with evidence.
- Watch for implicit conversions (VARCHAR vs NVARCHAR) — they appear as CONVERT_IMPLICIT in the plan and turn seeks into scans.
- A Key Lookup executing many times is a covering-index candidate — check its actual executions in the plan.
QUERY CONTEXT
Query:
```sql
[Paste the SQL query here]
```
Execution plan: not provided. Do not invent one. State the exact command to capture it — the Actual Execution Plan plus SET STATISTICS IO, TIME ON — and mark every plan-dependent conclusion as pending that evidence.
Tables and row counts: not provided — do not assume row counts. State the cardinality questions that block firm conclusions.
Existing indexes: not provided — do not assume any index exists. Before recommending new indexes, list what must be checked about the current ones.
PERFORMANCE SYMPTOMS
Settlement batch went from 40 minutes to 3 hours over six months; plan not yet captured.
OPTIMIZATION GOAL
Primary goal: Execution Plan Analysis.
Analysis priorities for this goal:
1. Work from the actual plan, not the query text: optimize what the engine does, not what the SQL looks like.
2. Find the cost concentration: the one or two operators where most of the time or rows go.
3. Compare estimates against actuals: a large estimate-vs-actual gap means stale statistics or unmodelable predicates — and invalidates every downstream choice.
4. Follow the row counts between operators — where rows explode or collapse is where the story is.
EVIDENCE REVIEW
Evidence mode: Forensic Optimization.
- Every conclusion must cite its evidence by name: the plan operator, the row count, the schema fact.
- No recommendation without an evidence trail — where the evidence does not exist yet, output the exact command to gather it instead of the recommendation.
- Label every statement: FACT (from the provided query, plan, or schema), ASSUMPTION (believed without evidence), or HYPOTHESIS (a testable claim with its test).
- Attach a confidence level to every recommendation, justified by the strength of its evidence.
- Generic advice is forbidden: a recommendation that would read the same without ever seeing this query does not belong in the analysis.
BOTTLENECK ANALYSIS
- Identify the bottlenecks and, for each: what it costs, why it matters for this workload, and the estimated share of the total cost.
- Expected analysis areas for this goal: Highest-cost operators; Estimate vs actual gaps; Plan warnings: spills, conversions, missing stats; Choices the optimizer was forced into.
- Distinguish the bottleneck from its symptom — a slow sort caused by a missing filter is a filter problem, not a sort problem.
OPTIMIZATION OPPORTUNITIES
- Order opportunities by expected impact, highest first; state the basis for each estimate.
- For each opportunity: the change, the expected effect, and the evidence that predicts it.
- Any query rewrite must return identical results — call out every difference in NULL handling, duplicates, and ordering, or state explicitly that there is none.
- Do not tune speculatively: a change without an evidenced problem is risk without benefit.
INDEX RECOMMENDATIONS
- Every index recommendation must name the exact predicates, joins, or sorts it serves — no index without a clause.
- Justify column order for composite indexes, and say whether the index should cover (and at what storage and write cost).
- State each index's write tax: which inserts and updates it slows, and whether the workload can afford that.
- Check existing indexes first: a near-miss index that could be extended beats a new overlapping one.
TRADEOFF ANALYSIS
- Every recommendation carries its costs: write amplification, storage, maintenance burden, plan-stability risk, staleness (for pre-aggregation).
- State when NOT to apply each recommendation — the workload shape under which it backfires.
ASSUMPTIONS
- List every assumption made about data volumes, value distributions, index state, or workload patterns.
- Mark each assumption VERIFIED (with its evidence) or UNVERIFIED (with the query or command that would resolve it).
- Any recommendation that depends on an UNVERIFIED assumption must be flagged as conditional on it.
NON-GOALS
- Do not invent execution plans.
- Do not assume indexes exist.
- Do not assume row counts.
- Do not recommend changes without justification.
- Separate facts from assumptions throughout.
- Explain the tradeoffs of every change that has them.
- Schema redesign and application-level changes are out of scope — unless the evidence shows no query-level fix exists, in which case say so and stop.
OUTPUT REQUIREMENTS
- Present recommendations as an ordered list, highest expected impact first, each with its evidence and its tradeoffs.
- For each recommendation, include the verification step: how to measure the improvement — the Actual Execution Plan plus SET STATISTICS IO, TIME ON before and after, on production-shaped data.
- Where essential evidence is missing, the first recommendations are the commands to gather it — not guesses in its place.
- End with the open questions: what could not be determined from the provided context, and what would settle each.