Pick a model for SQL query tuning by testing its diagnosis, not admiring its rewritten SQL. Your selection process should reward preserved results, evidence from execution plans, and changes you can verify. Start with a repeatable task, compare candidates under the same conditions, and let database results decide which model earns a place in your workflow.
1. Define the SQL tuning decision before choosing a model
Separate the work into three jobs: explaining a plan, proposing a change, and reviewing whether that change is safe. Use your evaluation to decide whether one model handles all three or whether difficult cases deserve a second review.
For an initial comparison, try Claude Sonnet 5.5 and Gemini 3.1 Pro as candidates, not predetermined winners. Give both the same evidence and instructions. Avoid selecting by brand, answer length, or how confidently a response recommends an index.
2. Build a sanitized query evidence packet
Prepare the database engine and version, original SQL, representative parameter values, table definitions, existing indexes, approximate table sizes, and execution plan. Remove credentials and personal information while preserving the relationships and data characteristics needed to understand the problem.
For PostgreSQL, distinguish estimated plans from observed execution. EXPLAIN describes the planner's chosen approach; EXPLAIN ANALYZE executes the statement and supplies actual execution statistics. Label the evidence accurately so candidates cannot quietly treat estimates as measurements. ([postgresql.org](https://www.postgresql.org/docs/current/sql-explain.html?utm_source=openai))
Collect execution evidence in a controlled environment. Because EXPLAIN ANALYZE actually runs the statement, do not treat it as a harmless preview, especially for statements that modify data. ([postgresql.org](https://www.postgresql.org/docs/current/sql-explain.html?utm_source=openai))
3. Require a diagnosis tied to plan evidence
Ask each candidate to explain the suspected bottleneck before proposing SQL. Require it to name the relevant plan node, separate observations from hypotheses, and identify missing information that could change its recommendation.
In PostgreSQL, comparing estimated and actual row counts helps assess the accuracy of planner estimates. Ask the model to identify meaningful mismatches rather than simply declare every sequential scan a problem. ([postgresql.org](https://www.postgresql.org/docs/current/using-explain.html?utm_source=openai))
Include buffer evidence when available. PostgreSQL's buffer reporting distinguishes hits, reads, and other buffer activity, giving the discussion more substance than an unsupported claim that the query needs an index. ([postgresql.org](https://www.postgresql.org/docs/current/sql-explain.html?utm_source=openai))
4. Compare candidates with one fixed tuning prompt
Use a reusable prompt such as:
Review this query for the stated database version. Explain the suspected bottleneck using supplied plan evidence. Propose one minimal change. Preserve result semantics, identify assumptions, and provide a validation procedure. Do not invent indexes, table statistics, or measured improvements.
Score both answers against the same checklist:
- Does the diagnosis cite evidence actually present in the packet?
- Does the proposed SQL match the stated database dialect?
- Does the answer discuss duplicates, nulls, and ordering where relevant?
- Does it provide a reversible, testable next step?
- Does it acknowledge uncertainty instead of fabricating measurements?
5. Validate one SQL change before accepting the recommendation
Test the proposed rewrite or index separately. Compare results against the original query using representative parameters and deliberate edge cases. Check row contents and duplicate counts, not merely whether both queries return the same number of rows.
Also check whether statistics need attention before accepting a structural change. PostgreSQL's documentation emphasizes keeping statistics current so the planner can make informed choices. Require the model to consider that possibility rather than automatically prescribing new indexes. ([postgresql.org](https://www.postgresql.org/docs/current/sql-explain.html?utm_source=openai))
Record rejected suggestions as well as accepted ones. A candidate that produces polished but semantically incorrect rewrites should not win your evaluation.
6. Route SQL tuning through a measured model workflow
SwiftRelay is a practical option for running this comparison through an OpenAI-compatible SDK and paying per token. Keep your prompt and evidence packet unchanged while switching candidates, and record token spend alongside correctness and review effort.
Sign up for SwiftRelay to run your first controlled SQL tuning comparison. Select the model that consistently produces verifiable improvements on your workload, and reserve a second review for changes with substantial operational risk.