Files
supabase/apps/studio/components/interfaces/ExplainVisualizer/ExplainVisualizer.ai.ts
Ali Waseem 89e0fe4f28 feature: Explain tab in SQL editor that shows output of explain analyze (#41569)
* wip: explain tab in results editor

* updated to add sql explain

* updated to default back to results

* updated explain function

* updated case with multiple statements

* updated to reset explain query results

* added tests for semi colon comments

* feature: add explain w/ AI on pretty-explain tab (#41588)

* wip: added explain with AI

* wip: updated header with new buttons

* updated prompt

* remove any types

* removed unused flag

* updated header

* formatted code
2025-12-24 10:14:53 -07:00

46 lines
1.1 KiB
TypeScript

import type { QueryPlanRow } from './ExplainVisualizer.types'
export interface ExplainPromptInput {
sql: string
explainPlanRows: QueryPlanRow[]
}
export interface ExplainPromptOutput {
query: string
prompt: string
}
export function buildExplainPrompt({
sql,
explainPlanRows,
}: ExplainPromptInput): ExplainPromptOutput {
const explainPlan = explainPlanRows.map((row) => row['QUERY PLAN']).join('\n')
const prompt = `Explain this PostgreSQL EXPLAIN ANALYZE output in simple terms:
\`\`\`sql
${sql}
\`\`\`
\`\`\`
${explainPlan}
\`\`\`
Format your response with:
**What it does** - 1-2 sentences.
**How it runs** - Briefly explain the plan from bottom to top in plain English. Mention key operations (scans, joins, sorts) and why PostgreSQL chose them.
**Issues** - Identify bottlenecks: slowest steps, sequential scans on large tables, inefficient joins, missing indexes. Be specific with timings from the plan.
**Fixes** - 2-3 specific suggestions with CREATE INDEX statements if applicable.
Keep it concise. Focus on actionable insights.`
return {
query: sql,
prompt,
}
}