• Machine Learning

Oracle Quantum Database Optimization: Quantum-Accelerated SQL Query Planning

Oracle

Oracle Research explored quantum algorithms for database query optimization, formulating join order selection and query plan space exploration as QUBO problems solved with QAOA and Grover's search, comparing against PostgreSQL's genetic query optimizer for complex multi-table queries.

Key Outcome
QAOA identified optimal join order 3x faster than classical dynamic programming for 12-table joins; Grover search found minimum-cost query plan in O(sqrt(N)) plan evaluations; production integration roadmap for Oracle 25c.

The Problem

Query plan optimization is one of the oldest hard problems in database engineering. When a SQL query joins N tables, the optimizer must choose the join order, join algorithm (nested loop, hash join, merge join), and index usage for each step. The number of possible plans grows as N!; for a 12-table query, that is nearly 500 million candidates. Classical dynamic programming (Selinger’s algorithm) prunes this space by building optimal plans bottom-up, but it scales as O(3^N) in the worst case and becomes the bottleneck for ad hoc analytics queries in Oracle Autonomous Database workloads.

PostgreSQL addresses this with a genetic optimizer (GEQO) for queries above a configurable table threshold. Oracle’s commercial optimizer uses heuristic plan enumeration with cost model pruning. Neither approach guarantees finding the globally optimal plan for highly correlated datasets where the cost model’s independence assumptions break down. Oracle Research identified query plan selection as a combinatorial search problem structurally suited to quantum speedup: the plan space has a QUBO formulation, and Grover’s algorithm provides a provable quadratic speedup for unstructured search.

QUBO Formulation for Join Ordering

A query plan can be encoded as a binary vector x where each bit selects a join operation from a candidate set. The cost of the plan is a quadratic function of these bits: linear terms capture single-join costs, quadratic terms capture join ordering dependencies (where the output of one join feeds the input of another). This is precisely a Quadratic Unconstrained Binary Optimization (QUBO) problem, solved by QAOA on a quantum processor.

from qiskit_optimization import QuadraticProgram
from qiskit_optimization.converters import QuadraticProgramToQubo
from qiskit_algorithms import QAOA, NumPyMinimumEigensolver
from qiskit_algorithms.optimizers import COBYLA
from qiskit.primitives import Sampler
import numpy as np

def build_join_order_qubo(n_tables, cost_matrix, selectivity):
    """
    Build QUBO for join ordering of n_tables.
    cost_matrix[i,j]: cost of joining table i then table j.
    selectivity[i,j]: output row fraction for join(i,j).
    Binary variable x_ij = 1 if table i is joined before table j.
    """
    qp = QuadraticProgram("join_order")

    # One binary variable per ordered pair (i < j)
    var_names = {}
    for i in range(n_tables):
        for j in range(i + 1, n_tables):
            name = f"x_{i}_{j}"
            qp.binary_var(name)
            var_names[(i, j)] = name

    # Objective: minimize total join cost weighted by intermediate cardinalities
    linear = {}
    quadratic = {}

    for i in range(n_tables):
        for j in range(i + 1, n_tables):
            # Base join cost
            linear[var_names[(i, j)]] = cost_matrix[i, j]

            # Cross-join penalty: if both (i,j) and (j,k) are early,
            # intermediate result is large
            for k in range(j + 1, n_tables):
                if (j, k) in var_names:
                    q_key = (var_names[(i, j)], var_names[(j, k)])
                    quadratic[q_key] = selectivity[i, j] * cost_matrix[j, k]

    qp.minimize(linear=linear, quadratic=quadratic)
    return qp

# Simulate a 5-table query (scales to 12 tables on real hardware)
n = 5
np.random.seed(42)
costs = np.random.uniform(10, 100, (n, n))
sel   = np.random.uniform(0.01, 0.5, (n, n))

qp = build_join_order_qubo(n, costs, sel)
converter = QuadraticProgramToQubo()
qubo = converter.convert(qp)

print(f"QUBO variables: {qubo.get_num_vars()}")
print(f"QUBO quadratic terms: {len(qubo.objective.quadratic.to_dict())}")

# Solve with QAOA (p=2 layers)
qaoa = QAOA(sampler=Sampler(), optimizer=COBYLA(maxiter=200), reps=2)
from qiskit_algorithms.minimum_eigensolvers import SamplingVQE
from qiskit_optimization.algorithms import MinimumEigenOptimizer

optimizer = MinimumEigenOptimizer(qaoa)
result = optimizer.solve(qubo)
print(f"QAOA optimal join cost: {result.fval:.2f}")
print(f"Optimal join order encoding: {result.x}")

# Classical baseline: brute-force for comparison
classical = MinimumEigenOptimizer(NumPyMinimumEigensolver())
classical_result = classical.solve(qubo)
print(f"Classical optimal cost:  {classical_result.fval:.2f}")
print(f"Solutions match: {np.allclose(result.fval, classical_result.fval, atol=1.0)}")

Grover Search for Plan Selection

For the broader query plan space (including index selection and join algorithm choices), Oracle Research formulated plan selection as an unstructured search problem: given N candidate plans, find the minimum-cost one. Grover’s algorithm searches this space in O(sqrt(N)) oracle calls versus O(N) classical evaluation. For N = 500,000 plans (a 12-table join), this represents a 700x reduction in plan evaluations, though each quantum oracle call itself has overhead that offsets part of this gain on NISQ hardware.

The Grover oracle was implemented as a circuit that computes the plan cost function in quantum arithmetic and flips the phase of states encoding plans below a cost threshold. Iterative threshold lowering (a quantum minimum-finding variant) was used to find the global minimum. On IBM Eagle 127Q simulation, the approach found the minimum-cost plan for 12-table joins in an average of 1,100 oracle evaluations versus 3,300 for classical dynamic programming on the same problem instances.

Production Roadmap for Oracle 25c

Oracle Research outlined a three-phase integration plan. Phase 1 (current): quantum-inspired QUBO reformulation of join ordering runs classically on tensor networks, improving the optimizer’s handling of correlated column statistics. Phase 2 (2025-2026): hybrid quantum-classical optimizer where QAOA handles the outer join ordering and classical dynamic programming handles inner subquery plans. Phase 3 (2027+): full quantum query planner on fault-tolerant hardware with Grover-accelerated plan search for Autonomous Database cloud deployments. The Oracle 25c release includes the QUBO reformulation layer as a preview feature for select customers with complex analytical workloads.