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:
@@ -1761,19 +1761,25 @@ retries) is individually logged for full observability.
|
||||
|
||||
#### Event-tier indexes
|
||||
|
||||
Beyond the primary keys, the per-webhook event databases carry secondary
|
||||
indexes on the columns the background work reads by, each created by
|
||||
`AutoMigrate` on a fresh and on an existing database:
|
||||
These indexes on the per-webhook event databases are declared in the model
|
||||
tags, so `AutoMigrate` creates them on a fresh and on an existing database:
|
||||
|
||||
| Column | Serves |
|
||||
| ------------------------------ | ------ |
|
||||
| `deliveries.status` | The recovery and sweep queries that select deliveries by status once a minute |
|
||||
| `deliveries.event_id` | Loading a page of the event log, which reads deliveries by event |
|
||||
| `delivery_results.delivery_id` | Loading a page of the event log, which reads results by delivery |
|
||||
| `events.created_at` | Retention, which deletes events by age |
|
||||
| Table | Columns | Serves |
|
||||
| ------------------ | --------------------------- | ------ |
|
||||
| `deliveries` | `status`, `deleted_at` | Startup recovery, the retry and pending sweeps every 60 seconds and the queue-depth sampler every 30 seconds, which select deliveries by status |
|
||||
| `deliveries` | `event_id`, `deleted_at` | The event log, which loads each event's deliveries, and retention, which selects and deletes the deliveries of expired events |
|
||||
| `delivery_results` | `delivery_id`, `deleted_at` | The event log, which loads the attempts of a page's deliveries, and retention, which deletes the attempts of expired events |
|
||||
| `events` | `deleted_at`, `created_at` | Retention, which selects expired events by age |
|
||||
| `events` | `created_at` | Retention's delete of the expired events themselves |
|
||||
|
||||
The `events.resubmitted_from_id` column is also indexed, to resolve the
|
||||
resubmit relationship both ways in the event log.
|
||||
GORM's soft delete adds `deleted_at IS NULL` to these queries; retention's
|
||||
deletes leave it out, but their lookups of expired rows keep it. SQLite keeps
|
||||
no statistics on these tables, and without them it rates the `deleted_at`
|
||||
index, which every live row matches, above an index on a column matched
|
||||
against several values or compared with `<`. So every index but the last also
|
||||
covers `deleted_at`. It comes second, so that retention's deletes can use the
|
||||
index without it, except in `events`, where `created_at` is compared with `<`
|
||||
and SQLite narrows by a `<` only on the last column it uses.
|
||||
|
||||
#### Common Fields
|
||||
|
||||
|
||||
Reference in New Issue
Block a user