SwiftRelay

← All sprint notes

Pre-Flight Checklist: Your SQL Tuning Model Shortlist

First lapsEN

Scope the Query Before the Green Light

Tuning starts with a sentence, not with an index. Before you open an editor or a chat window, write down which statement you are tuning and what a good outcome actually looks like. A query that feels slow inside a dashboard can be perfectly healthy when it runs for one user at a time, and a rewrite that helps a nightly batch job can quietly wreck an interactive screen. Scope is what keeps your model shortlist honest.

Use this pre-flight list before you touch anything else:

  • The exact statement — copy the real SQL, including literals, parameters, join order, filters, ordering, and any limits. The version in your repository is often not the version the database executes.
  • The workload shape — decide whether this is a point lookup, a filtered scan, an aggregation, or a report. Tuning advice that is correct for one shape is wrong for another.
  • The data volume — record rough row counts for each table and the growth pattern. Cardinality is the single biggest input to any plan decision.
  • The success condition — describe the plan change you want to see, such as an index scan replacing a sequential scan, rather than a vague feeling that things should be quicker.
  • The regression guard — list the statements and write paths you must not break. Every index you add is a cost somewhere else.

Write that scope as a short paragraph you could hand to a teammate. If you cannot describe the problem in three sentences, you are not ready for a model yet, because you are still investigating rather than tuning.

Gather the Plan and the Statistics First

Models are excellent at reading evidence and poor at inventing it. Your job in the pit stop is to collect the evidence before the green light. Run the statement with the execution plan enabled, capture the actual plan rather than the estimated one where your engine supports it, and save both. The gap between estimated row counts and actual row counts is where most tuning mysteries live.

  • Scan types — note where the plan uses sequential scans, index scans, bitmap scans, or nested loops with repeated probes. Large scans on large tables deserve attention; scans on tiny tables do not.
  • Join strategy — check which tables are on the outer and inner side of each join, and whether the planner has a realistic estimate for each.
  • Statistics freshness — stale or default statistics lead the planner to pick the wrong shape. Refreshing them is the cheapest fix available.
  • Skew and nulls — a status column where a single value covers most rows will confuse selective-index math unless you use partial indexes or extended statistics.
  • Existing indexes — list what already exists, which ones are partial or unique, and which are redundant. The best new index is often the one you already have but do not use.

Attach this evidence to every prompt you send. A model that receives the real plan, the real schema, and the real constraints will propose a change you can defend in code review. A model that receives only the query will propose a change you have to test anyway.

Shortlist Two Models and Give Them the Same Brief

Keep the shortlist narrow. Two capable models answering the same brief give you something far more useful than ten models answering different ones: agreement signals confidence, and disagreement points straight at the evidence you forgot to gather.

For plan reading and index reasoning, Claude Sonnet 5.5 is a strong first pick, because long plans with many joins reward a model that keeps context together while it argues about cardinality. DeepSeek V4 Pro works well as a second opinion on rewrites, especially when you want a different take on set-based alternatives and window functions.

Give both the identical brief: the scope paragraph, the schema, the plan, the statistics, and the constraint that the rewrite must return the same rows. Ask each to rank its hypotheses, name the evidence that supports the top one, and tell you what it would measure next. When the two shortlist answers converge on the same table and the same join, you have a target worth testing. When they diverge, do not average them; go back to the plan and resolve the ambiguity yourself.

Wire the Run Through SwiftRelay

Once the shortlist is set, you need one entry point for every model you want to try. SwiftRelay is a pay-as-you-go relay for Claude, GPT, Gemini, DeepSeek, Kimi, GLM, Qwen, Grok and other models, and it works with Claude Code, Codex, Gemini CLI and any OpenAI-compatible SDK. Billing is per token, so a tuning session costs exactly what you use.

The API host is https://api.fastapirelay.com. Claude Code and Gemini CLI take the bare host, while OpenAI-compatible SDKs add /v1 to the base URL. That single detail saves an hour of debugging when a client reports an unexpected route error mid-session.

Create a SwiftRelay account to claim the small gift credit and run your first tuning lap, and note that inviting friends earns 14% of their top-ups. With one account you can move between shortlist candidates without changing tooling, which keeps the comparison fair.

Write Prompts Like a Pit Board

A tuning prompt should be short, structured, and impossible to answer with generic advice. Treat it like a message on a pit board: only the information the engineer needs to act on right now.

  • State the job — read this plan, name the dominant cost, and rank the likely causes.
  • Supply the artefacts — schema, indexes, plan output, table sizes, and the parameter values used in the run.
  • Constrain the answer — ask for index suggestions with their write-path cost, and rewrites that are guaranteed to return the same result set.
  • Ask for verification — request the plan snippet or test query that would confirm the hypothesis, so the answer ends in evidence rather than opinion.
  • Forbid guesswork — ask the model to say explicitly where the evidence is missing instead of inventing a plausible schema.

Keep the prompt as a reusable template. When the next slow statement arrives, you refill the artefacts and the format stays consistent, which makes answers comparable across sessions and across your shortlist.

Verify the Rewrite and Close the Lap

The lap is not over when the model produces SQL. It is over when the plan proves the change. Re-run the statement with the plan enabled, compare it to the baseline you captured in the first section, and confirm that the dominant cost has moved. Check estimated versus actual row counts again, because a rewrite can look right while hiding a cardinality regression.

Then test the surrounding surface. Run the statements in your regression guard, check the write paths affected by any new index, and make sure nothing that relied on the old access pattern has changed shape. If a rewrite relies on a specific data distribution, record that assumption in a comment so the next engineer knows when it stops holding.

Finally, keep this pre-flight checklist as a standing document: scope, evidence, shortlist, prompt, verification. The teams that get consistently better plans are rarely the ones with the cleverest single rewrite; they are the ones who repeat the same disciplined run every time the green light comes on.

Keep reading

Back for another lap?Log in