Skip to main content
Glama

analyze-missing-indexes

analyze-missing-indexes

Analyzes SQL queries to identify missing indexes that could improve database performance in Firebird databases.

Instructions

Analyzes a SQL query to identify missing indexes that could improve performance

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
sqlYesSQL query to analyze for missing indexes

Implementation Reference

  • Core handler function that implements the logic for analyzing SQL queries to detect potential missing indexes by parsing WHERE, JOIN, and ORDER BY clauses using regex, suggesting CREATE INDEX statements.
    export const analyzeMissingIndexes = async (
        sql: string,
        config = DEFAULT_CONFIG
    ): Promise<{missingIndexes: string[], recommendations: string[], success: boolean, error?: string}> => {
        try {
            // Validate the SQL query to prevent injection
            if (!validateSql(sql)) {
                throw new FirebirdError(
                    `Invalid SQL query: ${sql}`,
                    'VALIDATION_ERROR'
                );
            }
    
            logger.info(`Analyzing missing indexes for query: ${sql.substring(0, 100)}${sql.length > 100 ? '...' : ''}`);
    
            // Parse the SQL query to extract table and column information
            const tablePattern = /\bFROM\s+([\w\.]+)\b/i;
            const wherePattern = /\bWHERE\s+(.+?)(?:\bGROUP BY\b|\bORDER BY\b|\bHAVING\b|$)/is;
            const joinPattern = /\bJOIN\s+([\w\.]+)\s+(?:\w+\s+)?ON\s+(.+?)(?:\bJOIN\b|\bWHERE\b|\bGROUP BY\b|\bORDER BY\b|\bHAVING\b|$)/gis;
            const orderByPattern = /\bORDER BY\s+(.+?)(?:$|;)/i;
    
            // Extract the main table
            const tableMatch = sql.match(tablePattern);
            const mainTable = tableMatch ? tableMatch[1].trim() : null;
    
            // Extract WHERE conditions
            const whereMatch = sql.match(wherePattern);
            const whereConditions = whereMatch ? whereMatch[1].trim() : null;
    
            // Extract JOIN conditions
            const joinMatches = Array.from(sql.matchAll(joinPattern));
            const joinTables: {table: string, condition: string}[] = joinMatches.map(match => ({
                table: match[1].trim(),
                condition: match[2].trim()
            }));
    
            // Extract ORDER BY columns
            const orderByMatch = sql.match(orderByPattern);
            const orderByColumns = orderByMatch ? orderByMatch[1].trim() : null;
    
            // Analyze and generate recommendations
            const missingIndexes: string[] = [];
            const recommendations: string[] = [];
    
            // Check WHERE conditions for potential indexes
            if (whereConditions && mainTable) {
                const whereColumns = extractColumnsFromCondition(whereConditions);
                if (whereColumns.length > 0) {
                    const indexName = `IDX_${mainTable}_${whereColumns.join('_')}`;
                    missingIndexes.push(`CREATE INDEX ${indexName} ON ${mainTable} (${whereColumns.join(', ')});`);
                    recommendations.push(`Consider creating an index on ${mainTable}(${whereColumns.join(', ')}) to improve WHERE clause filtering.`);
                }
            }
    
            // Check JOIN conditions for potential indexes
            for (const join of joinTables) {
                const joinColumns = extractColumnsFromCondition(join.condition);
                if (joinColumns.length > 0) {
                    const tableColumns = joinColumns.filter(col => col.includes(join.table + '.'));
                    if (tableColumns.length > 0) {
                        // Extract just the column names without table prefix
                        const columns = tableColumns.map(col => col.split('.')[1]);
                        const indexName = `IDX_${join.table}_${columns.join('_')}`;
                        missingIndexes.push(`CREATE INDEX ${indexName} ON ${join.table} (${columns.join(', ')});`);
                        recommendations.push(`Consider creating an index on ${join.table}(${columns.join(', ')}) to improve JOIN performance.`);
                    }
                }
            }
    
            // Check ORDER BY for potential indexes
            if (orderByColumns && mainTable) {
                const orderCols = orderByColumns.split(',').map(col => col.trim().split(' ')[0]); // Remove ASC/DESC
                const indexName = `IDX_${mainTable}_ORDER_${orderCols.join('_')}`;
                missingIndexes.push(`CREATE INDEX ${indexName} ON ${mainTable} (${orderCols.join(', ')});`);
                recommendations.push(`Consider creating an index on ${mainTable}(${orderCols.join(', ')}) to improve ORDER BY performance.`);
            }
    
            logger.info(`Missing index analysis complete, found ${missingIndexes.length} potential missing indexes`);
    
            return {
                missingIndexes,
                recommendations,
                success: true
            };
    
        } catch (error: any) {
            const errorMessage = `Error analyzing missing indexes: ${error.message || error}`;
            logger.error(errorMessage);
    
            return {
                missingIndexes: [],
                recommendations: [],
                success: false,
                error: errorMessage
            };
        }
    };
  • Registers the MCP tool 'analyze-missing-indexes' in the tools map, including description, input schema, and a wrapper handler that calls the core analyzeMissingIndexes function.
    tools.set("analyze-missing-indexes", {
        name: "analyze-missing-indexes",
        description: "Analyzes a SQL query to identify missing indexes that could improve performance",
        inputSchema: AnalyzeMissingIndexesArgsSchema,
        handler: async (request) => {
            const { sql } = request;
            logger.info(`Executing analyze-missing-indexes tool for query: ${sql.substring(0, 50)}...`);
    
            try {
                const result = await analyzeMissingIndexes(sql);
    
                return {
                    content: [{
                        type: "text",
                        text: formatForClaude(result)
                    }]
                };
            } catch (error) {
                const errorResponse = wrapError(error);
                logger.error(`Error analyzing missing indexes: ${errorResponse.error} [${errorResponse.errorType || 'UNKNOWN'}]`);
    
                return {
                    content: [{
                        type: "text",
                        text: formatForClaude(errorResponse)
                    }]
                };
            }
        }
    });
  • Zod schema defining the input for the tool: a required SQL query string.
    export const AnalyzeMissingIndexesArgsSchema = z.object({
        sql: z.string().min(1).describe("SQL query to analyze for missing indexes")
    });
  • Helper function used by analyzeMissingIndexes to extract column names from SQL WHERE/JOIN conditions using regex.
    function extractColumnsFromCondition(condition: string): string[] {
        const columns: string[] = [];
    
        // Match patterns like: column = value, column IN (...), column BETWEEN ... AND ...
        const columnPattern = /([\w\.]+)\s*(?:=|>|<|>=|<=|<>|!=|LIKE|IN|BETWEEN|IS)/gi;
        let match;
    
        while ((match = columnPattern.exec(condition)) !== null) {
            const column = match[1].trim();
            if (!columns.includes(column)) {
                columns.push(column);
            }
        }
    
        return columns;
    }
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 of behavioral disclosure. It states what the tool does but doesn't describe how it behaves: e.g., whether it's read-only or has side effects, what permissions are required, whether it analyzes live databases or cached data, or what the output format looks like. For a tool with no annotation coverage, this is a significant gap in transparency.

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, efficient sentence that directly states the tool's purpose without any wasted words. It's appropriately sized and front-loaded, with every part contributing essential information about the action and goal.

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 the complexity of SQL performance analysis and the lack of annotations or output schema, the description is incomplete. It doesn't explain what the tool returns (e.g., index recommendations, performance metrics), how it interacts with the database, or any limitations (e.g., supported SQL dialects, database systems). For a tool with no structured output documentation, this leaves significant gaps for an AI agent.

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%, with the single parameter 'sql' fully documented in the schema as 'SQL query to analyze for missing indexes'. The description adds no additional parameter semantics beyond what's already in the schema, so it meets the baseline score of 3 for high schema coverage without extra value.

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 a SQL query to identify missing indexes that could improve performance'. It specifies the verb ('analyzes'), resource ('SQL query'), and outcome ('identify missing indexes that could improve performance'). However, it doesn't explicitly differentiate from sibling tools like 'analyze-query-performance' or 'get-execution-plan', which might 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 'analyze-query-performance' and 'get-execution-plan' that might offer related performance analysis, there's no indication of when this specific tool is appropriate or what distinguishes it from those options. Usage is implied only by the purpose statement.

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