A 4B-parameter model is generating query plans that run 81% faster than PostgreSQL's built-in cost-based optimizer.
That's the work behind QORL. It hit the front page of Hacker News this week — 48 points, 6 comments. Not a viral explosion, but the engineering signal is clear: small models are starting to chew through the hard parts of traditional database systems.
Where the 81% comes from
QORL doesn't replace the executor. It replaces the planner's output. When Postgres receives a SQL query, it normally flows through parser -> analyzer -> rewriter -> planner -> executor. QORL sits at the planner step and generates the plan tree directly with the model, skipping cost estimation and join ordering entirely.
In other words: the model does what the optimizer used to do.
The 81% is end-to-end query latency, not a single-step speedup. From the moment a user fires off a SQL statement to getting results back, the whole pipeline is 81% faster. The number comes from the author's comparisons on TPC-H and JOB (Join Order Benchmark) — the former being the OLAP standard, the latter specifically designed to stress-test planner join selection.
JOB is the key benchmark here. It was built to expose CBO weaknesses: snowflake schemas across dozens of tables, low-selectivity columns, every kind of outer join. Any planner gets tangled up in that workload. QORL's gains on JOB are even more dramatic than on TPC-H — the author doesn't give exact numbers in the blog, but the abstract hints at close to a 5x improvement.
The engineering implication: decades of cost models, join order search, and statistics maintenance — all bypassed by a 4B model in a single inference-time forward pass on structured data.
Why end-to-end generation instead of RAG
Learning-based query optimization has split into two camps over the past few years:
Path A: LLM + hints
Stuff the schema, statistics, and current SQL into a prompt, let the model output hints like /*+ INDEX(...) */. The executor is still the CBO, just nudged.
Path B: End-to-end plan tree generation
Have the model output a structured representation of the plan tree directly, bypassing the CBO entirely.
QORL picks Path B. It's the harder road — the model has to understand the semantics of relations, joins, filters, and aggregates, and guarantee the output is a valid plan. The payoff is bigger though: every CBO assumption (uniform data distribution, independent correlations, accurate cost models) gets bypassed.
What does Path B cost?
- Where does training data come from? The supervision signal for plan trees is expensive. Either run EXPLAIN on historical queries to collect plans, or use a ground-truth optimizer (LP solver) to label backwards. The first approach inherits the existing planner's ceiling; the second is computationally explosive.
- Output must be a valid plan. SQL can be sloppy and still execute. Plans can't. One wrong join type returns wrong results, not slow results.
- Latency budget is tight. The planner step usually gets a few milliseconds to a few tens of milliseconds. A 4B model doing one forward pass on a GPU fits that budget. A 70B model blows past it.
Is 4B parameters enough?
Intuition says query planning is complex and needs a big model. QORL's answer is no.
From my own experience: for structured output tasks, small models are more reliable than large ones. Two reasons:
- Format constraints are tight. Plan trees are JSON or DSL — tokens must land in a valid space. 70B models have too much "creativity" and tend to output operator combinations that don't execute. 4B models learn a narrower distribution and stay stable.
- Inference cost. The planner runs on every query. A 70B model might take tens to hundreds of milliseconds per inference; a 4B model under vLLM can be pushed under 5ms.
It's not hyperbole to say the cost difference is two orders of magnitude. A 70B model on a single A100 runs about $1/hour. A 4B model on an L4 or 4090 runs about $0.10/hour. Multiply by QPS and you're looking at roughly 100x cost difference.
For a core component like Postgres, production QPS in the thousands or tens of thousands is common. The planner gets called thousands of times per second. A 70B model can't sustain that. A 4B model can.
Can it go to production today?
No. Several hard problems remain.
Training data bias
QORL's training set is almost certainly drawn from a specific workload (probably JOB + TPC-H + some synthetic queries). Drop it into production and hit query patterns it's never seen, and what comes out? Unknown. Garbage plan at best, silent fallback at worst.
Fallback for unseen patterns
QORL presumably has a fallback — low-confidence queries go back to the native CBO. But where do you set that threshold? Set it too high and garbage plans reach production. Set it too low and fallback fires constantly, defeating the purpose.
Drift across Postgres version upgrades
Major Postgres version upgrades change planner behavior — new operators, new join methods, tweaked cost constants. The plan space the model was trained on diverges from the real environment, and accuracy drops. Either retrain on every major version bump, or lock the model to a specific Postgres version.
Observability
When the CBO misbehaves, you can EXPLAIN and see cost, row estimates, actual rows. When the model misbehaves, you can only inspect the plan tree structure — you can't reason about why it chose what it chose. Debugging is one level harder.
Cold start
New database, new schema, new data distribution — the model has never seen it. How do you bootstrap initial training data? The old way: run production queries for a week, EXPLAIN to collect plans. During that bootstrap period, the model might actually hurt performance.
So QORL's current status: high research value, but still a way from production-ready engineering. The direction is right though.
Editor's take
81% isn't the point. The point is that "specialized small models replacing traditional optimizer components" is starting to work.
Over the past few years, LLMs in databases have mostly followed the RAG + hint path — models as advisors, never touching the execution path. End-to-end generation attempts like QORL were considered too expensive to attempt. A 4B model trained for structured output brought the cost down to something production can stomach.
The real implication: every rule-based and cost-model-based component inside a database kernel is a potential replacement target.
- Buffer pool replacement: currently LRU/K heuristics. Can a model predict access patterns?
- Vacuum scheduling: currently dead-tuple ratio thresholds. Can a model judge when vacuum pays off?
- Concurrency control: lock waits and deadlock detection. Can a model predict hotspots?
- Query rewrite: view rewrites, subquery flattening — these rule-based optimizations. Can a model just output the rewritten SQL?
The line to watch over the next year or two isn't planner specifically — it's whether the "traditional heuristic components replaced by specialized small models" pattern spreads to other database subsystems. If it does, database kernel architecture gets rewritten. CBO, heuristic-based optimizers, the whole stack — maybe they get swapped out one by one, the way MapReduce got replaced by Spark.
What I can't judge: QORL's training data scale and real generalization capability. 81% is a benchmark number. Production environments have messier data distributions, more diverse query mixes, and concurrency load that benchmarks don't capture. The real test is whether someone dares run it on production secondary queries — read-only, on a replica, tolerating occasional rollback — for a month and then we see.
If QORL or something like it runs on a mid-sized internet company's production environment for three months without breaking, then it's real.
Source: rohanbansal.com/qorl