Explain / Optimize / Review
The ai package includes static advisors that work on QueryPlans — no model
call required for explain/optimize/review.
explainPlan
Section titled “explainPlan”Returns a human-readable description of a plan.
import { explainPlan } from "@mountsqli/ai";
const info = explainPlan(plan);// info.summary: "Selects from users where age > 18, ordered by email"optimizePlan
Section titled “optimizePlan”Suggests improvements (e.g. missing indexes, full scans).
import { optimizePlan } from "@mountsqli/ai";
const suggestions = optimizePlan(plan);// [{ type: "index", table: "users", columns: ["email"] }]reviewPlans
Section titled “reviewPlans”Audits a set of plans for security/perf issues.
import { reviewPlans } from "@mountsqli/ai";
const findings = reviewPlans([planA, planB]);// [{ severity: "warn", message: "...", plan: "users" }]The Ai class
Section titled “The Ai class”Ai bundles them:
const ai = new Ai({ provider: myProvider });ai.explain(plan);ai.optimize(plan);ai.review([planA, planB]);Best practices
Section titled “Best practices”- Run
reviewPlansin CI on generated queries. - Use
optimizePlanhints to driveanalyzeindex suggestions. - Explain plans to non-SQL teammates for review.
Common mistakes
Section titled “Common mistakes”- Treating
optimizePlansuggestions as mandatory — weigh them against load. - Reviewing only hand-written plans and skipping AI-generated ones.
Related
Section titled “Related”- Natural Language → SQL — generate plans to review.
- CLI → analyze — runtime index suggestions.
