goon/app/scheduler/taxonomy_counts.py
jtrzupek 2163fee245 perf(taxonomy): denormalize scene_count for tags/performers/studios
Counts for /tags, /performers, /studios and /favorites were computed live
per-request by aggregating scene_tags / scene_performers with an EXISTS to
playback_sources. As the catalog grew to ~1.7M scenes (6.3M scene_tags) this
ran ~4.3s for /tags?order=popular (x2 incl. the total count) and ~950ms for
the default /scenes count, making those screens load in several seconds.

- migration 0019: add scene_count (+ DESC index) to tags/performers/studios
- background job _job_refresh_taxonomy_counts (every 3h) recomputes the counts
  in one UPDATE..FROM each (IS DISTINCT FROM to skip unchanged rows)
- /tags, /performers, /studios scenes path now read the column + ORDER BY the
  indexed scene_count; for_movies paths keep live aggregation (small tables)
- favorites read denormalized scene_count instead of a grouped EXISTS aggregate
- /scenes default count: 10-min in-process TTL cache (header is approximate)

Measured: /tags?order=popular&per_page=500 ~8s -> 66ms incl. serialization.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-05-31 17:53:48 +02:00

89 lines
2.9 KiB
Python

"""Refresh denormalizowanych `scene_count` na tags / performers / studios.
Liczniki są utrzymywane w tle (zamiast liczone per-request) bo agregacja po 6.3M
scene_tags / 3M scene_performers z EXISTS do 1.15M playback_sources zajmuje ~4.3s —
nie do zaakceptowania w hot-path UI (/tags, /performers, /studios, /favorites).
Definicja (identyczna z dotychczasowym has_live_playback filtrem w taxonomies.py):
scene_count = liczba scen z danym tagiem/performerem/studiem mających ≥1
playback_source z dead_at IS NULL.
Każdy UPDATE robi pełny LEFT JOIN (tag/performer/studio) ⨝ agregat → ustawia 0 dla
sierot. `IS DISTINCT FROM` pomija przepisywanie niezmienionych wierszy (mniej WAL/bloat).
Całość ~5-10s, leci co kilka godzin — counts do tego stale, co dla sortu "popular" i
badge "(N)" jest bez znaczenia.
"""
from __future__ import annotations
import logging
from sqlalchemy import text
from app.db import session_scope
log = logging.getLogger(__name__)
# Wspólny predykat: scena ma ≥1 żywy playback_source.
_LIVE = (
"EXISTS (SELECT 1 FROM playback_sources ps "
"WHERE ps.scene_id = {scene_col} AND ps.dead_at IS NULL)"
)
_TAGS_SQL = text(
f"""
UPDATE tags t SET scene_count = COALESCE(a.c, 0)
FROM tags base
LEFT JOIN (
SELECT st.tag_id, count(*) AS c
FROM scene_tags st
WHERE {_LIVE.format(scene_col="st.scene_id")}
GROUP BY st.tag_id
) a ON a.tag_id = base.id
WHERE t.id = base.id AND t.scene_count IS DISTINCT FROM COALESCE(a.c, 0)
"""
)
_PERFORMERS_SQL = text(
f"""
UPDATE performers p SET scene_count = COALESCE(a.c, 0)
FROM performers base
LEFT JOIN (
SELECT sp.performer_id, count(*) AS c
FROM scene_performers sp
WHERE {_LIVE.format(scene_col="sp.scene_id")}
GROUP BY sp.performer_id
) a ON a.performer_id = base.id
WHERE p.id = base.id AND p.scene_count IS DISTINCT FROM COALESCE(a.c, 0)
"""
)
_STUDIOS_SQL = text(
f"""
UPDATE studios s SET scene_count = COALESCE(a.c, 0)
FROM studios base
LEFT JOIN (
SELECT sc.studio_id, count(*) AS c
FROM scenes sc
WHERE sc.studio_id IS NOT NULL AND {_LIVE.format(scene_col="sc.id")}
GROUP BY sc.studio_id
) a ON a.studio_id = base.id
WHERE s.id = base.id AND s.scene_count IS DISTINCT FROM COALESCE(a.c, 0)
"""
)
def refresh_taxonomy_counts() -> dict[str, int]:
"""Przelicza scene_count dla tags/performers/studios. Zwraca rowcount per tabela
(ile wierszy faktycznie się zmieniło)."""
out: dict[str, int] = {}
with session_scope() as session:
for name, stmt in (
("tags", _TAGS_SQL),
("performers", _PERFORMERS_SQL),
("studios", _STUDIOS_SQL),
):
res = session.execute(stmt)
out[name] = res.rowcount or 0
# Commit per-tabela — długie transakcje trzymałyby locki na hot tables.
session.commit()
return out