query-performance-optimizer
Analyze SAP OData queries to identify performance bottlenecks and recommend optimization strategies for improved speed and efficiency.
Instructions
Optimize SAP OData query performance by analyzing execution patterns and suggesting improvements. Automatically identifies bottlenecks and recommends index strategies.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| entityType | Yes | Target entity type | |
| executionStats | No | Query execution statistics | |
| optimizationGoals | No | Primary optimization objectives | |
| query | Yes | Original OData query URL to optimize |
Implementation Reference
- src/tools/ai-enhanced-tools.ts:282-321 (handler)Main execution logic of the QueryPerformanceOptimizerTool. Analyzes input query, creates mock entity type, calls aiIntegration.optimizeQuery, computes improvements and performance gain, returns optimized query with explanations.async execute(params: any): Promise<any> { try { logger.info('Optimizing query performance', { originalQuery: params.query, entityType: params.entityType, goals: params.optimizationGoals, }); // Create mock entity for optimization const tool = new NaturalQueryBuilderTool(); const mockEntityType = tool.createMockEntityType(params.entityType); const optimizationResult = await aiIntegration.optimizeQuery( `Optimize this query for performance: ${params.query}`, mockEntityType, { optimizationGoals: params.optimizationGoals } ); const improvements = this.analyzeImprovements(params.query, optimizationResult.url); const performanceGain = this.estimatePerformanceGain(improvements); return { success: true, originalQuery: params.query, optimizedQuery: optimizationResult.url, improvements, performanceGain, explanation: optimizationResult.explanation, }; } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; logger.error('Query optimization failed', { error: errorMessage }); return { success: false, originalQuery: params.query, error: errorMessage, }; } }
- Input schema validation for the tool, defining required query and entityType, optional executionStats and optimizationGoals.inputSchema = { type: 'object' as const, properties: { query: { type: 'string' as const, description: 'Original OData query URL to optimize', }, entityType: { type: 'string' as const, description: 'Target entity type', }, executionStats: { type: 'object' as const, properties: { executionTime: { type: 'number' as const }, recordCount: { type: 'number' as const }, dataSize: { type: 'number' as const }, }, }, optimizationGoals: { type: 'array' as const, items: { type: 'string' as const, enum: ['speed', 'bandwidth', 'accuracy', 'caching'] as const, }, description: 'Primary optimization objectives', }, }, required: ['query', 'entityType'], };
- src/tools/ai-enhanced-tools.ts:462-467 (registration)Registration of the tool instance in the aiEnhancedTools export array, which is likely used by the MCP server to register the tool.export const aiEnhancedTools = [ new NaturalQueryBuilderTool(), new SmartDataAnalysisTool(), new QueryPerformanceOptimizerTool(), new BusinessProcessInsightsTool(), ];
- Helper function that compares original and optimized queries to identify specific improvements like added $select or $top.private analyzeImprovements(original: string, optimized: string): string[] { const improvements: string[] = []; if (optimized.includes('$select') && !original.includes('$select')) { improvements.push('Added field selection to reduce data transfer'); } if (optimized.includes('$top') && !original.includes('$top')) { improvements.push('Added result limit to improve response time'); } if (improvements.length === 0) { improvements.push('Query structure optimized for better SAP backend processing'); } return improvements; }
- Helper function that estimates performance gain based on number of improvements found.private estimatePerformanceGain(improvements: string[]): string { const totalGain = Math.min(improvements.length * 15, 80); return `Estimated ${totalGain}% performance improvement`; }