explain_query
Analyze MySQL query execution plans to identify performance bottlenecks and optimize slow queries using the EXPLAIN command.
Instructions
Run EXPLAIN on a SQL query to analyze its execution plan and performance.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| sql | Yes | The SQL query to explain. |
Implementation Reference
- src/index.ts:49-63 (registration)Registration of the 'explain_query' tool via server.registerTool(), including its schema (input: sql string) and handler.
server.registerTool( 'explain_query', { description: 'Run EXPLAIN on a SQL query to analyze its execution plan and performance.', inputSchema: z.object({ sql: z.string().describe('The SQL query to explain.'), }), }, async ({ sql }) => { const results = await db.query(`EXPLAIN ${sql}`); return { content: [{ type: 'text', text: JSON.stringify(results, null, 2) }], }; } ); - src/index.ts:57-62 (handler)The async handler that executes the EXPLAIN logic: it calls db.query() with 'EXPLAIN ${sql}' and returns the results as JSON text.
async ({ sql }) => { const results = await db.query(`EXPLAIN ${sql}`); return { content: [{ type: 'text', text: JSON.stringify(results, null, 2) }], }; } - src/index.ts:51-55 (schema)Input schema for 'explain_query' using Zod: requires a single 'sql' string parameter describing the query to explain.
{ description: 'Run EXPLAIN on a SQL query to analyze its execution plan and performance.', inputSchema: z.object({ sql: z.string().describe('The SQL query to explain.'), }), - src/db.ts:42-45 (helper)The db.query() helper function used by the explain_query handler to execute the EXPLAIN SQL against MySQL.
export async function query(sql: string, params?: any[]) { const [rows] = await pool.query(sql, params); return rows; }