From 3b0c48534dcf10d7ddd6a957caecadb5b26ddd1e Mon Sep 17 00:00:00 2001 From: yml Date: Sat, 21 Nov 2026 15:30:54 +0800 Subject: [PATCH] =?UTF-8?q?perf(platform):=20=E6=8C=89=E4=BC=98=E5=85=88?= =?UTF-8?q?=E7=BA=A7=E5=88=86=E5=B1=82=E6=9F=A5=E8=AF=A2=E8=B4=A6=E5=8F=B7?= =?UTF-8?q?=E5=80=99=E9=80=89=E6=B1=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_migrations.py | 2 +- .../migrations/migration_manifest.json | 4 + .../20261121_0109_account_pool_tier_index.py | 53 ++++++++++++ web/backend/models.py | 10 +++ .../platform_fulfillment/account_selector.py | 81 ++++++++++++++----- 5 files changed, 130 insertions(+), 20 deletions(-) create mode 100644 web/backend/migrations/versions/20261121_0109_account_pool_tier_index.py diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 3f1de43..5addfc3 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -42,7 +42,7 @@ TESTS_DIR = Path(__file__).resolve().parent PROJECT_ROOT = TESTS_DIR.parent VERSIONS_DIR = PROJECT_ROOT / "web" / "backend" / "migrations" / "versions" -HEAD_REVISION = "20261121_0108" +HEAD_REVISION = "20261121_0109" # 迁移新增、但不声明在模型里的复合查询索引。 EXTRA_INDEXES = ( diff --git a/web/backend/migrations/migration_manifest.json b/web/backend/migrations/migration_manifest.json index ae71829..5a4b5e2 100644 --- a/web/backend/migrations/migration_manifest.json +++ b/web/backend/migrations/migration_manifest.json @@ -433,6 +433,10 @@ "20261121_0108_account_pool_query_indexes.py": { "revision": "20261121_0108", "sha256": "b45a7f0c563e43e6516cf00ad12ce7a5fb78bb7bde6b3f8de02cb888ad761666" + }, + "20261121_0109_account_pool_tier_index.py": { + "revision": "20261121_0109", + "sha256": "0aa85febbc0f2c99f5768bba2364f8797a748c4de740780e33a2017311f591b0" } } } diff --git a/web/backend/migrations/versions/20261121_0109_account_pool_tier_index.py b/web/backend/migrations/versions/20261121_0109_account_pool_tier_index.py new file mode 100644 index 0000000..83f1d0d --- /dev/null +++ b/web/backend/migrations/versions/20261121_0109_account_pool_tier_index.py @@ -0,0 +1,53 @@ +"""账号池分层候选查询索引。 + +`live_candidate_accounts` 现在按 CK、生命周期、宝典状态和积分充足度分层查询; +这条索引让每一层都能在过滤后直接按 CK 检查时间/积分/ID 取前 N 条。 +""" + +from __future__ import annotations + +import sqlalchemy as sa +from alembic import op + +revision: str = "20261121_0109" +down_revision: str | None = "20261121_0108" +branch_labels: str | None = None +depends_on: str | None = None + +INDEX_NAME = "ix_accounts_pool_tier" + + +def _index_exists(table: str, name: str) -> bool: + conn = op.get_bind() + return bool( + conn.execute( + sa.text( + "SELECT COUNT(*) FROM information_schema.statistics " + "WHERE table_schema = DATABASE() " + "AND table_name = :table AND index_name = :name" + ), + {"table": table, "name": name}, + ).scalar() + ) + + +def upgrade() -> None: + if not _index_exists("accounts", INDEX_NAME): + op.create_index( + INDEX_NAME, + "accounts", + [ + "auto_managed", + "ck_check_status", + "status", + "elite_opened", + "ck_checked_at", + "points", + "id", + ], + ) + + +def downgrade() -> None: + if _index_exists("accounts", INDEX_NAME): + op.drop_index(INDEX_NAME, table_name="accounts") diff --git a/web/backend/models.py b/web/backend/models.py index 5f4d6db..52490ee 100644 --- a/web/backend/models.py +++ b/web/backend/models.py @@ -282,6 +282,16 @@ class Account(Base): "points", "id", ), + Index( + "ix_accounts_pool_tier", + "auto_managed", + "ck_check_status", + "status", + "elite_opened", + "ck_checked_at", + "points", + "id", + ), ) assigned_user: Mapped[User | None] = relationship( diff --git a/web/backend/services/platform_fulfillment/account_selector.py b/web/backend/services/platform_fulfillment/account_selector.py index b449abd..1de0b01 100644 --- a/web/backend/services/platform_fulfillment/account_selector.py +++ b/web/backend/services/platform_fulfillment/account_selector.py @@ -201,26 +201,69 @@ def live_candidate_accounts( query = query.filter(~exchanged) if empty_only: query = query.filter(Account.role_bind_status == "unbound") - order_by = [ - case((Account.ck_check_status == "valid", 0), else_=1), - case((Account.status == "active", 0), else_=1), - case((Account.elite_opened.is_(True), 0), else_=1), - case( - (Account.points >= max(0, int(required_points)), 0), - else_=1, - ), - Account.ck_checked_at.is_(None).desc(), - Account.ck_checked_at.asc(), - Account.points.asc(), - Account.id.asc(), - ] - if prefer_unbound: - order_by.insert(0, case((Account.role_bind_status == "unbound", 0), else_=1)) - return ( - query.order_by(*order_by) - .limit(max(1, min(int(limit), 100))) - .all() + effective_limit = max(1, min(int(limit), 100)) + required = max(0, int(required_points)) + + # Keep the old priority order, but turn it into disjoint indexed queries. + # A single ORDER BY CASE forced MySQL to sort the whole account pool; each + # tier below can stop as soon as it has enough rows. + tiers = ( + (True, "active", True, True), + (True, "active", True, False), + (True, "active", False, True), + (True, "active", False, False), + (True, "not_active", True, True), + (True, "not_active", True, False), + (True, "not_active", False, True), + (True, "not_active", False, False), + (False, "active", True, True), + (False, "active", True, False), + (False, "active", False, True), + (False, "active", False, False), + (False, "not_active", True, True), + (False, "not_active", True, False), + (False, "not_active", False, True), + (False, "not_active", False, False), ) + rows: list[Account] = [] + for ck_valid, status_active, elite_opened, points_enough in tiers: + if ck_valid: + tier_query = query.filter(Account.ck_check_status == "valid") + else: + tier_query = query.filter(Account.ck_check_status != "valid") + if status_active == "active": + tier_query = tier_query.filter(Account.status == "active") + else: + tier_query = tier_query.filter(Account.status != "active") + tier_query = tier_query.filter( + Account.elite_opened.is_(True) + if elite_opened + else Account.elite_opened.is_not(True) + ) + tier_query = tier_query.filter( + Account.points >= required + if points_enough + else or_(Account.points.is_(None), Account.points < required) + ) + order_by = [ + Account.ck_checked_at.is_(None).desc(), + Account.ck_checked_at.asc(), + Account.points.asc(), + Account.id.asc(), + ] + if prefer_unbound: + order_by.insert( + 0, + case((Account.role_bind_status == "unbound", 0), else_=1), + ) + rows.extend( + tier_query.order_by(*order_by) + .limit(effective_limit - len(rows)) + .all() + ) + if len(rows) >= effective_limit: + break + return rows def capability_candidate_accounts(