fix(douyu): 优化供应商直充会话与任务时间索引
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
"""add douyu_tasks.created_at index
|
||||
|
||||
历史任务清理和按时间范围统计都以 created_at 为条件。表已接近 50 万行,
|
||||
没有索引时清理作业需要扫描约 20 万行;单列索引即可覆盖这些低频但重扫描的查询。
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "20261119_0099"
|
||||
down_revision: str | None = "20261119_0098"
|
||||
branch_labels: str | None = None
|
||||
depends_on: str | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
indexes = {item["name"] for item in inspector.get_indexes("douyu_tasks")}
|
||||
if "ix_douyu_tasks_created_at" not in indexes:
|
||||
op.create_index(
|
||||
"ix_douyu_tasks_created_at",
|
||||
"douyu_tasks",
|
||||
["created_at"],
|
||||
unique=False,
|
||||
)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
indexes = {item["name"] for item in inspector.get_indexes("douyu_tasks")}
|
||||
if "ix_douyu_tasks_created_at" in indexes:
|
||||
op.drop_index("ix_douyu_tasks_created_at", table_name="douyu_tasks")
|
||||
@@ -356,7 +356,9 @@ class DouyuTask(Base):
|
||||
message: Mapped[str] = mapped_column(String(512), default="")
|
||||
result: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
created_by: Mapped[int] = mapped_column(ForeignKey("users.id"), nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=_utcnow)
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
UTCDateTime, default=_utcnow, index=True
|
||||
)
|
||||
finished_at: Mapped[datetime | None] = mapped_column(UTCDateTime, nullable=True)
|
||||
platform_task_id: Mapped[int | None] = mapped_column(
|
||||
BigInteger,
|
||||
|
||||
Reference in New Issue
Block a user