Query Optimization Agent

EXPLAIN-driven analysis: the agent runs the dialect-specific plan command, parses it, and feeds the plan plus schema to the model for rewrite candidates and missing-index DDL. Grounded in the actual plan, not the query text alone.

Per-dialect EXPLAIN

Output

{
  diagnosis: 'what the plan tells us',
  bottleneck: 'the most expensive thing in the plan',
  rewrites: ['alternative SQL'...],
  indexSuggestions: [
    { table, columns, reason, ddl }
  ],
  estimatedImpact: 'low' | 'medium' | 'high'
}

The agent is biased to prefer rewrites over indexes when the cardinality estimates implicate plan shape rather than a missing access path.