Design database operations including migrations, indexing strategies, transactions, connection pooling, and ORM best practices. Use when creating database schemas, writing migrations, optimizing slow queries, configuring connection pools, or choosing between ORM frameworks.
Purpose: Efficient, reliable database operations with migrations, indexes, and transactions. author: "GitHub Copilot" Note: For database-specific details, see PostgreSQL or MySQL/MariaDB.
Database operation?
+- Schema change?
| +- New table/column? -> Migration (up + down)
| +- Rename/drop? -> Migration + verify no dependents
| - Index needed? -> CREATE INDEX CONCURRENTLY (avoid locks)
+- Query performance?
| +- Slow query? -> EXPLAIN ANALYZE -> add index or rewrite
| +- N+1 problem? -> Use eager loading / JOIN
| - Large result set? -> Pagination (cursor-based preferred)
+- Data integrity?
| +- Multiple writes? -> Use transaction
| - Concurrent access? -> Optimistic concurrency (version column)
- Connection management?
- Always use connection pooling, never open/close per query
Analyze Query Performance:
-- PostgreSQL
EXPLAIN ANALYZE
SELECT * FROM users WHERE email = '[email protected]';
-- MySQL
EXPLAIN
SELECT * FROM users WHERE email = '[email protected]';
Look for:
| Issue | Problem | Solution |
|---|---|---|
| N+1 queries | Loading relations one by one | Use eager loading, JOINs |
| Missing indexes | Slow queries | Add indexes on WHERE/JOIN columns |
| SELECT * | Loading unnecessary data | Select only needed columns |
| No connection pooling | Too many connections | Implement connection pooling |
| Large transactions | Locks held too long | Keep transactions short |
| No query timeout | Queries run forever | Set query timeout limits |
Popular ORMs:
Database Docs:
Tools:
See Also: Skills.md - AGENTS.md - PostgreSQL - MySQL/MariaDB
Last Updated: January 27, 2026
| Script | Purpose | Usage |
|---|---|---|
scaffold-migration.py | Generate migration scaffold (SQL/EF Core/Alembic) | python scripts/scaffold-migration.py --type sql --name add_users_table |
| Issue | Solution |
|---|---|
| Slow query performance | Check EXPLAIN plan, add missing indexes, avoid SELECT * |
| Connection pool exhaustion | Increase pool size, ensure connections are properly disposed/returned |
| Migration conflicts | Use sequential numbering, resolve merge conflicts in migration order |