Skip to content

Deep RL Query Optimizer

QueryOptimizerSelf-Tuning
Beta Works · surface still evolving · works, surface still evolving

Traditional cost-based optimisers compute a “cost” for each candidate plan from histograms and a fixed formula. The formula is approximate, the histograms drift, and the optimiser keeps picking the same wrong shape until someone files a ticket.

AetheriusDB pairs a deterministic rule-based optimiser (for correctness and warm-up) with a reinforcement-learning agent that learns from observed execution. Each time a query runs, the agent logs the plan shape and the measured latency as a reward. Over time it learns things like “this join order is fast when the tenant has fewer than 100 rows on the left, but slow otherwise” — knowledge no single statistic could encode.

The RL agent improves plans the cost-based system gets persistently wrong — especially for skewed distributions, correlated predicates, or shifting tenant cardinalities. It also gives you a built-in feedback loop: queries you run often tend to get faster on their own.

  • Learns from actual latency — the reward is wall-clock time, not an estimate.
  • Safe by construction — the rule-based fallback always works, even before the model is trained.
  • Self-tuning — repeated queries can improve over hours and days without DBA intervention.
flowchart LR
  classDef plan fill:#1f2640,stroke:#7c5cff,color:#e8eaf0
  classDef exec fill:#1f2640,stroke:#00d4ff,color:#e8eaf0
  classDef rl   fill:#1f2640,stroke:#4ade80,color:#4ade80

  Q[SQL]:::plan --> P[Plan candidates]:::plan
  P --> A[RL agent<br/>picks shape]:::rl
  A --> E[Compiled execution]:::exec
  E --> R[Measured latency<br/>= reward]:::rl
  R -.train.-> A
  E --> OUT[Result]:::exec

You can inspect what the optimiser picked, and pin the rule-based plan for a session when you need fully deterministic shapes.

-- See which plan the optimiser chose and why.
EXPLAIN (RL) SELECT o.id, c.name
FROM orders o
JOIN customers c ON c.id = o.customer_id
WHERE o.tenant_id = 42;
-- Use the rule-based plan only, for this session:
SET optimizer = 'rule_based';
-- Inspect the model's accumulated knowledge for a query shape:
SHOW PLAN HISTORY FOR 'orders_join_customers';

During the first few runs of an entirely new query shape, the agent explores — occasionally trying a non-greedy plan to learn its real cost. Once it has confidence, it sticks with the best-performing shape.

TermWhat it means
Reward signalReal measured latency from each execution, used to update the model.
Exploration vs exploitationThe agent occasionally tries an alternative plan to learn its cost, and exploits the best-known shape otherwise.
Rule-based fallbackA deterministic optimiser that always works and is used until the model has enough samples.
Plan historyThe per-shape log of plans tried and latencies observed, via SHOW PLAN HISTORY.