Skip to main content
Glama

analyze-table-statistics

Analyze table statistics including row count, column data, and distribution patterns to understand database structure and optimize queries.

Instructions

Analyzes statistical information about a table including row count, column statistics, and data distribution.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
tableNameYesName of the table to analyze

Implementation Reference

  • The async handler function that executes the core logic: queries row count, table schema using describeTable, sample data, computes statistics object including column details, and returns formatted response or error.
    handler: async (args: z.infer<typeof AnalyzeTableStatisticsArgsSchema>) => {
        const { tableName } = args;
        logger.info(`Analyzing statistics for table: ${tableName}`);
    
        try {
            // Get row count
            const countResult = await executeQuery(`SELECT COUNT(*) as ROW_COUNT FROM "${tableName}"`);
            const rowCount = countResult[0]?.ROW_COUNT || 0;
    
            // Get table schema
            const schema = await describeTable(tableName);
    
            // Get sample data for analysis
            const sampleData = await executeQuery(`SELECT FIRST 100 * FROM "${tableName}"`);
    
            const statistics = {
                tableName,
                rowCount,
                columnCount: schema.length,
                sampleSize: sampleData.length,
                columns: schema.map((col: any) => ({
                    name: col.FIELD_NAME,
                    type: col.FIELD_TYPE,
                    nullable: col.NULL_FLAG === 'YES',
                    hasDefault: !!col.DEFAULT_VALUE
                }))
            };
    
            logger.info(`Statistics analyzed for ${tableName}: ${rowCount} rows, ${schema.length} columns`);
    
            return {
                content: [{
                    type: "text",
                    text: formatForClaude(statistics)
                }]
            };
        } catch (error) {
            const errorResponse = wrapError(error);
            logger.error(`Error analyzing statistics for ${tableName}: ${errorResponse.error}`);
    
            return {
                content: [{
                    type: "text",
                    text: formatForClaude(errorResponse)
                }]
            };
        }
    }
  • Zod input schema requiring a single 'tableName' string parameter.
    export const AnalyzeTableStatisticsArgsSchema = z.object({
        tableName: z.string().min(1).describe("Name of the table to analyze")
    });
  • Registers the 'analyze-table-statistics' tool in the Map returned by setupDatabaseTools, providing name, title, description, schema, and handler.
    tools.set("analyze-table-statistics", {
        name: "analyze-table-statistics",
        title: "Analyze Table Statistics",
        description: "Analyzes statistical information about a table including row count, column statistics, and data distribution.",
        inputSchema: AnalyzeTableStatisticsArgsSchema,
        handler: async (args: z.infer<typeof AnalyzeTableStatisticsArgsSchema>) => {
            const { tableName } = args;
            logger.info(`Analyzing statistics for table: ${tableName}`);
    
            try {
                // Get row count
                const countResult = await executeQuery(`SELECT COUNT(*) as ROW_COUNT FROM "${tableName}"`);
                const rowCount = countResult[0]?.ROW_COUNT || 0;
    
                // Get table schema
                const schema = await describeTable(tableName);
    
                // Get sample data for analysis
                const sampleData = await executeQuery(`SELECT FIRST 100 * FROM "${tableName}"`);
    
                const statistics = {
                    tableName,
                    rowCount,
                    columnCount: schema.length,
                    sampleSize: sampleData.length,
                    columns: schema.map((col: any) => ({
                        name: col.FIELD_NAME,
                        type: col.FIELD_TYPE,
                        nullable: col.NULL_FLAG === 'YES',
                        hasDefault: !!col.DEFAULT_VALUE
                    }))
                };
    
                logger.info(`Statistics analyzed for ${tableName}: ${rowCount} rows, ${schema.length} columns`);
    
                return {
                    content: [{
                        type: "text",
                        text: formatForClaude(statistics)
                    }]
                };
            } catch (error) {
                const errorResponse = wrapError(error);
                logger.error(`Error analyzing statistics for ${tableName}: ${errorResponse.error}`);
    
                return {
                    content: [{
                        type: "text",
                        text: formatForClaude(errorResponse)
                    }]
                };
            }
        }
    });

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