Put deleted_at in the event-tier indexes so SQLite uses them
check / check (push) Successful in 3m16s
check / check (push) Successful in 3m16s
GORM adds deleted_at IS NULL to these queries, and SQLite, which has no table statistics here, preferred the deleted_at index to the new single-column ones wherever a column is matched against several values or compared with <. The event log's attempt load, retention's lookups of expired events and their deliveries, and the queue-depth sampler still read every live row. Each index now also covers deleted_at, first in the events index because created_at is compared with <; events keeps its created_at index for retention's final delete. A test checks SQLite's plan for each statement as GORM builds it. Model: opus-5-5
This commit is contained in:
@@ -2,37 +2,33 @@ package database_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
"sneak.berlin/go/webhooker/internal/database"
|
||||
)
|
||||
|
||||
// indexedColumn names a secondary index by the model and struct field
|
||||
// GORM derives the index name from.
|
||||
type indexedColumn struct {
|
||||
model any
|
||||
field string
|
||||
}
|
||||
|
||||
// TestWebhookDBManager_OpenAddsEventTierIndexes verifies that opening a
|
||||
// per-webhook database that predates these indexes creates them, so the
|
||||
// queries that read by those columns stop scanning whole tables. It
|
||||
// per-webhook database that predates these indexes creates them. It
|
||||
// stands in for an older database file by dropping the indexes
|
||||
// AutoMigrate just created, then reopening the same file.
|
||||
func TestWebhookDBManager_OpenAddsEventTierIndexes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
// The columns the background work reads by: the recovery and sweep
|
||||
// queries (status), the event log (event_id and delivery_id) and
|
||||
// retention (created_at).
|
||||
eventTierIndexes := []indexedColumn{
|
||||
{&database.Delivery{}, "Status"},
|
||||
{&database.Delivery{}, "EventID"},
|
||||
{&database.DeliveryResult{}, "DeliveryID"},
|
||||
{&database.Event{}, "CreatedAt"},
|
||||
indexes := []struct {
|
||||
model any
|
||||
name string
|
||||
}{
|
||||
{&database.Delivery{}, "idx_deliveries_status"},
|
||||
{&database.Delivery{}, "idx_deliveries_event_id"},
|
||||
{&database.DeliveryResult{}, "idx_delivery_results_delivery_id"},
|
||||
{&database.Event{}, "idx_events_deleted_at_created_at"},
|
||||
{&database.Event{}, "idx_events_created_at"},
|
||||
}
|
||||
|
||||
mgr, lc := setupTestWebhookDBManager(t)
|
||||
@@ -47,14 +43,14 @@ func TestWebhookDBManager_OpenAddsEventTierIndexes(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// A fresh database has them.
|
||||
for _, ix := range eventTierIndexes {
|
||||
require.True(t, db.Migrator().HasIndex(ix.model, ix.field))
|
||||
for _, ix := range indexes {
|
||||
require.True(t, db.Migrator().HasIndex(ix.model, ix.name))
|
||||
}
|
||||
|
||||
// Stand in for a database file created before the indexes existed.
|
||||
for _, ix := range eventTierIndexes {
|
||||
require.NoError(t, db.Migrator().DropIndex(ix.model, ix.field))
|
||||
require.False(t, db.Migrator().HasIndex(ix.model, ix.field))
|
||||
for _, ix := range indexes {
|
||||
require.NoError(t, db.Migrator().DropIndex(ix.model, ix.name))
|
||||
require.False(t, db.Migrator().HasIndex(ix.model, ix.name))
|
||||
}
|
||||
|
||||
// Drop the cached connection so the next open reopens the file and
|
||||
@@ -64,9 +60,107 @@ func TestWebhookDBManager_OpenAddsEventTierIndexes(t *testing.T) {
|
||||
db, err = mgr.GetDB(webhookID)
|
||||
require.NoError(t, err)
|
||||
|
||||
for _, ix := range eventTierIndexes {
|
||||
assert.True(t, db.Migrator().HasIndex(ix.model, ix.field),
|
||||
"opening the existing database should create the index on %s",
|
||||
ix.field)
|
||||
for _, ix := range indexes {
|
||||
assert.True(t, db.Migrator().HasIndex(ix.model, ix.name),
|
||||
"opening the existing database should create %s", ix.name)
|
||||
}
|
||||
}
|
||||
|
||||
// TestEventTierQueriesUseTheirIndexes verifies that the statements the
|
||||
// indexes are for use them. GORM builds each statement in a dry run as
|
||||
// the code named above it does, soft-delete condition included, and
|
||||
// SQLite, which keeps no statistics on these tables, must plan to seek
|
||||
// on each index listed by the columns in parentheses.
|
||||
func TestEventTierQueriesUseTheirIndexes(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
mgr, lc := setupTestWebhookDBManager(t)
|
||||
ctx := context.Background()
|
||||
require.NoError(t, lc.Start(ctx))
|
||||
|
||||
defer func() { require.NoError(t, lc.Stop(ctx)) }()
|
||||
|
||||
db, err := mgr.GetDB(uuid.New().String())
|
||||
require.NoError(t, err)
|
||||
|
||||
dry := db.Session(&gorm.Session{DryRun: true})
|
||||
ids := []string{
|
||||
uuid.New().String(), uuid.New().String(), uuid.New().String(),
|
||||
}
|
||||
cutoff := time.Now()
|
||||
|
||||
var (
|
||||
deliveries []database.Delivery
|
||||
results []database.DeliveryResult
|
||||
depths []struct{ Depth int }
|
||||
)
|
||||
|
||||
byStatus := "idx_deliveries_status (status=? AND deleted_at=?)"
|
||||
byEvent := "idx_deliveries_event_id (event_id=? AND deleted_at=?)"
|
||||
byAge := "idx_events_deleted_at_created_at (deleted_at=? AND created_at<?)"
|
||||
|
||||
// The delivery engine: recovery and the retry sweep, the sweep for
|
||||
// stranded pending deliveries, and the queue depth count.
|
||||
assertPlanUses(t, db, dry.Where(
|
||||
"status = ?", database.DeliveryStatusRetrying,
|
||||
).Find(&deliveries), byStatus)
|
||||
assertPlanUses(t, db, dry.Where(
|
||||
"status = ? AND updated_at < ?",
|
||||
database.DeliveryStatusPending, cutoff,
|
||||
).Limit(500).Find(&deliveries), byStatus)
|
||||
assertPlanUses(t, db, dry.Model(&database.Delivery{}).
|
||||
Select("target_id", "status", "count(*) as depth").
|
||||
Where("status IN ?", []database.DeliveryStatus{
|
||||
database.DeliveryStatusPending,
|
||||
database.DeliveryStatusRetrying,
|
||||
}).Group("target_id, status").Find(&depths), byStatus)
|
||||
|
||||
// The event log: each event's deliveries, then their attempts
|
||||
// (loadEventsWithDeliveries, loadDeliveryResults).
|
||||
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=?)")
|
||||
|
||||
// Retention's three deletes (reapExpired), whose subqueries are built
|
||||
// afresh for each statement as it builds them.
|
||||
expiredEventIDs := func() *gorm.DB {
|
||||
return dry.Model(&database.Event{}).Select("id").
|
||||
Where("created_at < ?", cutoff)
|
||||
}
|
||||
|
||||
assertPlanUses(t, db, dry.Unscoped().Where(
|
||||
"delivery_id IN (?)", dry.Model(&database.Delivery{}).
|
||||
Select("id").Where("event_id IN (?)", expiredEventIDs()),
|
||||
).Delete(&database.DeliveryResult{}),
|
||||
"idx_delivery_results_delivery_id (delivery_id=?)", byEvent, byAge)
|
||||
assertPlanUses(t, db, dry.Unscoped().Where(
|
||||
"event_id IN (?)", expiredEventIDs(),
|
||||
).Delete(&database.Delivery{}),
|
||||
"idx_deliveries_event_id (event_id=?)", byAge)
|
||||
assertPlanUses(t, db, dry.Unscoped().Where(
|
||||
"created_at < ?", cutoff,
|
||||
).Delete(&database.Event{}), "idx_events_created_at (created_at<?)")
|
||||
}
|
||||
|
||||
// assertPlanUses asserts that SQLite's plan for a statement GORM built
|
||||
// in a dry run, run with the same SQL and arguments GORM would send,
|
||||
// names each of the given indexes.
|
||||
func assertPlanUses(
|
||||
t *testing.T, db, built *gorm.DB, indexes ...string,
|
||||
) {
|
||||
t.Helper()
|
||||
|
||||
var plan []struct{ Detail string }
|
||||
|
||||
require.NoError(t, db.Raw(
|
||||
"EXPLAIN QUERY PLAN "+built.Statement.SQL.String(),
|
||||
built.Statement.Vars...,
|
||||
).Scan(&plan).Error)
|
||||
|
||||
for _, index := range indexes {
|
||||
assert.Contains(t, fmt.Sprint(plan), index,
|
||||
built.Statement.SQL.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user