optimizing-sql

Diagnose and optimize slow SQL queries through execution plan analysis, indexing strategies, and query rewriting.

1|Updated Feb 24, 2026
One-click install
npx skills add https://github.com/masermediagroup-stack/maser-media --skill optimizing-sql-masermediagroup-stack
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: optimizing-sql
Source: https://github.com/masermediagroup-stack/maser-media/tree/main/.cursor/skills/community/ai-design-components/skills/optimizing-sql
Command: npx skills add https://github.com/masermediagroup-stack/maser-media --skill optimizing-sql-masermediagroup-stack

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow database queries and missing indexes degrade application performance, and diagnosing them requires deep knowledge of execution plans, scan types, and index design across different database engines. ## Core Features & Use Cases - Execution Plan Analysis: Interpret EXPLAIN and EXPLAIN ANALYZE output for PostgreSQL, MySQL, and SQL Server to identify sequential scans, costly sorts, and inefficient joins. - Index Strategy Design: Apply decision frameworks for single-column, composite, covering, partial, and expression indexes with correct column ordering. - Query Rewriting: Fix anti-patterns such as N+1 queries, non-sargable conditions, correlated subqueries, and SELECT * over-fetching. - Use Case: An API endpoint takes 2 seconds to load. Run EXPLAIN ANALYZE, find a sequential scan filtering 99,990 rows, add a composite index on (customer_id, created_at DESC), and reduce the query to 10ms. ## Quick Start Analyze this slow PostgreSQL query with EXPLAIN ANALYZE and recommend indexes and a rewritten version to improve its performance.

Frequently Asked Questions about optimizing-sql

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize a slow SQL query?▼

Start by running EXPLAIN ANALYZE to identify bottlenecks like sequential scans or expensive sorts. Then add indexes on filtered or joined columns, rewrite non-sargable conditions, and verify improvement by comparing before-and-after execution plans.

How do I read a PostgreSQL EXPLAIN ANALYZE output?▼

Focus on the scan type, cost range, estimated versus actual rows, and execution time. Sequential scans with high 'Rows Removed by Filter' indicate missing indexes, while large row estimate discrepancies suggest outdated table statistics.

What column order should a composite index use?▼

Place equality filter columns first, ordered by selectivity, then range filter columns, and finally ORDER BY columns matching the sort direction. A composite index on (A, B, C) only helps queries that filter on the leading columns.

Does this cover MySQL and SQL Server or only PostgreSQL?▼

It covers PostgreSQL, MySQL, SQL Server, and Snowflake. Each database has dedicated guidance for its EXPLAIN format, index types such as GIN, full-text, clustered, and columnstore, and engine-specific features like Query Store or index hints.

Why is my index not being used by the query planner?▼

Common causes include functions applied to the indexed column, implicit type conversion from mismatched data types, leading wildcards in LIKE patterns, and low column selectivity. Rewrite the condition into a sargable form or create an expression index.

When should I not add an index?▼

Skip indexes on small tables under roughly 1,000 rows, low-selectivity columns like booleans, and write-heavy tables where each index adds 5-10% overhead to inserts and updates. Also avoid redundant indexes that overlap with existing composite indexes.