AI for Systems: Using LLMs to Optimize Database Query Execution
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.
Related posts
- Quail: Speeding up AI-SQL by jointly optimizing query planner and inference engine
Modal hardMaster
Quail is a query-aware inference layer that jointly optimizes AI-SQL query planning and LLM inference. AI-SQL functions such as AI.IF and AI_JOIN produce large, structured batches of prefill-only requests that each need only a single output token. Because the planner sees the whole query plan up front, it can order requests to maximize KV-cache reuse and evict entries safely, using a revised Hydragen-style cascade attention and KV-aware Pareto join-order search with speed-of-light cost estimates. It also skips decode-specific machinery like sampling, CUDA graphs and speculative decoding. On one multi-join query Quail exceeds a billion tokens per minute per H100, over 10x a vLLM baseline at under 6¢ per billion tokens, and it averages 1.84x faster than vLLM on a new AI-SQL benchmark.
- What really matters for performance: lessons from a year of benchmarks
ClickHouse easyNovice
Six ClickHouse benchmarks from 2025 point to one conclusion: performance depends on how efficiently an engine moves data end-to-end. JSONBench showed columnar JSON storage running orders of magnitude faster than document stores like MongoDB on up to 1B Bluesky events. FastFormats found the Native format, LZ4 compression and large batches matter most for ingestion, and Netflix used those results to reach ~5 PB/day of log ingestion. A distributed cache matched or beat local SSDs on object storage, lightweight SQL UPDATEs ran up to 1,000× faster than mutations, and parallel replicas aggregated 100B rows in ~0.4s.
- Text2SQL using Hugging Face Dataset Viewer API and Motherduck DuckDB-NSQL-7B
Hugging Face mediumAdeptDuckDB-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. The pipeline fetches a dataset's auto-converted parquet URL from the Dataset Viewer API and derives a CREATE TABLE schema by loading one row into DuckDB. It then prompts the quantized GGUF model through llama.cpp with the schema and question, rewrites the table name to the parquet URL, and executes the query in DuckDB. On the world-cities-geo dataset the generated query matches the results of the Viewer's search and filter APIs.
- AI doesn’t always generate perfect ClickHouse schemas (yet)
ClickHouse mediumAdept
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. Five recurring pitfalls are covered: partitioning for query speed instead of data lifecycle, OPTIMIZE TABLE ... FINAL creating irreversible oversized parts that break later mutations, materialized view sprawl slowing ingestion, JSON columns misconfigured via max_dynamic_paths, and projections adding 1-2 seconds of planning latency at 5-10+ TB. The recommendation is to start with a plain MergeTree, a well-chosen ORDER BY, default compression and nothing else, then measure the real workload and add complexity only where data justifies it.
- Using Dictionaries to Accelerate Queries
ClickHouse mediumAdept
ClickHouse dictionaries, in-memory key-value structures, can replace JOINs against small lookup tables. Using the NOAA weather dataset, a normalized INNER JOIN against a stations table forces a full scan of 1.08 billion rows because ClickHouse runs joins before WHERE filtering. Swapping in dictGet lookups alongside a primary-key-friendly subquery brings that query back to millisecond latency. The post walks through creating a dictionary, choosing a layout (flat, hashed, hashed_array, complex_key variants, sparse), setting LIFETIME and invalidate_query for refreshes, and loading data, recommending hashed_array or complex_key_hashed_array for most cases.