---
title: "AI for Systems: Using LLMs to Optimize Database Query Execution"
description: "LLMs can act as semantic cardinality estimators that fix join-order mistakes in database physical plans without modifying the engine."
url: "https://swe.quest/articles/ai-for-systems-using-llms-to-optimize-database-query-execution-4407ac9f4162928a"
canonical: "https://www.together.ai/blog/using-llms-to-optimize-database-query-execution"
source: "Together AI"
published: "2026-04-03"
difficulty: "hard"
kind: "Research"
reading_minutes: 6
tags: ["query-engines","llms","performance","databases"]
---

# AI for Systems: Using LLMs to Optimize Database Query Execution

A post on [Together AI](https://swe.quest/sources/together.md), published 2026-04-03, 6 min read, Research. Original: [https://www.together.ai/blog/using-llms-to-optimize-database-query-execution](https://www.together.ai/blog/using-llms-to-optimize-database-query-execution)

## Summary

LLMs can act as semantic cardinality estimators that fix join-order mistakes in database physical plans without modifying the engine. DBPlanBench exposes Apache DataFusion's physical operator graph to an LLM through a compact JSON serialization roughly 10x smaller than the native form. GPT-5 proposes localized RFC 6902 JSON Patch edits, such as swapping join sides, and an evolutionary loop keeps only the patches that cut latency. On generated TPC-H and TPC-DS workloads, median speedups were 1.1–1.2x, with up to 4.78x and a drop from 3.3 GB to 411 MB of hash-build memory on one multi-join query. Plans optimized on a small SF3 database transferred to SF10 with similar gains.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Query engines](https://swe.quest/tags/query-engines.md) (Data & databases)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Databases](https://swe.quest/tags/databases.md) (Data & databases)

## Prerequisites

- query execution plans
- cardinality estimation
- join ordering
- LLM prompting ([easy posts on LLMs](https://swe.quest/tags/llms.md?difficulty=easy))

## Keywords

DBPlanBench, Apache DataFusion, JSON Patch, TPC-DS, TPC-H, GPT-5

## Related posts

- [Quail: Speeding up AI-SQL by jointly optimizing query planner and inference engine](https://swe.quest/articles/quail-speeding-up-ai-sql-by-jointly-optimizing-query-planner-and-inference-515cdcca6399e688.md): Modal · 2026-09-24 · Hard · 18 min read · Inference & serving, Query engines, LLMs, Performance engineering, SQL
  Quail is a query-aware inference layer that jointly optimizes AI-SQL query planning and LLM inference.
- [What really matters for performance: lessons from a year of benchmarks](https://swe.quest/articles/what-really-matters-for-performance-lessons-from-a-year-of-benchmarks-0ec8700c872ad4a4.md): ClickHouse · 2025-12-22 · Easy · 7 min read · Analytics & OLAP, Performance engineering, Caching, Databases
  Six ClickHouse benchmarks from 2025 point to one conclusion: performance depends on how efficiently an engine moves data end-to-end.
- [Text2SQL using Hugging Face Dataset Viewer API and Motherduck DuckDB-NSQL-7B](https://swe.quest/articles/text2sql-using-hugging-face-dataset-viewer-api-and-motherduck-duckdb-nsql-7b-7024872a0cc79ab7.md): Hugging Face · 2024-04-04 · Medium · 7 min read · LLMs, SQL, Analytics & OLAP, Python
  DuckDB-NSQL-7B, a Llama-2-7B model fine-tuned on general SQL and then DuckDB text-to-SQL pairs, can turn plain-language questions into DuckDB SQL that runs directly against Hugging Face datasets.
- [AI doesn’t always generate perfect ClickHouse schemas (yet)](https://swe.quest/articles/ai-doesn-t-always-generate-perfect-clickhouse-schemas-yet-fd8dcf025b9d0fa8.md): ClickHouse · 2026-03-13 · Medium · 10 min read · Analytics & OLAP, Databases, LLMs, Performance engineering
  LLM-generated ClickHouse schemas tend to over-optimize from day one with custom partitioning, per-column codecs, projections and materialized views that can hurt at production scale.
- [Using Dictionaries to Accelerate Queries](https://swe.quest/articles/using-dictionaries-to-accelerate-queries-fcf5f4fb14453a0d.md): ClickHouse · 2022-12-07 · Medium · 17 min read · Query engines, Analytics & OLAP, SQL, Performance engineering
  ClickHouse dictionaries, in-memory key-value structures, can replace JOINs against small lookup tables.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
