AI for Systems: Using LLMs to Optimize Database Query Execution

· Source: Together AI | The AI Native Cloud - Together.ai · Field: Technology & Digital — Artificial Intelligence & Machine Learning, Software Development & Engineering · Depth: Expert, medium

Summary

A collaboration involving Stanford University, the University of Wisconsin–Madison, and Bauplan demonstrates that LLM-guided plan rewrites can significantly optimize database query execution without modifying the database engine. Traditional cost-based optimizers often struggle with attribute independence assumptions, leading to cardinality misestimates and suboptimal physical plans. The team introduced DBPlanBench, a harness for Apache DataFusion, exposing the physical operator graph to an LLM. This system employs a token-efficient JSON schema, reducing plan payload size by approximately 10x. The LLM generates targeted JSON Patches (RFC 6902) for localized edits like swapping join sides. This method achieved a 4.78x speedup in a TPC-DS derived query, cutting hash-table build time from 10.16 seconds to 0.41 seconds and memory from 3.3 GB to 411 MB. Median speedups on TPC-H and TPC-DS workloads were 1.1x to 1.2x, with 60.8% of queries optimized over 5%. An "optimize small, deploy large" workflow was validated, transferring SF3 optimizations to SF10 databases.

Key takeaway

For AI Engineers or Data Engineers optimizing database performance, consider integrating LLM-guided query plan optimization. This approach significantly improves query latency and resource consumption, especially for complex OLAP workloads where traditional optimizers struggle with semantic correlations. You should explore using JSON Patches for safe, localized plan modifications. Also, adopt an "optimize small, deploy large" strategy to efficiently transfer optimizations from development to production environments. This can yield substantial speedups and reduce manual tuning efforts.

Key insights

LLMs can semantically optimize database query plans by correcting cardinality misestimates without engine modification.

Principles

Method

DBPlanBench serializes physical plans into a token-efficient JSON schema, then an LLM generates JSON Patches (RFC 6902) for targeted edits, iteratively refining plans.

In practice

Topics

Best for: Research Scientist, AI Scientist, AI Engineer, Data Engineer

Related on AIssential

Open in AIssential →

Editorial summary, takeaway, and curation by AIssential. Original article published by Together AI | The AI Native Cloud - Together.ai.