fix: real root cause - ORDER BY id forced a full table scan every batch, use received_at (indexed) instead

This commit is contained in:
2026-08-22 13:42:25 +02:00
parent 7c62de0a8a
commit 40b0816440
+2 -2
View File
@@ -86,13 +86,13 @@ const stmtSummarizeBatch = DB.prepare(`
INSERT INTO daily_summary (day, site_id, bot_type, action, country, count)
SELECT strftime('%Y-%m-%d', received_at, 'unixepoch'), site_id, bot_type, action, country, COUNT(*)
FROM bots
WHERE id IN (SELECT id FROM bots WHERE received_at < ? ORDER BY id LIMIT ?)
WHERE id IN (SELECT id FROM bots WHERE received_at < ? ORDER BY received_at LIMIT ?)
GROUP BY 1, 2, 3, 4, 5
ON CONFLICT (day, site_id, bot_type, action, country)
DO UPDATE SET count = count + excluded.count
`);
const stmtDeleteBatch = DB.prepare(
'DELETE FROM bots WHERE id IN (SELECT id FROM bots WHERE received_at < ? ORDER BY id LIMIT ?)'
'DELETE FROM bots WHERE id IN (SELECT id FROM bots WHERE received_at < ? ORDER BY received_at LIMIT ?)'
);
function pruneOldRowsBatch(cutoff) {