- Home
- Skills
- Data & Databases
- Database Query & Migration Optimizer
Works with the AI Agents you already use
Database Query & Migration Optimizer
Autonomous DBA skill. Detects N+1 query bottlenecks, missing composite indexes, and generates SQL/ORM migrations classified by real locking impact (Postgres, MySQL, MariaDB, SQLite, Prisma, Drizzle, SQLAlchemy, Django).
$16
Database Query & Migration Optimizer
Example session with this skill installed
Audit this PostgreSQL Prisma query for a news feed. It fetches users, then loops to fetch posts and their comments. Generate a non-blocking index migration for the comments table.
- Read your context and instructions
- Compiled the database query
I have audited the feed logic. The current implementation triggers an N+1 pattern by fetching comments individually for each post. I have refactored this to use Prisma's include for batch fetching and generated a concurrent migration for the necessary indexes.
Database Optimization Report
| Feature | Current State | Optimized State | Impact |
|---|---|---|---|
| Query Pattern | N+1 (1 + N + M queries) | Eager Load (3 queries total) | High |
| Index Strategy | Sequential Scan | Index Scan (Composite) | Medium |
| Locking Impact | None (Read) | Online (Concurrent Index) | Low |
Refactored Prisma Query
const feed = await prisma.user.findUnique({
where: { id: userId },
include: {
posts: {
include: {
comments: {
orderBy: { createdAt: 'desc' },
take: 20
}
}
}
}
});
PostgreSQL Migration (SQL)
-- UP MIGRATION
-- Classified: ONLINE
-- Impact: No locks on reads or writes. Note: Cannot run inside a transaction block.
CREATE INDEX CONCURRENTLY idx_comments_post_created
ON "Comment" (post_id, created_at DESC);
-- DOWN MIGRATION
-- Classified: ONLINE
DROP INDEX CONCURRENTLY IF EXISTS idx_comments_post_created;
Next steps
- Run the migration using a tool that supports non-transactional DDL (e.g., Prisma's
$executeRawUnsafeor direct CLI). - Verify the index is valid using
\d commentsin psql after completion. - Monitor for any temporary CPU spikes during the concurrent index build.
Connects securely to your tools. The creator never sees your data.
About this skill
The problem
Naive ORM queries and unoptimized SQL frequently cause production bottlenecks, N+1 query loops, and table locking during migrations. Developers often guess at index placement or rely on default migration tools that can inadvertently block writes on large production tables.
What it does
- Detects N+1 query patterns in ORM code and provides drop-in refactors using eager loading or batching.
- Architects composite B-Tree indexes based on
WHERE,JOIN, andORDER BYclause cardinality. - Rewrites expensive subqueries into Common Table Expressions (CTEs) or window functions.
- Replaces slow
OFFSET/LIMITscanning with indexed keyset (cursor-based) pagination. - Generates migrations for PostgreSQL, MySQL, and SQLite classified by their real locking impact (online, write-blocking, or downtime-required).
Frameworks & tools
PostgreSQL, MySQL, MariaDB, SQLite, Prisma, Drizzle, SQLAlchemy, Django ORM, TypeORM.
Why this beats prompting it yourself
General LLMs often suggest "zero downtime" migrations that actually trigger metadata locks or write blocks on specific database versions. This skill applies engine-specific rules, such as CREATE INDEX CONCURRENTLY for Postgres and ALGORITHM=INPLACE for MySQL, while validating support for the specific operation to prevent production outages.
Use cases
- Auditing Prisma or Django routes to eliminate nested database loops.
- Optimizing pagination for datasets with millions of rows.
- Refactoring complex 5-table joins into performant CTEs.
- Safe schema migrations for high-traffic tables.
Known limitations
Performance timings are estimates unless a live EXPLAIN ANALYZE output is provided. Requires engine version details for accurate locking classification.
How to use
- 1
Connect your agent to the Agensi MCP
Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup
- 2
Sign in to Agensi
Your agent shows a sign-in link. Sign in and click Allow.
- 3
Ask for what you need
Your agent loads this skill and uses it on your task. It always asks before buying a skill.
Reviews
No reviews yet
Be one of the first to try it. Every listed skill goes through the trust checks below.
Security scanned
Checked by our 8-point scan before listing
Fresh listing
Recently published to Agensi
Money-back guarantee
Doesn't do what its page promises? You get your money back.
Trust & safety
Security scan
Security scan passed 2 months ago
Permissions
What's inside
Frequently Asked Questions
New in Data & Databases
Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Data & Databases.