# 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).

Database Query & Migration Optimizer is a paid AI agent skill on Agensi by Via mae Sarol, in Data & Databases. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: $16 (one-time purchase)
- Creator: [Via mae Sarol](https://www.agensi.io/creators/via-mae-sarol)
- Category: [Data & Databases](https://www.agensi.io/skills/data-databases)
- Version: 1.0
- Last updated: 2026-09-10
- Security: scan passed (2026-09-10); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/database-query-migration-optimizer
- Tags: database, postgresql, mysql, prisma, sqlalchemy, orm

## 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`, and `ORDER BY` clause cardinality.
- Rewrites expensive subqueries into Common Table Expressions (CTEs) or window functions.
- Replaces slow `OFFSET/LIMIT` scanning 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 this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the Database Query & Migration Optimizer skill from Agensi" and describe your task.
3. Buy it on the listing or let your agent ask you first; your agent then loads it from Agensi when it needs it. Every paid skill has a money-back guarantee: https://www.agensi.io/docs/money-back-guarantee

## Frequently asked questions

### How does this skill differ from a standard AI chat response for SQL optimization?

The skill analyzes ORM code or raw SQL to identify inefficiencies like N+1 loops and slow scans, then generates optimized queries and migration files tailored to your specific database engine.

### Which database engines and ORMs are compatible with the Optimizer?

It supports PostgreSQL, MySQL, MariaDB, and SQLite, along with major ORMs like Prisma, Drizzle, SQLAlchemy, Django, and TypeORM.

### Does the skill account for production downtime when generating schema migrations?

The skill evaluates migrations based on engine-specific locking behavior, recommending 'online' strategies like PostgreSQL's CONCURRENTLY index creation to prevent table locks during high-traffic periods.

### What exactly is included with the purchase of this skill?

You will receive the full AI agent skill definition, including the specialized logic for query refactoring, indexing architecture, and database-specific locking rules.

### Do I need to provide my database version for this to work correctly?

For the most accurate locking impact classification and syntax generation, you should provide the specific database engine name and version number (e.g., PostgreSQL 15 or MySQL 8.0).

### How do I integrate this skill into my existing development workflow?

Simply import the skill into your preferred AI agent host; it works by reviewing your schema files, ORM snippets, or SQL logs to provide actionable refactoring advice.

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
