SwiftRelay

← All sprint notes

First Lap: A Two-Model SQL Tuning Shortlist

First lapsEN

A database query feels slow, and an AI model suggests adding an index. Should you trust it? Not yet. SQL tuning means finding unnecessary database work and reducing it without changing the answer. A model can help explain that work, but your database must confirm whether a proposed fix actually helps.

For a first run, keep your shortlist small: Claude Sonnet 5.5 and DeepSeek V4 Pro. Treat these as two candidates to evaluate, not a guaranteed ranking. The useful model is the one that understands your database context, preserves query meaning, and proposes changes you can verify.

Give Two Models the Same SQL Tuning Task

Start by asking Claude Sonnet 5.5 to explain one troublesome query and identify missing information. Give DeepSeek V4 Pro the identical request as an independent review. Assigning these roles does not assume either model is inherently better at SQL; it creates a manageable test with two perspectives.

Do not judge by confidence or answer length. Look for specific reasoning: which table is being scanned, where rows multiply during a join, and whether a suggested index matches the filter and sorting requirements. A model that asks for the execution plan may be more useful than one that immediately rewrites everything.

Pack the Database Context Before Asking for Fixes

SQL syntax alone rarely explains a performance problem. The same query can behave differently across database engines, table sizes, and data distributions. Supply enough context for a grounded recommendation:

  • Engine and version: Identify PostgreSQL, MySQL, SQL Server, or your actual database.
  • Query and parameters: Include representative values, especially when some values match far more rows than others.
  • Table definitions and indexes: Show column types, keys, and existing index definitions.
  • Approximate table sizes: Explain which tables are large and how their data is distributed.
  • Execution plan: Include the database's description of how it intends to run the query.

Remove credentials, customer records, and sensitive literal values before sending anything. Preserve relevant patterns, such as uneven category sizes, without exposing real identities.

Ask for an Explanation Before a Rewrite

An execution plan is a route map for a query. It shows operations such as scanning a table, finding rows through an index, joining results, and sorting them. Ask the model to explain the likely expensive steps in ordinary language before proposing a fix.

Try this prompt: Explain this query and execution plan for a beginner. Separate observed facts from hypotheses. Identify missing evidence, suggest one minimal change, and describe how to check correctness and performance. Do not assume that adding an index is always beneficial.

Watch for suggestions that alter meaning. Replacing a left join with an inner join can remove unmatched rows. Removing DISTINCT can introduce duplicates. Moving a filter across a join can change results. A shorter query is not automatically a better query.

Check One SQL Change at a Time

Test on a safe, representative environment. Save the original query and plan, apply one proposed change, then compare results and database measurements under similar conditions. Check duplicate counts, null handling, and ordering where ordering is part of the requirement.

Indexes also have costs: they consume storage and add work to writes. Ask whether an existing index already covers the need. Be careful with execution-plan commands that actually run statements; for example, PostgreSQL EXPLAIN ANALYZE executes the statement. Never treat a diagnostic command as harmless without checking its behavior.

Run Your Shortlist Through SwiftRelay

SwiftRelay is a practical way to try your shortlist through one pay-as-you-go relay, with billing per token. It works with OpenAI-compatible SDKs using https://api.fastapirelay.com/v1. Keep the prompt and supplied evidence consistent when evaluating each candidate.

Sign up for SwiftRelay and use the small gift credit for new accounts to begin your SQL tuning trial. Keep the model whose advice is easiest to validate, not the one that sounds most certain. Your finish line is unchanged results and a measured improvement in your own database.

Keep reading

Back for another lap?Log in