analyze_query_performance
Analyze SQL query performance and receive optimization suggestions to improve database efficiency and execution speed.
Instructions
Analyze query performance and provide optimization suggestions
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| query | Yes | SQL query to analyze for performance optimization | |
| database | No | Database name (optional) |
Implementation Reference
- lib/analysis/query-optimizer.js:46-89 (handler)Core implementation performing static analysis on SQL queries: calculates complexity score, detects bottlenecks, generates performance warnings and optimization suggestions using heuristic analysis.analyzeQuery(query, executionStats = {}, planData = {}) { // Input validation to prevent null pointer exceptions if (!query || typeof query !== 'string') { throw new Error('Query must be a non-empty string'); } try { const analysis = { query: query.trim(), query_type: this.determineQueryType(query), complexity_score: this.calculateComplexityScore(query), estimated_cost: planData.TotalSubtreeCost || 0, table_access_methods: this.extractTableAccessMethods(planData), join_algorithms: this.extractJoinAlgorithms(planData), operators: this.extractOperators(planData), bottlenecks: this.identifyBottlenecks(query, executionStats, planData), performance_warnings: this.generatePerformanceWarnings(query, executionStats), optimization_suggestions: this.generateOptimizationSuggestions( query, executionStats, planData ), is_modification_query: this.isModificationQuery(query) }; return analysis; } catch (error) { // Log the error and return a safe fallback analysis console.error('Error during query analysis:', error.message); return { query: query.trim(), query_type: 'UNKNOWN', complexity_score: 0, estimated_cost: 0, table_access_methods: [], join_algorithms: [], operators: [], bottlenecks: [], performance_warnings: ['Query analysis failed due to parsing error'], optimization_suggestions: [], is_modification_query: false, error: 'Query analysis failed' }; }
- lib/tools/tool-registry.js:162-172 (schema)Tool schema definition including input validation schema with required 'query' parameter and optional 'database'.name: 'analyze_query_performance', description: 'Analyze query performance and provide optimization suggestions', inputSchema: { type: 'object', properties: { query: { type: 'string', description: 'SQL query to analyze for performance optimization' }, database: { type: 'string', description: 'Database name (optional)' } }, required: ['query'] } }
- index.js:333-336 (registration)Tool registration in the main MCP tool call dispatcher switch statement.case 'analyze_query_performance': return { content: await this.analyzeQueryPerformance(args.query, args.database) };
- index.js:658-672 (handler)MCP tool handler wrapper that delegates analysis to QueryOptimizer class and formats JSON response.async analyzeQueryPerformance(query, database) { const analysis = await this.queryOptimizer.analyzeQuery(query, database); return [ { type: 'text', text: JSON.stringify( { success: true, data: analysis }, null, 2 ) } ];