import { QueryHookPlugin, QueryExecutionContext } from '../index'; /** * Query execution plan from EXPLAIN */ export interface QueryExecutionPlan { raw: string; parsed?: any; hasSeqScan?: boolean; estimatedCost?: number; estimatedRows?: number; } /** * Options for SlowQueryAnalyzerPlugin */ export interface SlowQueryAnalyzerOptions { /** * Threshold in milliseconds for running EXPLAIN (default: 1000ms) * Queries slower than this will automatically get analyzed */ threshold?: number; /** * Run EXPLAIN ANALYZE (default: false) * When true, runs actual query to get real execution stats * ⚠️ Warning: EXPLAIN ANALYZE actually executes the query */ runAnalyze?: boolean; /** * Database type for EXPLAIN syntax (default: 'postgres') * Different databases have different EXPLAIN syntax */ databaseType?: 'postgres' | 'mysql' | 'mariadb' | 'sqlite' | 'mssql'; /** * Callback when analysis is complete (optional) * * @param context - Query execution context * @param plan - Parsed execution plan * * @example * ```typescript * onAnalysis: (context, plan) => { * if (plan.hasSeqScan) { * logger.warn('Sequential scan detected - missing index?', { * sql: context.sql, * plan: plan.raw * }); * } * } * ``` */ onAnalysis?: (context: QueryExecutionContext, plan: QueryExecutionPlan) => void | Promise; /** * Enable console logging for this plugin (default: false) * When true, logs execution plans for slow queries */ enableLogging?: boolean; } /** * Plugin for automatic query execution plan analysis * Automatically runs EXPLAIN on slow queries to identify issues * * 🔬 Solves: Manual copy-paste of SQL to run EXPLAIN in DB tool * * **The Problem:** * 1. Slow query alert fires * 2. You copy the SQL * 3. Open pgAdmin/DBeaver * 4. Paste and run EXPLAIN ANALYZE * 5. Look for issues (seq scans, missing indexes, etc.) * * **The Solution:** * Automatically runs EXPLAIN when query is slow and shows you the plan immediately * * @example * ```typescript * import { enableQueryHooks, registerPlugin } from 'typeorm-query-hooks'; * import { SlowQueryAnalyzerPlugin } from 'typeorm-query-hooks/plugins/slow-query-analyzer'; * * enableQueryHooks(); * * // Basic usage: Auto-analyze slow queries * registerPlugin(SlowQueryAnalyzerPlugin({ * threshold: 1000, // Analyze queries > 1s * runAnalyze: false, // Don't run EXPLAIN ANALYZE (safer) * databaseType: 'postgres', * enableLogging: true * })); * * // Advanced: Detect missing indexes * registerPlugin(SlowQueryAnalyzerPlugin({ * threshold: 500, * databaseType: 'postgres', * onAnalysis: async (context, plan) => { * // Check for sequential scans (indicates missing index) * if (plan.hasSeqScan) { * logger.error('🔍 MISSING INDEX DETECTED:', { * sql: context.sql.substring(0, 200), * executionTime: context.executionTime, * estimatedCost: plan.estimatedCost, * plan: plan.raw, * suggestion: 'Consider adding an index to improve performance' * }); * * // Send to monitoring * monitoring.alert({ * type: 'missing_index_detected', * severity: 'high', * details: plan * }); * } * } * })); * ``` */ export declare function SlowQueryAnalyzerPlugin(options?: SlowQueryAnalyzerOptions): QueryHookPlugin; //# sourceMappingURL=slow-query-analyzer.d.ts.map