perf: avoid full archive scans in statistics

This commit is contained in:
ImBenji
2026-08-04 21:46:56 +01:00
parent f5f092cfb0
commit d020fa3811
3 changed files with 17 additions and 14 deletions
+9 -6
View File
@@ -5,20 +5,23 @@ try {
const db = new Database(workerData.databasePath, { readonly: true, fileMustExist: true });
const counts = db.prepare(`
SELECT
(SELECT COUNT(*) FROM articles) AS total,
COALESCE((SELECT seq FROM sqlite_sequence WHERE name='articles'), 0) AS total,
(SELECT COUNT(*) FROM article_embedding_meta) AS withContent,
(SELECT COUNT(*) FROM article_embedding_meta) AS withEmbedding,
(SELECT COUNT(*) FROM events) AS eventCount,
COALESCE((SELECT seq FROM sqlite_sequence WHERE name='events'), 0) AS eventCount,
(SELECT COUNT(*) FROM articles WHERE ingested_at >= datetime('now', '-1 hour')) AS ingestedPerHour,
(SELECT COUNT(*) FROM article_embedding_meta WHERE embedded_at >= datetime('now', '-1 hour')) AS contentPerHour
`).get();
const bySource = db.prepare(`
SELECT source, COUNT(*) AS n FROM articles GROUP BY source ORDER BY n DESC
`).all();
const byStatus = db.prepare(`
SELECT COALESCE(content_status, 'null') AS status, COUNT(*) AS n
FROM articles GROUP BY content_status ORDER BY n DESC
`).all();
// Avoid scanning the 3.7 GB article table for legacy content-status values.
// Readiness is the useful operational distinction and is available from the
// compact embedding metadata table.
const byStatus = [
{ status: 'ready', n: counts.withEmbedding },
{ status: 'unprocessed', n: Math.max(0, counts.total - counts.withEmbedding) },
];
let embeddingsPerHour = 0;
try {
embeddingsPerHour = db.prepare(`