18 lines
995 B
SQL
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);
|