Index the event log's resubmit count with deleted_at (closes #325)
check / check (push) Failing after 1m10s

The resubmit count matches resubmitted_from_id against a page of event
ids, and GORM adds deleted_at IS NULL. With no statistics, SQLite
preferred the deleted_at index once the page held five or more ids, so
every event log page read every live event. deleted_at now joins
resubmitted_from_id in idx_events_resubmitted_from_id, as for the other
event-tier indexes, and the plan test checks the statement with a full
page of ids.

Model: opus-5-5
This commit is contained in:
2026-10-02 18:37:01 +00:00
parent 9526e961b5
commit 84c00e771b
3 changed files with 27 additions and 7 deletions
+18 -1
View File
@@ -93,6 +93,7 @@ func TestEventTierQueriesUseTheirIndexes(t *testing.T) {
deliveries []database.Delivery
results []database.DeliveryResult
depths []struct{ Depth int }
resubmits []struct{ Total int }
removed []database.TargetTotals
)
@@ -116,13 +117,29 @@ func TestEventTierQueriesUseTheirIndexes(t *testing.T) {
}).Group("target_id, status").Find(&depths), byStatus)
// The event log: each event's deliveries, then their attempts
// (loadEventsWithDeliveries, loadDeliveryResults).
// (loadEventsWithDeliveries, loadDeliveryResults), and how many
// events were resubmitted from each of the page's (resubmitCounts).
assertPlanUses(t, db, dry.Where("event_id = ?", ids[0]).
Find(&deliveries), byEvent)
assertPlanUses(t, db, dry.Where("delivery_id IN ?", ids).
Order("attempt_num ASC").Find(&results),
"idx_delivery_results_delivery_id (delivery_id=? AND deleted_at=?)")
// The resubmit count gets a full page of 25 ids: with an index on
// resubmitted_from_id alone, SQLite still uses it for three ids and
// turns to the deleted_at index only from five.
page := make([]string, 25)
for i := range page {
page[i] = uuid.New().String()
}
assertPlanUses(t, db, dry.Model(&database.Event{}).
Select("resubmitted_from_id, count(*) AS total").
Where("resubmitted_from_id IN ?", page).
Group("resubmitted_from_id").Find(&resubmits),
"idx_events_resubmitted_from_id "+
"(resubmitted_from_id=? AND deleted_at=?)")
// Retention (reapExpired, deleteEvents): one batch of expired
// events, then their attempts, deliveries and the events.
var expired []string