Files
thamanyah/discovery/internal/db/migrations/0005_add_video_recency_index.up.sql
T
FahdShalhoub c3465bedb1
Build, Push and Deploy Discovery / build-push-deploy (push) Successful in 1m57s
FEAT: Video Search In Discovery
2026-08-29 17:56:11 +03:00

18 lines
995 B
SQL

-- Browsing the catalogue, and paging through it.
--
-- Every search orders by (rank, created_at, id). When there is no title term
-- the rank is a constant, and Postgres folds a constant sort key away — so the
-- order that remains is exactly this index, and a browse becomes an index-only
-- scan that stops as soon as the page is full instead of sorting the table.
--
-- Measured at 200k rows: a first page of a browse goes from a 15 ms parallel
-- sequential scan with a top-N sort to 0.03 ms. It matters more for the cursor
-- than for the first page — the keyset comparison (created_at, id) < (…)
-- becomes an index condition rather than a filter, so a deep page is a seek
-- to the right place in the index rather than a walk through everything
-- before it.
--
-- DESC on both columns to match the ORDER BY: a btree can be read backwards,
-- but only as a whole, so a mixed-direction ordering could not use it.
CREATE INDEX videos_recent_idx ON videos (created_at DESC, id DESC);