Skip to content
Skills Library
Data & analytics · Building & coding

fai-sql-optimization-skill

/fai-sql-optimization-skill

Optimize SQL queries with execution plan analysis, index tuning, query rewriting, and statistics management. Use when diagnosing slow queries across PostgreSQL, SQL Server, or MySQL.

Repository
frootai ↗
Licence
MIT
Last repository change
26.10.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
3
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 26.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Optimize SQL queries with execution plan analysis, index tuning, query rewriting, and statistics management. Use when diagnosing slow queries across PostgreSQL, SQL Server, or MySQL.

Original author description

Optimize SQL queries with execution plan analysis, index tuning, query rewriting, and statistics management. Use when diagnosing slow queries across PostgreSQL, SQL Server, or MySQL.

How it works

  1. When to Use
  2. Execution Plan Analysis
  3. Key Plan Red Flags
  4. Index Optimization
  5. Query Rewriting
  6. Statistics Management
  7. Troubleshooting
Original SKILL.md headings · 98 lines, 406 words
SKILL.md preview
---
name: fai-sql-optimization-skill
description: |
  Optimize SQL queries with execution plan analysis, index tuning, query
  rewriting, and statistics management. Use when diagnosing slow queries
  across PostgreSQL, SQL Server, or MySQL.
---

# SQL Query Optimization

Diagnose and fix slow queries with execution plans, indexes, and rewrites.

## When to Use

- Queries exceeding latency SLOs
- High CPU or I/O from database operations
- Identifying missing or redundant indexes
- Tuning queries for AI application data access

---

## Execution Plan Analysis

```sql
-- PostgreSQL
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT) SELECT ...;

-- SQL Server
SET STATISTICS IO ON;
SET STATISTICS TIME ON;
-- Then run query, check Messages tab

-- MySQL
EXPLAIN FORMAT=TREE SELECT ...;
```

## Key Plan Red Flags

| Signal | Problem | Fix |
|--------|---------|-----|
| Seq Scan / Table Scan | Full table read | Add index on filter columns |
| Nested Loop (high rows) | N+1 join pattern | Consider Hash/Merge Join |
| Sort (external) | work_mem too low | Increase work_mem or add sorted index |
| Key Lookup | Non-covering index | Add INCLUDE columns |

## Index Optimization

```sql
-- Composite index for common query pattern
CREATE INDEX idx_conv_user_status ON conversations(user_id, status, created_at DESC);

-- Covering index (avoids key lookup)
CREATE INDEX idx_msg_conv_covering ON messages(conversation_id)
    INCLUDE (role, content, created_at);

-- Partial index (index only relevant rows)
CREATE INDEX idx_active_conv ON conversations(user_id)
    WHERE status = 'active';

-- Find unused indexes
SELECT indexrelname, idx_scan FROM pg_stat_user_indexes
WHERE idx_scan = 0 ORDER BY pg_relation_size(indexrelid) DESC;
```

## Query Rewriting

```sql
-- Bad: Correlated subquery
SELECT * FROM conversations c
WHERE (SELECT COUNT(*) FROM messages WHERE conversation_id = c.id) > 10;

-- Good: JOIN with HAVING
SELECT c.id, COUNT(m.id) FROM conversations c
JOIN messages m ON m.conversation_id = c.id
GROUP BY c.id HAVING COUNT(m.id) > 10;

-- Bad: OFFSET pagination
SELECT * FROM messages ORDER BY created_at LIMIT 20 OFFSET 50000;

-- Good: Keyset pagination
SELECT * FROM messages WHERE created_at < @last_seen
ORDER BY created_at DESC LIMIT 20;
```

## Statistics Management

```sql
-- PostgreSQL: Update table statistics
ANALYZE conversations;
ANALYZE messages;

-- SQL Server: Update statistics
UPDATE STATISTICS conversations;
UPDATE STATISTICS messages;
```

## Troubleshooting

| Issue | Cause | Fix |
|-------|-------|-----|
| Seq scan on indexed column | Statistics stale | Run ANALYZE |
| Slow with LIKE '%text%' | B-tree can't use leading wildcard | Use full-text search or GIN trigram |
| Join order wrong | Planner estimates off | Update statistics, consider join hints |
| Index not used | Type mismatch in WHERE | Ensure column and parameter types match |
Files in this package (3)
  • SKILL.lean.md 3 KB
  • SKILL.md 3 KB
  • agents/openai.yaml 1 KB

Versions

The database has no other skills named fai-sql-optimization-skill.

Database snapshot: 13.11.2026 · package version c27f1e0a50ab (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

Report a problem with this skill: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 95sql-optimization
    SQL query optimization patterns including EXPLAIN plan analysis, index strategies, query rewriting, and N+1 query prevention. Use when optimizing slow database queries, analyzing query performance, designing indexes, or debugging database bottlenecks. Works with PostgreSQL,…
  • 100sql-pro
    Use this agent when you need to optimize complex SQL queries, design efficient database schemas, or solve performance issues across PostgreSQL, MySQL, SQL Server, and Oracle requiring advanced query optimization, index strategies, or data warehouse patterns.
  • 100sql-pro
    Use this agent when you need to optimize complex SQL queries, design efficient database schemas, or solve performance issues across PostgreSQL, MySQL, SQL Server, and Oracle requiring advanced query optimization, index strategies, or data warehouse patterns.
  • 100sql-expert
    Expert-level SQL database design, querying, optimization, and administration across PostgreSQL, MySQL, and SQL Server. Use when the user mentions database, PostgreSQL, MySQL, or query optimization, or when the task involves Database Design, Advanced Queries, Indexes and…
  • 100sql-optimizer
    Analyze and optimize SQL queries for performance. Use when a user asks to optimize a query, speed up a slow query, analyze a query plan, add indexes, fix N+1 queries, reduce query time, tune database performance, or rewrite SQL for efficiency. Supports PostgreSQL, MySQL, and SQLite.
  • 100fai-sql-code-review-skill
    Review SQL code for correctness, performance, security, and maintainability across PostgreSQL, SQL Server, and MySQL. Use when auditing SQL in PRs or reviewing database migration scripts.

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.