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
| Name | Required | Description | Default |
|---|---|---|---|
| tableName | Yes | Name of the table to analyze |
Implementation Reference
- src/tools/database.ts:604-651 (handler)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) }] }; } }
- src/tools/database.ts:113-115 (schema)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") });
- src/tools/database.ts:599-652 (registration)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) }] }; } } });