Analyze and optimize SQL queries for performance, correctness and readability.
| Variable | Description |
|---|---|
| {{database_engine}} | Database system: PostgreSQL, MySQL, SQLite, SQL Server, Oracle |
| {{table_sizes}} | Approximate row counts for the tables in the query (e.g. "users: 5M, orders: 50M") |
| {{current_execution_time}} | How long the query currently takes (e.g. "3.2 seconds") |
| {{optimization_goal}} | Primary goal: reduce latency, reduce I/O, reduce CPU, improve readability |
ppm install sql-optimizer
# Raw prompt text
GET https://registry.pascalai.org/v1/packages/sql-optimizer/1.0.0/raw
# Rendered with variables
GET https://registry.pascalai.org/v1/packages/sql-optimizer/1.0.0/render?database_engine=PostgreSQL&table_sizes=users%3A2M+orders%3A20M¤t_execution_time=4.5+seconds&optimization_goal=reduce+latency
uses promptlib;
var
Prompt := Get('sql-optimizer');
Rendered := Bind(Prompt, [
'database_engine', 'PostgreSQL 16',
'table_sizes', 'users: 2M rows, orders: 20M rows, products: 50K rows',
'current_execution_time', '4.5 seconds',
'optimization_goal', 'reduce latency for dashboard queries'
]);
Report := LLM.Complete(Rendered);
Structural breakdown of joins, subqueries, aggregations, and filter predicates.
Full-table scans, missing indexes, implicit type casts, inefficient join order, unnecessary DISTINCT or ORDER BY.
Exact CREATE INDEX statements with reasoning for each covering index, composite index, or partial index.
Fully rewritten SQL with inline comments explaining every transformation applied.
Partitioning strategies, materialized views, denormalization opportunities, or column type changes.
Estimated reduction in execution time, I/O reads, and memory usage relative to the current query.
Engine-specific features such as query hints, planner settings, extensions, or EXPLAIN plan guidance.