# A 4B model beats Postgres query plans by 44.7%

Original title: Training a 4B model to produce 81% faster query plans than Postgres

> Hacker News story 49731285 · language: en · submitted 2026-09-16
> Web version: https://zeli.app/story/49731285
> About this feed: https://zeli.app/llms.txt · Agent guide: https://zeli.app/skill.md

▲ 665 · by [polyphilz](<https://news.ycombinator.com/user?id=polyphilz>) · 💬 136
[original](<https://rohanbansal.com/qorl>) · [HN discussion](<https://news.ycombinator.com/item?id=49731285>)

## Summary

A 4B open-weights model was post-trained with supervised fine-tuning and agentic reinforcement learning to produce Postgres query plans that outperform the default planner. Across 113 join-heavy queries, latency dropped 44.7%, even though the model initially failed to produce any plan for 99 of them. The work required a custom GRPO variant for noisy environments and a measurement rig that minimized page cache contention.

## Key quote

> A query optimizer does not aim to be exact in its cost minimization… it aims to be good enough across many types of queries.

## HN discussion (136 comments)

### [refibrillator](<https://news.ycombinator.com/item?id=49733010>)

> “81% faster query plans than Postgres”…on an 8 GB dataset that fits entirely in memory, with shared_buffers constrained to a fraction of that, queries warmed before measuring, and read-only SELECTs.
>
> I would be cautious about over fitting, it’s tough to say if those query plans would really be more optimal than Postgres heuristics at scale and with a bit more realistic OLTP workloads.
>
> In any case, such is life with profile guided optimization. Many of us appreciate how database workloads can drift over time and with scale.
>
> Kudos to the author for getting their hands dirty and writing up their experiments.

### [hamilyon2](<https://news.ycombinator.com/item?id=49733064>)

> Optimal plan construction is math-heavy, algorithm-heavy and vary even by workload. There are options like creating just-in-time indexes, so solution space grows even faster than article presents. Sometimes it is the query planner which is the slow part of total execution time.
>
> LLM is kind of blunt weapon to use here. I am waiting rather for alphago style neural net heuristic.

### [2001zhaozhao](<https://news.ycombinator.com/item?id=49733800>)

> Engineer: "HELP, our production DB is frozen on this query that worked fine before!"
>
> Infra: "Hmm, let's check... Well would you look at that, it seems like your LLM query planner usually works and produces fast queries, but this time when you changed a variable name to trigger query rebuild, it happened to hallucinate and miss an index, would you mind re-running the LLM a few times until you get a faster query?"

### [devsda](<https://news.ycombinator.com/item?id=49732170>)

> \> Frontier intelligence is extremely powerful; the distillation I did off Astra trajectories is proof enough that large models are not going anywhere
>
> Wouldn't admitting this invite trouble due to accusations of distillation flying around between closed and open models.

### [sgarland](<https://news.ycombinator.com/item?id=49734855>)

> Unless they’ve been elided, there were no indices other than the PK on any table, and no additional statistics. There are correlated columns here: a given country may have produced more movies in a given range of years, as its movie industry built up; a given country may produce more TV series than movies, etc.
>
> Nearly every time I’ve seen someone resorting to hints for a query, it’s because their statistics are incorrect. Adding hints is papering over the problem, and can backfire later if the data shape changes.

### [Someone](<https://news.ycombinator.com/item?id=49732105>)

> \> I paid ~$800 to rent a 2x H100 SXM node from Lambda for ~95 hours, and ~$400 in OpenAI API fees to generate the Astra trajectory demonstrations.
>
> \> a tiny 4B model went from not being able to understand the harness it was wrapped in, to achieving a 1.81x geometric mean speedup and a summed latency decrease of 44.7% across a workload of join-heavy SQL queries
>
> I can’t find it in the article (may have skimmed it too much), but I suspect they didn’t include those ~95 hours in the benchmark numbers.
>
> I think all database vendors know their query optimizers could do much better if they could afford to spend lots of time to derive query plans.
>
> ⇒ this may be useful for some workloads, but even then, can you afford to spend hours every now and then to update your 4B model to ensure it still picks a good query plan?

### [jerpint](<https://news.ycombinator.com/item?id=49735286>)

> I have a theory that soon enough every code library will ship a CLI and a very tiny finetune for that specific lib alongside it

### [BirdieNZ](<https://news.ycombinator.com/item?id=49734077>)

> This was a thoroughly enjoyable read, both the writing and presentation. I really liked the level of writing as it's basically introducing a whole lot of advanced topics but at just the right level for a non-AI researcher type of engineer like myself to be able to understand what's going on, and I felt it made some elements of LLMs actually something I could understand rather than wizardry done by maths PhDs. Probably because it's more like applied engineering rather than hard mathematics here. Thank you for a delightful post.

---

## Cite

- Page: https://zeli.app/story/49731285
- Date: 2026-09-16
- Source: rohanbansal.com (https://rohanbansal.com/qorl)
- Hacker News: https://news.ycombinator.com/item?id=49731285

## Other languages

- zh: https://zeli.app/zh/story/49731285 · markdown: https://zeli.app/zh/story/49731285.md
- ja: https://zeli.app/ja/story/49731285 · markdown: https://zeli.app/ja/story/49731285.md
- ko: https://zeli.app/ko/story/49731285 · markdown: https://zeli.app/ko/story/49731285.md
- de: https://zeli.app/de/story/49731285 · markdown: https://zeli.app/de/story/49731285.md
- es: https://zeli.app/es/story/49731285 · markdown: https://zeli.app/es/story/49731285.md
- ru: https://zeli.app/ru/story/49731285 · markdown: https://zeli.app/ru/story/49731285.md
