analyzing-query-performance
Tools and guidance for analyzing and optimizing slow database queries using execution plans and performance metrics.
Install
mkdir -p .claude/skills/analyzing-query-performance && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/4112" && unzip -o skill.zip -d .claude/skills/analyzing-query-performance && rm skill.zipInstalls to .claude/skills/analyzing-query-performance
Activation
This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.
Execute use when you need to work with query optimization.Key capabilities
- →Analyze slow database queries using execution plans
- →Identify sequential scans on large tables
- →Detect missing indexes and measure buffer cache hit ratios
- →Produce actionable optimization recommendations
- →Generate CREATE INDEX statements and query rewrite suggestions
- →Prioritize recommendations by impact-to-effort ratio
How it works
The skill captures EXPLAIN output from PostgreSQL, MySQL, or MongoDB, then analyzes it for red flags like sequential scans or high `rows_removed_by_filter`. It also checks buffer cache performance and index usage to generate specific optimization recommendations.
Inputs & outputs
When to use analyzing-query-performance
- →Identify slow database queries
- →Analyze query execution plans
- →Improve database performance
- →Detect missing indexes
About this skill
Query Performance Analyzer
Overview
Analyze slow database queries using execution plans, wait statistics, and I/O metrics across PostgreSQL, MySQL, and MongoDB. This skill captures EXPLAIN output, identifies sequential scans on large tables, detects missing indexes, measures buffer cache hit ratios, and produces actionable optimization recommendations ranked by expected performance impact.
Prerequisites
- Database credentials with permissions to run
EXPLAIN ANALYZE(PostgreSQL),EXPLAIN FORMAT=JSON(MySQL), orexplain()(MongoDB) pg_stat_statementsextension enabled for PostgreSQL (provides aggregated query statistics)- Access to slow query logs or performance_schema (MySQL)
- Baseline query execution times for comparison
psql,mysql, ormongoshCLI tools installed
Instructions
-
Identify the slowest queries by examining
pg_stat_statements(PostgreSQL):SELECT query, calls, mean_exec_time, total_exec_time FROM pg_stat_statements ORDER BY mean_exec_time DESC LIMIT 20. For MySQL, enable and query the slow query log orperformance_schema.events_statements_summary_by_digest. -
Run
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)on each slow query in PostgreSQL, orEXPLAIN ANALYZE FORMAT=JSONin MySQL. Capture the full execution plan including actual row counts, loop iterations, and buffer usage. -
Analyze the execution plan for these red flags:
- Sequential scans on tables with >10,000 rows (indicates missing index)
- Nested loop joins with high outer row counts (consider hash join or merge join)
- Sort operations without index support (adding a covering index eliminates the sort)
- High
rows_removed_by_filterrelative torows(predicate not selective enough) - Bitmap heap scans with high recheck rate (index selectivity too low)
-
Check buffer cache performance:
SELECT heap_blks_read, heap_blks_hit, heap_blks_hit::float / (heap_blks_hit + heap_blks_read) AS cache_hit_ratio FROM pg_statio_user_tables WHERE relname = 'table_name'. A ratio below 0.95 suggests the working set exceeds available shared_buffers. -
Evaluate index usage with
SELECT indexrelname, idx_scan, idx_tup_read, idx_tup_fetch FROM pg_stat_user_indexes WHERE schemaname = 'public' ORDER BY idx_scan ASC. Indexes with zero scans are unused and waste write performance. -
Check for table bloat using
SELECT relname, n_live_tup, n_dead_tup, n_dead_tup::float / GREATEST(n_live_tup, 1) AS dead_ratio FROM pg_stat_user_tables WHERE n_dead_tup > 1000 ORDER BY dead_ratio DESC. A dead tuple ratio above 0.2 indicates the table needs VACUUM. -
For each identified issue, generate a specific recommendation: CREATE INDEX statement with the exact columns, query rewrite suggestions, or configuration parameter adjustments.
-
Estimate the performance impact of each recommendation by comparing the EXPLAIN plan before and after applying the change on a staging database or by analyzing the expected row reduction from new indexes.
-
Prioritize recommendations by impact-to-effort ratio: index additions (high impact, low effort) before query rewrites (medium impact, medium effort) before schema changes (high impact, high effort).
-
Generate a performance analysis report with before/after execution plans, estimated improvements, and implementation priority ranking.
Output
- Slow query inventory with execution frequency, mean/P95 duration, and total time consumed
- Annotated execution plans highlighting sequential scans, sort bottlenecks, and join inefficiencies
- Index recommendations as ready-to-execute CREATE INDEX statements with expected impact
- Query rewrite suggestions with original and optimized SQL side by side
- Buffer cache analysis with shared_buffers sizing recommendations
- Performance report ranking all findings by severity and implementation priority
Error Handling
| Error | Cause | Solution |
|---|---|---|
EXPLAIN ANALYZE takes too long on production | Query modifies data or runs for minutes | Use EXPLAIN without ANALYZE for estimated plans; run EXPLAIN ANALYZE on staging with representative data |
pg_stat_statements not available | Extension not installed or not in shared_preload_libraries | Run CREATE EXTENSION pg_stat_statements; add to shared_preload_libraries in postgresql.conf and restart |
| Execution plan differs between staging and production | Different data distribution, statistics, or configuration | Run ANALYZE on staging tables to update statistics; match work_mem, random_page_cost, and effective_cache_size settings |
| Index recommendation causes slow writes | Too many indexes on a write-heavy table | Limit indexes to 5-7 per table; use partial indexes to reduce scope; consider covering indexes to replace multiple single-column indexes |
| Query plan uses wrong index | Stale statistics or cost model miscalculation | Run ANALYZE table_name to refresh statistics; adjust random_page_cost for SSD storage; use SET enable_seqscan = off to test index plans |
Examples
Optimizing a dashboard aggregate query: A query computing daily revenue with GROUP BY date and JOIN across orders and line_items takes 12 seconds. EXPLAIN reveals a sequential scan on line_items (5M rows). Adding a composite index on (order_id, created_at) with INCLUDE (amount) reduces execution to 200ms by enabling an index-only scan.
Diagnosing N+1 query pattern: Application loads a list page showing 50 products, each with a separate query for category name. pg_stat_statements reveals SELECT name FROM categories WHERE id = $1 called 50 times per page load. Resolution: rewrite as a single JOIN query or implement eager loading in the ORM.
Identifying bloated table causing cache misses: Buffer cache hit ratio drops to 0.78 on the sessions table. Investigation reveals 80% dead tuples due to aggressive INSERT/DELETE cycling without autovacuum tuning. Setting autovacuum_vacuum_scale_factor = 0.01 and running VACUUM FULL restores cache hit ratio to 0.99.
Resources
- PostgreSQL EXPLAIN documentation: https://www.postgresql.org/docs/current/using-explain.html
- pg_stat_statements reference: https://www.postgresql.org/docs/current/pgstatstatements.html
- MySQL EXPLAIN output format: https://dev.mysql.com/doc/refman/8.0/en/explain-output.html
- Use The Index, Luke (SQL indexing guide): https://use-the-index-luke.com/
- pgMustard EXPLAIN visualizer: https://www.pgmustard.com/
When not to use it
- →When `EXPLAIN ANALYZE` takes too long on a production database
- →When `pg_stat_statements` is not available for PostgreSQL
- →When execution plans differ between staging and production environments
Prerequisites
Limitations
- →Index recommendations may cause slow writes if too many indexes are added to a write-heavy table
- →Query plans may use the wrong index due to stale statistics or cost model miscalculation
How it compares
This skill automates the analysis of database execution plans and metrics across multiple database systems, providing ranked optimization recommendations, unlike manual inspection of raw EXPLAIN output.
Compared to similar skills
analyzing-query-performance side by side with the closest alternatives in the catalog.
| Skill | Installs | Updated | Safety | Difficulty |
|---|---|---|---|---|
| analyzing-query-performance (this skill) | 1 | 27d | Review | Intermediate |
| databases | 1 | 9mo | Review | Intermediate |
| database-optimizer | 1 | 4mo | No flags | Advanced |
| database-design | 6 | 6mo | Review | Intermediate |
Try saying
Example prompts that trigger this skill in your AI assistant.
More by jeremylongshore
View all by jeremylongshore →You might also like
databases
mrgoonie
Work with MongoDB (document database, BSON documents, aggregation pipelines, Atlas cloud) and PostgreSQL (relational database, SQL queries, psql CLI, pgAdmin). Use when designing database schemas, writing queries and aggregations, optimizing indexes for performance, performing database migrations, configuring replication and sharding, implementing backup and restore strategies, managing database users and permissions, analyzing query performance, or administering production databases.
database-optimizer
sickn33
Expert database optimizer specializing in modern performance tuning, query optimization, and scalable architectures. Masters advanced indexing, N+1 resolution, multi-tier caching, partitioning strategies, and cloud database optimization. Handles complex query analysis, migration strategies, and performance monitoring. Use PROACTIVELY for database optimization, performance issues, or scalability challenges.
database-design
davila7
Database design principles and decision-making. Schema design, indexing strategy, ORM selection, serverless databases.
vector-database-engineer
sickn33
Expert in vector databases, embedding strategies, and semantic search implementation. Masters Pinecone, Weaviate, Qdrant, Milvus, and pgvector for RAG applications, recommendation systems, and similar
generating-database-seed-data
jeremylongshore
Process this skill enables AI assistant to generate realistic test data and database seed scripts for development and testing environments. it uses faker libraries to create realistic data, maintains relational integrity, and allows configurable data volumes. u... Use when working with databases or data models. Trigger with phrases like 'database', 'query', or 'schema'.
database-schema-designer
davila7
Design robust, scalable database schemas for SQL and NoSQL databases. Provides normalization guidelines, indexing strategies, migration patterns, constraint design, and performance optimization. Ensures data integrity, query performance, and maintainable data models.