‹ 首页

sql-optimization-patterns

@tranhieutt · 收录于 1 周前

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.

适合你,如果经常处理数据库性能瓶颈或慢查询问题

/ 下载安装
sql-optimization-patterns.skill双击,或拖进 Claude 桌面版 / Cowork,即完成安装↓ .skill↓ .zip
用别的 agent?下载 .zip 解压,把文件夹放进它的技能目录
Claude Code~/.claude/skills/(项目级 .claude/skills/)
Codex CLI~/.codex/skills/
Cursor自动读取上面两处目录
其他工具见其文档的「skills」目录;两个下载是同一份文件,只是名字不同
/ 通过 npx 安装 校验哈希
npx oh-my-skill add tranhieutt/software_development_department/sql-optimization-patterns
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- tranhieutt/software_development_department/sql-optimization-patterns
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify tranhieutt/software_development_department/sql-optimization-patterns
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
68GitHub stars
~581最小装载
~581含声明引用
~3.4K文本包总量
镜像托管

怎么用

技能原文 SKILL.md作者撰写 · MIT · e5ba809

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-Pattern | Problem | Fix | |-------------|---------|-----| | EAV (Entity-Attribute-Value) | No type safety, slow queries | Use JSONB or proper columns | | God table | Too many columns | Normalize into related tables | | No constraints | Data integrity issues | Add CHECK, FK, UNIQUE constraints | | String dates | Sorting/filtering issues | Use 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
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。