---
title: "Quail: Speeding up AI-SQL by jointly optimizing query planner and inference engine"
description: "Quail is a query-aware inference layer that jointly optimizes AI-SQL query planning and LLM inference."
url: "https://swe.quest/articles/quail-speeding-up-ai-sql-by-jointly-optimizing-query-planner-and-inference-515cdcca6399e688"
canonical: "https://modal.com/blog/quail-billion-tpm"
source: "Modal"
published: "2026-09-24"
difficulty: "hard"
kind: "Research"
reading_minutes: 18
tags: ["inference","query-engines","llms","performance","sql"]
---

# Quail: Speeding up AI-SQL by jointly optimizing query planner and inference engine

A post on [Modal](https://swe.quest/sources/modal.md), published 2026-09-24, 18 min read, Research. Original: [https://modal.com/blog/quail-billion-tpm](https://modal.com/blog/quail-billion-tpm)

## Summary

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.

Written by swe.quest, not by the author.

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

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [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)
- [SQL](https://swe.quest/tags/sql.md) (Languages)

## Prerequisites

- transformer KV caching ([easy posts on Caching](https://swe.quest/tags/caching.md?difficulty=easy))
- prefill vs decode in LLM inference ([easy posts on Inference & serving](https://swe.quest/tags/inference.md?difficulty=easy))
- query planning and join ordering
- GPU arithmetic intensity ([easy posts on Hardware](https://swe.quest/tags/hardware.md?difficulty=easy))

## Keywords

Quail, AI-SQL, vLLM, Hydragen, H100, sqlglot, Substrait, Apache Arrow

## Related posts

- [AI for Systems: Using LLMs to Optimize Database Query Execution](https://swe.quest/articles/ai-for-systems-using-llms-to-optimize-database-query-execution-4407ac9f4162928a.md): Together AI · 2026-04-03 · Hard · 6 min read · Query engines, LLMs, Performance engineering, Databases
  LLMs can act as semantic cardinality estimators that fix join-order mistakes in database physical plans without modifying the engine.
- [Accelerating Qwen3-8B Agent on Intel® Core™ Ultra with Depth-Pruned Draft Models](https://swe.quest/articles/accelerating-qwen3-8b-agent-on-intel-coretm-ultra-with-depth-pruned-draft-models-ea1317a126102827.md): Hugging Face · 2025-09-29 · Medium · 6 min read · Inference & serving, LLMs, AI agents, Performance engineering
  Speculative decoding with a Qwen3-0.6B draft model speeds up 4-bit Qwen3-8B on an Intel Lunar Lake integrated GPU by about 1.3× using OpenVINO.GenAI.
- [Assisted Generation: a new direction toward low-latency text generation](https://swe.quest/articles/assisted-generation-a-new-direction-toward-low-latency-text-generation-46c4fea13508392f.md): Hugging Face · 2023-05-11 · Medium · 12 min read · Inference & serving, LLMs, Performance engineering, Python
  Assisted generation cuts autoregressive LLM latency by letting a much smaller assistant model with the same tokenizer greedily draft candidate tokens, then verifying them all in a single forward pass of the large model.
- [Bypassing inference bottlenecks: Accelerating complex AI search with Retrieve-for-Train](https://swe.quest/articles/bypassing-inference-bottlenecks-accelerating-complex-ai-search-with-retrieve-95676e43f1ce9cc3.md): Google Research · 2026-09-15 · Hard · 8 min read · Search, Model training, Inference & serving, LLMs, Recommendations & ranking
  Retrieve-for-Train replaces expensive inference-time LLM reasoning for query fan-out with a one-time offline RL stage.
- [Continuous batching from first principles](https://swe.quest/articles/continuous-batching-from-first-principles-b2a5a3d9c00e56be.md): Hugging Face · 2025-11-25 · Medium · 17 min read · Inference & serving, LLMs, Performance engineering
  Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill.

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)
