Now liveThe Skillselion MCP - thousands of ranked skills, loaded into your agent mid-task. No install.Get it →
aj-geddes avatar

Sql Query Optimization

  • 464 installs
  • 305 repo stars
  • Updated March 4, 2026
  • aj-geddes/useful-ai-prompts

sql-query-optimization is an agent skill that helps developers tune slow SQL for production apps by rewriting queries, adding indexes, fixing joins, and reducing latency on API or reporting endpoints backed by Postgres,

About

sql-query-optimization is a skill from aj-geddes/useful-ai-prompts focused on production database performance rather than tutorial SQL. It guides rewriting expensive queries, choosing indexes, correcting join plans, and cutting latency on API handlers and reporting paths that hit Postgres, MySQL, or comparable relational engines. Developers reach for sql-query-optimization when EXPLAIN plans show full scans, nested loops blow up, or p95 endpoint times trace back to specific statements. The skill fits iterative ops work after launch—when schema exists and the goal is measurable query speedups without redesigning the entire data model.

  • Index and join strategy guidance
  • Explain-plan driven diagnosis
  • Latency reduction for hot paths
  • Production-safe rewrite patterns
  • Works across common SQL engines

Sql Query Optimization by the numbers

  • 464 all-time installs (skills.sh)
  • Ranked #128 of 911 Databases skills by installs in the Skillselion catalog
  • Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill sql-query-optimization

Add your badge

Show developers this skill is listed on Skillselion. Paste this into your README.

Listed on Skillselion
Installs464
repo stars305
Last updatedMarch 4, 2026
Repositoryaj-geddes/useful-ai-prompts

How do you optimize slow SQL queries in production?

Tune slow SQL for production apps: rewrite queries, add indexes, fix joins, and cut latency on reporting or API endpoints backed by Postgres, MySQL, or similar engines.

Who is it for?

Developers debugging slow Postgres or MySQL queries affecting live API or reporting endpoints who need concrete rewrite and index guidance.

Skip if: Greenfield schema design or NoSQL-only data stores where relational index and join tuning patterns do not apply.

When should I use this skill?

Production SQL is slow, EXPLAIN shows bad plans, or API/reporting latency traces to specific relational queries on Postgres or MySQL.

What you get

Rewritten SQL statements, recommended indexes, improved join plans, and measurable latency reductions on targeted endpoints.

  • Optimized SQL rewrite
  • Index recommendations
  • Improved query execution plan

Files

SKILL.mdMarkdownGitHub ↗

SQL Query Optimization

Table of Contents

Overview

Analyze SQL queries to identify performance bottlenecks and implement optimization techniques. Includes query analysis, indexing strategies, and rewriting patterns for improved performance.

When to Use

  • Slow query analysis and tuning
  • Query rewriting and refactoring
  • Index utilization verification
  • Join optimization
  • Subquery optimization
  • Query plan analysis (EXPLAIN)
  • Performance baseline establishment

Quick Start

PostgreSQL:

-- Analyze query plan with execution time
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT u.id, u.email, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at > NOW() - INTERVAL '1 year'
GROUP BY u.id, u.email;

-- Check table statistics
SELECT * FROM pg_stats
WHERE tablename = 'users' AND attname = 'created_at';

Reference Guides

Detailed implementations in the references/ directory:

GuideContents
Analyze Current PerformanceAnalyze Current Performance
Common Optimization PatternsCommon Optimization Patterns
Query Rewriting TechniquesQuery Rewriting Techniques
Batch OperationsBatch Operations

Best Practices

✅ DO

  • Follow established patterns and conventions
  • Write clean, maintainable code
  • Add appropriate documentation
  • Test thoroughly before deploying

❌ DON'T

  • Skip testing or validation
  • Ignore error handling
  • Hard-code configuration values

Related skills

How it compares

Use sql-query-optimization for statement-level relational tuning; use broader observability skills when latency root cause is outside the database layer.

FAQ

Which databases does sql-query-optimization target?

sql-query-optimization targets production relational engines including Postgres and MySQL, focusing on rewriting queries, adding indexes, and fixing joins that slow API or reporting endpoints.

When should sql-query-optimization be invoked?

sql-query-optimization fits when specific SQL statements drive high latency in production—after identifying slow queries via logs or EXPLAIN—and the goal is targeted rewrites and indexes, not initial schema design.

Databasesdatabasesanalytics

This week in AI coding

Five minutes, every Monday - the tools, releases and tactics for developers.

unsubscribe anytime.