Urgent.News

One page, thousands of outlets. See who else covered it.

Editions

Tech

A Generated SQL Query Got Faster by Returning Fewer Rows. Test That Before You Merge It

Have you ever watched a generated SQL refactor run faster and assumed it must be correct? That assumption breaks down when the speedup comes from an inner join that silently drops rows the old left join preserved. The output still looks plausible because every displayed row has a customer name, so a quick smoke test misses the loss. I treat a generated query change as a patch, not a proof, until…

A generated SQL query may appear to run faster due to returning fewer rows, but this speedup may actually result in losing data. A quick visual check of the displayed rows may miss the fact that some rows have been silently dropped during the join conversion. It is crucial to treat a modified generated query as a patch, not a proven solution, until a differential check confirms the old and new result sets are identical.

To ensure this, start with a tiny data fixture containing an order referencing a non-existent customer. This scenario often occurs in legacy systems. The original query using a LEFT JOIN preserves all orders, including the orphaned one, while the generated INNER JOIN rewrite returns only four rows, dropping the orphaned order. This discrepancy highlights that a performance improvement can come at the cost of changing the query's result, which is not an optimization.

Before implementing a new query, build a golden result check by creating a known-good baseline and normalizing the result set. Compare the candidate query's result with this baseline, not just the first few rows. If the baseline and candidate return the same normalized set for various edge cases like orphaned children, parents without children, duplicate children, NULL values, boundary values, and different insertion orders, you can have more confidence in the query's correctness.

However, relying solely on a single candidate rewrite is insufficient. Use MonkeyCode's free model access to generate multiple candidates, then test each one through the same golden result check. This approach treats model output as a hypothesis that must survive automated testing, rather than blindly accepting the first plausible rewrite.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

A Free Model vs 30 Security Advisory Records: An Accuracy Test You Can Rerun

A single wrong severity label can push a bad dependency upgrade into production. An advisory said "moderate." The package in our tree was critical. The model guessed low.

  • Model's accuracy tested with 30 hand-checked advisories
  • Missed several moderate advisories, high precision for criticals
  • Failure modes: vendor word mapping, package name collisions

More from Tuesday 18 August →