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>
62 lines
2.7 KiB
Python
62 lines
2.7 KiB
Python
import uuid
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import DateTime, Float, ForeignKey, Integer, String, UniqueConstraint, func
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, mapped_column
|
|
|
|
from app.models.base import Base, TimestampMixin, UUIDPKMixin
|
|
|
|
|
|
class Studio(UUIDPKMixin, TimestampMixin, Base):
|
|
__tablename__ = "studios"
|
|
|
|
name: Mapped[str] = mapped_column(String(256), nullable=False)
|
|
name_normalized: Mapped[str] = mapped_column(String(256), nullable=False, index=True)
|
|
slug: Mapped[str] = mapped_column(String(256), nullable=False, unique=True)
|
|
parent_studio_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("studios.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
network: Mapped[str | None] = mapped_column(String(256))
|
|
homepage_url: Mapped[str | None] = mapped_column(String(512))
|
|
# Denormalizowany licznik scen z żywym playback (refresh w tle). Patrz migracja
|
|
# 0019 + _job_refresh_taxonomy_counts. Sortowanie "popular" + badge w favorites.
|
|
scene_count: Mapped[int] = mapped_column(
|
|
Integer, nullable=False, default=0, server_default="0"
|
|
)
|
|
|
|
|
|
class StudioAlias(Base):
|
|
__tablename__ = "studio_aliases"
|
|
__table_args__ = (UniqueConstraint("studio_id", "alias_normalized"),)
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), primary_key=True, server_default=func.gen_random_uuid()
|
|
)
|
|
studio_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("studios.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
alias: Mapped[str] = mapped_column(String(256), nullable=False)
|
|
alias_normalized: Mapped[str] = mapped_column(String(256), nullable=False, index=True)
|
|
source_id: Mapped[uuid.UUID | None] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="SET NULL"), nullable=True
|
|
)
|
|
|
|
|
|
class StudioExternalRef(Base):
|
|
__tablename__ = "studio_external_refs"
|
|
|
|
source_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("sources.id", ondelete="CASCADE"), primary_key=True
|
|
)
|
|
external_id: Mapped[str] = mapped_column(String, primary_key=True)
|
|
studio_id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True), ForeignKey("studios.id", ondelete="CASCADE"), nullable=False, index=True
|
|
)
|
|
confidence: Mapped[float] = mapped_column(Float, nullable=False, default=1.0, server_default="1.0")
|
|
first_seen: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|
|
last_seen: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True), server_default=func.now(), nullable=False
|
|
)
|