get_execution_plan
Analyze SQL query performance by retrieving actual execution plans with runtime statistics to identify optimization opportunities.
Instructions
Get the actual execution plan with runtime statistics for a SQL query
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | The SQL query to analyze |
Implementation Reference
- Handler dispatch for 'get_execution_plan': extracts query argument and modifies it to 'EXPLAIN ANALYZE {query}' for execution.elif name == "get_execution_plan": query = arguments.get("query") if not query: raise ValueError("Query is required") query = f"EXPLAIN ANALYZE {query}"
- src/adb_mysql_mcp_server/server.py:151-164 (registration)Registration of the 'get_execution_plan' tool in list_tools(), including its description and input schema.Tool( name="get_execution_plan", description="Get the actual execution plan with runtime statistics for a SQL query", inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The SQL query to analyze" } }, "required": ["query"] } )
- Shared database execution helper: connects to MySQL using config, executes the query (EXPLAIN ANALYZE), fetches results, formats as CSV text content, handles exceptions.conn = pymysql.connect(**config) conn.autocommit(True) cursor = conn.cursor() try: # Execute the query cursor.execute(query) columns = [desc[0] for desc in cursor.description] rows = cursor.fetchall() result = [",".join(map(str, row)) for row in rows] return [TextContent(type="text", text="\n".join([",".join(columns)] + result))] except Exception as e: return [TextContent(type="text", text=f"Error executing query: {str(e)}")] finally: if cursor: cursor.close() if conn.open: conn.close()
- Pydantic input schema validation for the tool: requires a single 'query' string property.inputSchema={ "type": "object", "properties": { "query": { "type": "string", "description": "The SQL query to analyze" } }, "required": ["query"] }