Skip to main content
Glama

analyze-query-performance

analyze-query-performance

Analyze SQL query performance by executing queries multiple times to measure execution time and identify optimization opportunities for Firebird databases.

Instructions

Analyzes the performance of a SQL query by executing it multiple times and measuring execution time

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlYesSQL query to analyze
paramsNoParameters for parameterized queries
iterationsNoNumber of times to run the query for averaging performance

Implementation Reference

  • Registration of the 'analyze-query-performance' MCP tool, including inline handler that parses args, calls the core analyzeQueryPerformance function, handles errors, and formats response for MCP.
    tools.set("analyze-query-performance", {
        name: "analyze-query-performance",
        description: "Analyzes the performance of a SQL query by executing it multiple times and measuring execution time",
        inputSchema: AnalyzeQueryPerformanceArgsSchema,
        handler: async (request) => {
            const { sql, params, iterations } = request;
            logger.info(`Executing analyze-query-performance tool for query: ${sql.substring(0, 50)}...`);
    
            try {
                const result = await analyzeQueryPerformance(
                    sql,
                    params || [],
                    iterations || 3
                );
    
                return {
                    content: [{
                        type: "text",
                        text: formatForClaude(result)
                    }]
                };
            } catch (error) {
                const errorResponse = wrapError(error);
                logger.error(`Error analyzing query performance: ${errorResponse.error} [${errorResponse.errorType || 'UNKNOWN'}]`);
    
                return {
                    content: [{
                        type: "text",
                        text: formatForClaude(errorResponse)
                    }]
                };
            }
        }
    });
  • Zod input schema for the analyze-query-performance tool defining sql, optional params, and iterations.
    export const AnalyzeQueryPerformanceArgsSchema = z.object({
        sql: z.string().min(1).describe("SQL query to analyze"),
        params: z.array(z.string().or(z.number()).or(z.boolean()).or(z.null())).optional().describe("Parameters for parameterized queries"),
        iterations: z.number().int().positive().default(3).describe("Number of times to run the query for averaging performance")
    });
  • Core helper function that implements query performance analysis by running the query multiple times, calculating stats (avg/min/max time, row count), and providing basic optimization suggestions.
    export const analyzeQueryPerformance = async (
        sql: string,
        params: any[] = [],
        iterations: number = 3,
        config = DEFAULT_CONFIG
    ): Promise<QueryPerformanceResult> => {
        try {
            // Validate the SQL query to prevent injection
            if (!validateSql(sql)) {
                throw new FirebirdError(
                    `Invalid SQL query: ${sql}`,
                    'VALIDATION_ERROR'
                );
            }
    
            logger.info(`Analyzing query performance with ${iterations} iterations`);
            logger.debug(`Query: ${sql}`);
    
            const executionTimes: number[] = [];
            let rowCount = 0;
            let results: any[] = [];
    
            // Execute the query multiple times and measure performance
            for (let i = 0; i < iterations; i++) {
                const startTime = performance.now();
                results = await executeQuery(sql, params, config);
                const endTime = performance.now();
    
                const executionTime = endTime - startTime;
                executionTimes.push(executionTime);
    
                // Only set rowCount on the first iteration
                if (i === 0) {
                    rowCount = results.length;
                }
    
                logger.debug(`Iteration ${i+1}: ${executionTime.toFixed(2)}ms`);
            }
    
            // Calculate statistics
            const averageTime = executionTimes.reduce((sum, time) => sum + time, 0) / executionTimes.length;
            const minTime = Math.min(...executionTimes);
            const maxTime = Math.max(...executionTimes);
    
            // Basic query analysis
            let analysis = "";
    
            // Check if the query has a WHERE clause
            if (!sql.toLowerCase().includes('where') && rowCount > 100) {
                analysis += "Query doesn't have a WHERE clause and returns many rows. Consider adding filters. ";
            }
    
            // Check for potential full table scans
            if (sql.toLowerCase().includes('select') && !sql.toLowerCase().includes('index') && rowCount > 1000) {
                analysis += "Query might be performing a full table scan. Consider using indexed columns in the WHERE clause. ";
            }
    
            // Check for ORDER BY on non-indexed columns (simplified check)
            if (sql.toLowerCase().includes('order by') && rowCount > 500) {
                analysis += "Query includes ORDER BY which might be slow on large datasets if columns aren't indexed. ";
            }
    
            // Performance assessment
            if (averageTime < 100) {
                analysis += "Performance is good. ";
            } else if (averageTime < 500) {
                analysis += "Performance is acceptable. ";
            } else if (averageTime < 1000) {
                analysis += "Performance could be improved. ";
            } else {
                analysis += "Performance is poor, query optimization is recommended. ";
            }
    
            const result: QueryPerformanceResult = {
                query: sql,
                executionTimes,
                averageTime,
                minTime,
                maxTime,
                rowCount,
                success: true,
                analysis: analysis.trim()
            };
    
            logger.info(`Query analysis complete: Avg=${averageTime.toFixed(2)}ms, Rows=${rowCount}`);
            return result;
    
        } catch (error: any) {
            const errorMessage = `Error analyzing query performance: ${error.message || error}`;
            logger.error(errorMessage);
    
            return {
                query: sql,
                executionTimes: [],
                averageTime: 0,
                minTime: 0,
                maxTime: 0,
                rowCount: 0,
                success: false,
                error: errorMessage,
                analysis: "Query execution failed."
            };
        }
    };
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

No annotations are provided, so the description carries the full burden. It mentions executing the query multiple times and measuring execution time, which implies read-only behavior (no data modification) and potential performance impact. However, it lacks details on permissions required, whether it runs in a test environment, rate limits, or what the output format looks like. For a tool that executes queries, this is insufficient behavioral disclosure.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, clear sentence that efficiently conveys the core functionality. It's front-loaded with the main purpose and includes the key method. There's no wasted language or unnecessary elaboration.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given no annotations and no output schema, the description is incomplete for a tool that executes SQL queries. It doesn't explain what the output contains (e.g., average execution time, statistics), whether it runs in isolation, or potential side effects. For a performance analysis tool with 3 parameters, this leaves significant gaps in understanding how to use it effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters3/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Schema description coverage is 100%, so the schema already documents all three parameters (sql, params, iterations) with good descriptions. The description adds no additional parameter semantics beyond what's in the schema. It mentions 'executing it multiple times' which aligns with the 'iterations' parameter, but doesn't provide new insights. Baseline 3 is appropriate when schema does the heavy lifting.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the tool's purpose: 'Analyzes the performance of a SQL query by executing it multiple times and measuring execution time.' It specifies the verb ('analyzes'), resource ('SQL query'), and method ('executing it multiple times and measuring execution time'). However, it doesn't explicitly differentiate from siblings like 'get-execution-plan' or 'execute-query', which could have overlapping functionality.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives. With siblings like 'execute-query' (for single execution), 'get-execution-plan' (for query plan analysis), and 'analyze-missing-indexes' (for index optimization), there's no indication of when this performance analysis tool is preferred. The description only states what it does, not when it should be used.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/PuroDelphi/mcpFirebird'

If you have feedback or need assistance with the MCP directory API, please join our Discord server