mirror of
https://github.com/djdevin/recflare.git
synced 2026-10-25 23:18:51 -07:00
[api] improve invention query
This commit is contained in:
@@ -0,0 +1,17 @@
|
|||||||
|
-- Index the browse/search feed (`searchInventions`) in the order it pages: newest first.
|
||||||
|
-- Without it every page — however small — parsed the JSON of every published invention,
|
||||||
|
-- sorted the lot in a temp B-tree and threw all but one page away. With it the query walks
|
||||||
|
-- this index in order and stops after OFFSET + LIMIT rows.
|
||||||
|
--
|
||||||
|
-- PARTIAL, over exactly the rows `VISIBLE_IN_FEEDS` admits, so it holds only what a feed can
|
||||||
|
-- show. SQLite uses a partial index only when the query's WHERE contains each of these terms
|
||||||
|
-- as written, so this WHERE must track `VISIBLE_IN_FEEDS` in src/inventions-db.ts term for
|
||||||
|
-- term — change one and the index silently stops being used. The key is the same
|
||||||
|
-- `json_extract` expression the query orders by, which is what lets it replace the sort.
|
||||||
|
-- Generated from src/inventions-db.ts (SCHEMA_DDL) — keep in sync.
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_invention_feed_newest
|
||||||
|
ON invention (json_extract(data, '$.CreatedAt') DESC, id DESC)
|
||||||
|
WHERE is_published = 1
|
||||||
|
AND hide_from_player = 0
|
||||||
|
AND COALESCE(json_extract(data, '$.Accessibility'), 0) <> 2;
|
||||||
@@ -39,6 +39,13 @@ export const SCHEMA_DDL: string[] = [
|
|||||||
`CREATE UNIQUE INDEX IF NOT EXISTS idx_invention_id ON invention (id)`,
|
`CREATE UNIQUE INDEX IF NOT EXISTS idx_invention_id ON invention (id)`,
|
||||||
`CREATE INDEX IF NOT EXISTS idx_invention_creator ON invention (creator_player_id)`,
|
`CREATE INDEX IF NOT EXISTS idx_invention_creator ON invention (creator_player_id)`,
|
||||||
`CREATE INDEX IF NOT EXISTS idx_invention_featured ON invention (is_featured)`,
|
`CREATE INDEX IF NOT EXISTS idx_invention_featured ON invention (is_featured)`,
|
||||||
|
// Backs `searchInventions`' newest-first paging (migrations/0027). Its WHERE must match
|
||||||
|
// `VISIBLE_IN_FEEDS` term for term or SQLite won't use it.
|
||||||
|
`CREATE INDEX IF NOT EXISTS idx_invention_feed_newest
|
||||||
|
ON invention (json_extract(data, '$.CreatedAt') DESC, id DESC)
|
||||||
|
WHERE is_published = 1
|
||||||
|
AND hide_from_player = 0
|
||||||
|
AND COALESCE(json_extract(data, '$.Accessibility'), 0) <> 2`,
|
||||||
`CREATE TABLE IF NOT EXISTS invention_interaction (
|
`CREATE TABLE IF NOT EXISTS invention_interaction (
|
||||||
player_id INTEGER NOT NULL,
|
player_id INTEGER NOT NULL,
|
||||||
invention_id INTEGER NOT NULL,
|
invention_id INTEGER NOT NULL,
|
||||||
@@ -873,6 +880,9 @@ export const INVENTION_ACCESSIBILITY = {
|
|||||||
* The "anyone may come across this" test the browse feeds and search share: published, not
|
* The "anyone may come across this" test the browse feeds and search share: published, not
|
||||||
* hidden, and not unlisted. An unlisted invention is still reachable BY ID — that is what
|
* hidden, and not unlisted. An unlisted invention is still reachable BY ID — that is what
|
||||||
* unlisted means — so the by-id reads deliberately don't apply it.
|
* unlisted means — so the by-id reads deliberately don't apply it.
|
||||||
|
*
|
||||||
|
* `idx_invention_feed_newest` (SCHEMA_DDL, migrations/0027) is a partial index over exactly
|
||||||
|
* these terms — edit one here and edit it there, or the feed goes back to a full scan.
|
||||||
*/
|
*/
|
||||||
const VISIBLE_IN_FEEDS = [
|
const VISIBLE_IN_FEEDS = [
|
||||||
'is_published = 1',
|
'is_published = 1',
|
||||||
|
|||||||
Reference in New Issue
Block a user