perf: 优化 MySQL 查询并接入 pytest
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
"""Add composite indexes for task polling, ordering, and retention queries.
|
||||
|
||||
Revision ID: 20260830_0031
|
||||
Revises: 20260829_0030
|
||||
Create Date: 2026-08-30
|
||||
"""
|
||||
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
|
||||
revision: str = "20260830_0031"
|
||||
down_revision: Union[str, None] = "20260829_0030"
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
INDEXES = (
|
||||
# /api/douyu/tasks filters by scope and returns the newest rows first.
|
||||
("ix_douyu_tasks_handbook_scope_id", "douyu_tasks", ["handbook_scope", "id"]),
|
||||
# The legacy compatibility branch also filters by task_type.
|
||||
(
|
||||
"ix_douyu_tasks_handbook_scope_task_type_id",
|
||||
"douyu_tasks",
|
||||
["handbook_scope", "task_type", "id"],
|
||||
),
|
||||
# Supports terminal-task retention and finished-time ordered Cookie views.
|
||||
("ix_douyu_tasks_status_finished_at_id", "douyu_tasks", ["status", "finished_at", "id"]),
|
||||
("ix_login_tasks_status_finished_at_id", "login_tasks", ["status", "finished_at", "id"]),
|
||||
)
|
||||
|
||||
|
||||
def _indexes(bind, table_name: str) -> set[str]:
|
||||
if not sa.inspect(bind).has_table(table_name):
|
||||
return set()
|
||||
return {index["name"] for index in sa.inspect(bind).get_indexes(table_name)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
for name, table_name, columns in INDEXES:
|
||||
if name not in _indexes(bind, table_name):
|
||||
op.create_index(name, table_name, columns)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
bind = op.get_bind()
|
||||
for name, table_name, _ in reversed(INDEXES):
|
||||
if name in _indexes(bind, table_name):
|
||||
op.drop_index(name, table_name=table_name)
|
||||
Reference in New Issue
Block a user