From b260c20cf5af6fef2c8109f6af34bfa4f2df3c12 Mon Sep 17 00:00:00 2001 From: Peter Lord Date: Thu, 9 Jul 2026 22:08:51 -0700 Subject: [PATCH] Index run_hash so admin hide/delete/search by hash is instant Looking a run up by hash (admin search, hide, delete) matched on the run_hash field, which is unindexed, so every lookup was a full scan of the ~740k-doc runs collection (seconds). _id is the hash for single-player runs and is indexed; multiplayer runs carry a shared run_hash field. Add a sparse index on run_hash so the $or: [{_id}, {run_hash}] lookup (from the hide/delete fix) is an index union instead of a collection scan. Sparse because only multiplayer docs have the field, so the index stays small. --- backend/app/services/runs_db_mongo.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/backend/app/services/runs_db_mongo.py b/backend/app/services/runs_db_mongo.py index 6ba671dc..cd5fb1c1 100644 --- a/backend/app/services/runs_db_mongo.py +++ b/backend/app/services/runs_db_mongo.py @@ -165,6 +165,12 @@ def _ensure_indexes(coll) -> None: # /api/runs/list hot shapes: filter+sort compounds so Mongo never # in-memory-sorts a 40k-doc subset, plus seed for anchored prefix search. coll.create_index([("seed", ASCENDING)]) + # Admin hide/delete/search look runs up by hash. _id IS the hash for + # single-player runs; multiplayer runs carry a shared run_hash field. Index + # it (sparse — only MP docs have the field) so the admin lookup + # $or: [{_id}, {run_hash}] is an index union instead of a full scan of the + # ~740k-doc collection. + coll.create_index([("run_hash", ASCENDING)], sparse=True) coll.create_index([("run_time", ASCENDING)]) coll.create_index([("ascension", DESCENDING), ("run_time", ASCENDING)]) coll.create_index([("character", ASCENDING), ("submitted_at", DESCENDING)])