@vasoyaprince14/sql-analyzer
Version:
🚀 Enhanced SQL database analyzer with AI-powered insights, comprehensive security analysis, RLS policy auditing, and beautiful HTML reports
350 lines • 9.94 kB
TypeScript
import { Client } from 'pg';
export interface EnhancedDatabaseHealthReport {
databaseInfo: DatabaseInfo;
schemaHealth: SchemaHealthScore;
indexAnalysis: IndexAnalysis;
tableAnalysis: TableAnalysis;
triggerAnalysis: TriggerAnalysis;
procedureAnalysis: ProcedureAnalysis;
securityAnalysis: SecurityAnalysis;
performanceIssues: PerformanceIssue[];
optimizationRecommendations: OptimizationRecommendation[];
costAnalysis: DatabaseCostAnalysis;
maintenanceRecommendations: MaintenanceRecommendation[];
aiInsights?: AIInsights;
}
export interface TableAnalysis {
totalTables: number;
tablesWithoutPK: string[];
tablesWithBloat: TableBloatInfo[];
partitionedTables: PartitionInfo[];
largeTables: LargeTableInfo[];
orphanedTables: string[];
}
export interface TableBloatInfo {
tableName: string;
estimatedBloat: number;
wastedSpace: string;
recommendation: string;
beforeOptimization: {
size: string;
performance: string;
};
afterOptimization: {
expectedSize: string;
expectedPerformance: string;
improvementPercentage: number;
};
}
export interface TriggerAnalysis {
totalTriggers: number;
activeTriggers: TriggerInfo[];
disabledTriggers: TriggerInfo[];
performanceImpactingTriggers: TriggerInfo[];
recommendations: TriggerRecommendation[];
}
export interface TriggerInfo {
name: string;
table: string;
event: string;
timing: string;
function: string;
enabled: boolean;
estimatedImpact: 'low' | 'medium' | 'high';
}
export interface TriggerRecommendation {
trigger: string;
issue: string;
solution: string;
priority: 'low' | 'medium' | 'high' | 'critical';
beforeOptimization: string;
afterOptimization: string;
expectedImprovement: string;
}
export interface ProcedureAnalysis {
totalProcedures: number;
procedures: ProcedureInfo[];
unusedProcedures: string[];
performanceIssues: ProcedurePerformanceIssue[];
}
export interface ProcedureInfo {
name: string;
language: string;
returnType: string;
parameters: number;
complexity: 'low' | 'medium' | 'high';
lastExecuted?: string;
}
export interface ProcedurePerformanceIssue {
procedure: string;
issue: string;
impact: string;
solution: string;
estimatedImprovement: string;
}
export interface SecurityAnalysis {
rlsPolicies: RLSPolicyInfo[];
permissions: PermissionAnalysis;
vulnerabilities: SecurityVulnerability[];
recommendations: SecurityRecommendation[];
}
export interface RLSPolicyInfo {
table: string;
policy: string;
command: string;
role: string;
expression: string;
enabled: boolean;
effectiveness: 'good' | 'poor' | 'missing';
}
export interface PermissionAnalysis {
overPrivilegedUsers: string[];
publicAccess: string[];
missingPermissions: string[];
recommendations: string[];
}
export interface SecurityVulnerability {
type: 'rls_disabled' | 'public_access' | 'weak_permissions' | 'unencrypted_data' | 'sql_injection_risk';
severity: 'low' | 'medium' | 'high' | 'critical';
description: string;
affectedObjects: string[];
impact: string;
solution: string;
priority: number;
}
export interface SecurityRecommendation {
category: 'access_control' | 'encryption' | 'auditing' | 'policies';
title: string;
description: string;
implementation: string;
beforeState: string;
afterState: string;
securityImprovement: string;
}
export interface AIInsights {
overallAssessment: string;
priorityRecommendations: string[];
riskAnalysis: string;
performancePredictions: string;
costOptimizationSuggestions: string[];
implementationRoadmap: string[];
}
export interface DatabaseInfo {
version: string;
size: string;
tableCount: number;
indexCount: number;
triggerCount: number;
procedureCount: number;
connectionInfo: {
maxConnections: number;
activeConnections: number;
};
settings: DatabaseSettings;
}
export interface DatabaseSettings {
sharedBuffers: string;
effectiveCacheSize: string;
workMem: string;
maintenanceWorkMem: string;
checkpointCompletionTarget: number;
walBuffers: string;
randomPageCost: number;
enableRLS: boolean;
}
export interface SchemaHealthScore {
overall: number;
normalization: number;
indexEfficiency: number;
foreignKeyIntegrity: number;
dataTypes: number;
naming: number;
security: number;
issues: SchemaIssue[];
recommendations: SchemaRecommendation[];
}
export interface SchemaIssue {
type: 'missing_pk' | 'missing_fk_index' | 'redundant_index' | 'poor_naming' | 'data_type_inefficiency' | 'security_risk';
table: string;
column?: string;
severity: 'low' | 'medium' | 'high' | 'critical';
description: string;
suggestion: string;
sqlFix?: string;
beforeFix: string;
afterFix: string;
expectedImprovement: string;
}
export interface SchemaRecommendation {
type: 'index' | 'normalization' | 'data_type' | 'constraint' | 'security' | 'other';
priority: 'low' | 'medium' | 'high' | 'critical';
title: string;
description: string;
sql?: string;
impact: 'low' | 'medium' | 'high';
fix: string;
improvement: string;
}
export interface IndexAnalysis {
totalIndexes: number;
unusedIndexes: UnusedIndex[];
missingIndexes: MissingIndex[];
duplicateIndexes: DuplicateIndex[];
oversizedIndexes: OversizedIndex[];
indexEfficiencyScore: number;
recommendations: IndexRecommendation[];
}
export interface IndexRecommendation {
type: 'create' | 'drop' | 'modify';
description: string;
sql: string;
estimatedImpact: string;
beforeOptimization: {
queryTime: string;
diskUsage: string;
};
afterOptimization: {
queryTime: string;
diskUsage: string;
improvement: string;
};
}
export interface UnusedIndex {
name: string;
table: string;
size: string;
lastUsed: string | null;
impact: 'low' | 'medium' | 'high';
recommendation: string;
}
export interface MissingIndex {
table: string;
columns: string[];
reason: string;
estimatedImpact: 'low' | 'medium' | 'high';
suggestedSql: string;
performanceGain: string;
}
export interface DuplicateIndex {
indexes: string[];
table: string;
columns: string[];
wastedSpace: string;
recommendation: string;
}
export interface OversizedIndex {
name: string;
table: string;
size: string;
suggestion: string;
optimizationPotential: string;
}
export interface PerformanceIssue {
type: 'slow_query' | 'table_bloat' | 'lock_contention' | 'poor_statistics' | 'inefficient_triggers' | 'low_cache_hit' | 'low_index_hit' | 'poor_index_usage';
severity: 'low' | 'medium' | 'high' | 'critical';
description: string;
impact: string;
solution: string;
sqlFix?: string;
estimatedImprovement: string;
}
export interface OptimizationRecommendation {
category: 'performance' | 'storage' | 'maintenance' | 'security';
priority: 'low' | 'medium' | 'high' | 'critical';
title: string;
description: string;
estimatedImpact: string;
implementation: string;
sqlCommands?: string[];
timeToImplement: string;
riskLevel: 'low' | 'medium' | 'high';
}
export interface DatabaseCostAnalysis {
storageUsage: {
totalSize: string;
dataSize: string;
indexSize: string;
wastedSpace: string;
};
estimatedCosts: {
storage: number;
compute: number;
maintenance: number;
};
optimizationSavings: {
storage: number;
performance: number;
monthly: number;
};
}
export interface MaintenanceRecommendation {
task: string;
frequency: 'daily' | 'weekly' | 'monthly' | 'quarterly';
importance: 'low' | 'medium' | 'high' | 'critical';
description: string;
command?: string;
automation: string;
}
export interface PartitionInfo {
table: string;
partitionStrategy: string;
partitionCount: number;
effectiveness: 'good' | 'poor' | 'excellent';
}
export interface LargeTableInfo {
name: string;
size: string;
rowCount: number;
recommendations: string[];
}
export declare class EnhancedDatabaseHealthAuditor {
private client;
private aiEnabled;
private openaiApiKey?;
private openaiModel?;
private openaiTemperature?;
constructor(client: Client, options?: {
enableAI?: boolean;
openaiApiKey?: string;
openaiModel?: string;
openaiTemperature?: number;
});
/**
* Perform comprehensive enhanced database health audit
*/
performComprehensiveAudit(): Promise<EnhancedDatabaseHealthReport>;
/**
* Get comprehensive database information including triggers and procedures
*/
private getDatabaseInfo;
/**
* Analyze tables for bloat, partitioning, and other issues
*/
private analyzeTables;
/**
* Analyze triggers for performance impact
*/
private analyzeTriggers;
private estimateTriggerImpact;
/**
* Analyze stored procedures and functions
*/
private analyzeProcedures;
private estimateProcedureComplexity;
/**
* Comprehensive security analysis including RLS policies
*/
private analyzeSecurityAndRLS;
private evaluatePolicyEffectiveness;
private analyzeSchemaHealth;
private analyzeIndexes;
private detectPerformanceIssues;
private analyzeCosts;
private generateOptimizationRecommendations;
private generateMaintenanceRecommendations;
/**
* Generate AI insights using OpenAI API
*/
private generateAIInsights;
}
//# sourceMappingURL=enhanced-database-auditor.d.ts.map