migration(0021): raise scene_tags.tag_id statistics target to 1000

Completes the literal-tag_id perf fix — the planner's MCV stats on tag_id are what
let it pick the index-walk for common tags. Default target (100) covers only the
top ~100 tags; 1000 extends correct cardinality estimates to mid-tier tags.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
jtrzupek 2026-06-07 21:12:22 +02:00
parent 43f7e1f7b2
commit c5abdc1186

View file

@ -0,0 +1,31 @@
"""scene_tags.tag_id statistics target — MCV captures common tags
Revision ID: 0021_scene_tags_tag_id_stats
Revises: 0020_scene_tags_tag_id_index
Create Date: 2026-06-07
Perf (2026-06-07): dopełnia 0020. Z literalnym tag_id (scenes.py resolvuje slugid)
planner używa statystyk MCV kolumny scene_tags.tag_id by poznać kardynalność tagu i
wybrać index-walk po created_at zamiast materializować wszystkie scene_tags
(tag-filtered listy 4-12s ~20ms). Domyślny statistics target (100) łapie tylko
~top-100 tagów; podnosimy do 1000 by mid-tier tagi też miały poprawne estymaty.
ANALYZE NIE w migracji (ciężki/lockujący) autovacuum przeliczy po zmianie targetu;
na prodzie wykonano ANALYZE ręcznie przy wdrożeniu.
"""
from collections.abc import Sequence
from alembic import op
revision: str = "0021_scene_tags_tag_id_stats"
down_revision: str | None = "0020_scene_tags_tag_id_index"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None
def upgrade() -> None:
op.execute("ALTER TABLE scene_tags ALTER COLUMN tag_id SET STATISTICS 1000")
def downgrade() -> None:
op.execute("ALTER TABLE scene_tags ALTER COLUMN tag_id SET STATISTICS -1")