Files
shaguabijia-app-server/alembic/versions/analytics_active_idx_active_composite_index.py
T
guke fd1c1a5abc perf(analytics): analytics_event 活跃口径复合索引 (event,page,user_id,created_at)
active_event_condition 按 (event=show & page=home) ∪ 比价 ∪ 领券 过滤后 group by user_id、
max(created_at),是 worker 清零/预警 + admin 最近活跃的热点聚合;page 原无索引、show 又高频。
加覆盖索引 ix_analytics_event_active 让该聚合走 index-only。

⚠️ 分支迁移树既有多头(135e79414fd0 与 phone_rebind_log 同从 comparison_llm_cost 分叉,
`alembic upgrade head` 会多头报错)——既有问题、与本次无关。本迁移挂在 135e79414fd0 一侧,
dev 用 `alembic upgrade analytics_active_idx` 应用;集成 main 时需 alembic merge 合并 phone_rebind_log 头。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-18 18:31:15 +08:00

37 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""analytics_event 活跃口径复合索引
Revision ID: analytics_active_idx
Revises: 135e79414fd0
Create Date: 2026-07-18 17:35:00.000000
给 analytics_event 加活跃口径热点复合索引 (event, page, user_id, created_at):
activity.active_event_condition 按 (event=show & page=home) 比价 领券 过滤后
group by user_id、max(created_at)。覆盖索引让该聚合走 index-only,避免高频 show 事件全表扫。
⚠️ 本分支迁移树有**既有多头**:135e79414fd0(不活跃两表)与 phone_rebind_log 同从
comparison_llm_cost 分叉,`alembic upgrade head` 会多头报错。本迁移挂在 135e79414fd0
一侧;集成到 main 时需 `alembic merge` 合并 phone_rebind_log 那个头(与本迁移无关的既有问题)。
"""
from typing import Sequence, Union
from alembic import op
# revision identifiers, used by Alembic.
revision: str = "analytics_active_idx"
down_revision: Union[str, Sequence[str], None] = "135e79414fd0"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_index(
"ix_analytics_event_active",
"analytics_event",
["event", "page", "user_id", "created_at"],
unique=False,
)
def downgrade() -> None:
op.drop_index("ix_analytics_event_active", table_name="analytics_event")