agentsclimarketplace

Sql optimization patterns

Skill tranhieutt/software_development_department/.claude/skills/sql-optimization-patterns

Provides SQL optimization patterns for query performance, indexing strategies, schema design, and database tuning. Use when optimizing slow queries, designing indexes, or tuning database performance.From its SKILL.md

Install
npx -y skills add tranhieutt/software_development_department --skill sql-optimization-patterns

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

SKILL.md

2.7 KB, 598 tokens by cl100k_base, as published. Nobody here has run it

SQL Optimization Patterns

Query optimization, indexing, and performance tuning for PostgreSQL, MySQL, and SQLite.

Index Strategy

When to Create Index

```sql -- High selectivity columns (many unique values) CREATE INDEX idx_orders_user_id ON orders(user_id);

-- Composite index: order matters (equality first, then range) CREATE INDEX idx_orders_user_date ON orders(user_id, created_at DESC);

-- Covering index (includes all needed columns) CREATE INDEX idx_orders_covering ON orders(user_id, status) INCLUDE (total, created_at); ```

When NOT to Index

  • Low cardinality columns (boolean, status with few values)
  • Small tables (< 1000 rows)
  • Write-heavy tables with rare reads

Query Patterns

Avoid SELECT *

```sql -- Bad SELECT * FROM orders WHERE user_id = 1;

-- Good (select only needed columns) SELECT id, total, status FROM orders WHERE user_id = 1; ```

Avoid N+1 (use JOIN or subquery)

```sql -- Bad: N+1 queries from application -- Good: Single query with JOIN SELECT o.id, o.total, u.name FROM orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'pending'; ```

Pagination (keyset, not OFFSET)

```sql -- Bad: OFFSET scans all skipped rows SELECT * FROM orders ORDER BY id LIMIT 20 OFFSET 10000;

-- Good: Keyset pagination SELECT * FROM orders WHERE id > 10000 ORDER BY id LIMIT 20; ```

EXPLAIN ANALYZE

```sql EXPLAIN ANALYZE SELECT * FROM orders WHERE user_id = 1 AND status = 'pending'; ```

Read output:

  • Seq Scan = missing index
  • Index Scan = good
  • Nested Loop with high row count = check join strategy

Schema Anti-Patterns

Anti-PatternProblemFix
EAV (Entity-Attribute-Value)No type safety, slow queriesUse JSONB or proper columns
God tableToo many columnsNormalize into related tables
No constraintsData integrity issuesAdd CHECK, FK, UNIQUE constraints
String datesSorting/filtering issuesUse TIMESTAMP type

Connection Pooling

``` App → Pool (min: 5, max: 20) → PostgreSQL ```

Tools: PgBouncer (PostgreSQL), ProxySQL (MySQL).

Related Skills

  • database-architect — schema design
  • postgres-patterns — PostgreSQL specifics
  • nosql-expert — NoSQL alternatives
  • db-review — database code review

What ships with it: 1 file

13.0 KB alongside SKILL.md

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.