Enhance database performance diagnostics: Add detailed logging for unused primary key index and implement error handling for query statistics retrieval. Additionally, automate ANALYZE execution for affected tables after index creation to ensure PostgreSQL optimizes query performance. This improves clarity on index usage and enhances overall database performance management.

This commit is contained in:
Torsten Schulz (local)
2026-01-29 14:05:24 +01:00
parent d1ddfe7d31
commit cb2631061e
2 changed files with 34 additions and 7 deletions

View File

@@ -86,8 +86,19 @@ async function main() {
});
console.log('');
// Erklärung: Warum knowledge_pkey ungenutzt ist
const pkUnused = allIndexes.find(i => i.indexname === 'knowledge_pkey' && (i.idx_scan == null || parseInt(i.idx_scan) === 0));
if (pkUnused) {
console.log('💡 Warum knowledge_pkey (0 Scans) ungenutzt ist:');
console.log(' Alle Zugriffe filtern nach (character_id, product_id), nie nach id.');
console.log(' Der PK-Index wird nur für Eindeutigkeit/Referenzen genutzt, nicht für Lookups.');
console.log(' idx_knowledge_character_product deckt die tatsächlichen Queries ab.\n');
}
// Prüfe ob Queries mit id (Primary Key) gemacht werden
const [idUsage] = await sequelize.query(`
let idUsage = [];
try {
const [rows] = await sequelize.query(`
SELECT
query,
calls,
@@ -99,6 +110,10 @@ async function main() {
ORDER BY calls DESC
LIMIT 5;
`);
idUsage = rows;
} catch (e) {
console.log(' pg_stat_statements nicht verfügbar keine Query-Statistik.\n');
}
if (idUsage.length > 0) {
console.log('🔍 Queries die knowledge.id verwenden:');

View File

@@ -125,13 +125,25 @@ async function main() {
console.log(` Übersprungen: ${skipped}`);
console.log(` Fehler: ${errors}\n`);
// ANALYZE ausführen, damit PostgreSQL die neuen Indizes berücksichtigt
const tablesToAnalyze = [
'falukant_data.knowledge',
'falukant_data.inventory',
'falukant_data.stock',
'falukant_data.production',
'falukant_data.director'
];
if (created > 0) {
console.log('💡 Tipp: Führe ANALYZE aus, damit PostgreSQL die neuen Indizes berücksichtigt:');
console.log(' ANALYZE falukant_data.knowledge;');
console.log(' ANALYZE falukant_data.inventory;');
console.log(' ANALYZE falukant_data.stock;');
console.log(' ANALYZE falukant_data.production;');
console.log(' ANALYZE falukant_data.director;\n');
console.log('📊 Führe ANALYZE auf betroffenen Tabellen aus...\n');
for (const table of tablesToAnalyze) {
try {
await sequelize.query(`ANALYZE ${table};`);
console.log(` ANALYZE ${table};`);
} catch (err) {
console.log(` ⚠️ ${table}: ${err.message}`);
}
}
console.log('');
}
await sequelize.close();