From 14d1b4c7bcbd3842069692827d9490bf80bf20db Mon Sep 17 00:00:00 2001 From: Nixevol Date: Wed, 11 Nov 2026 03:06:31 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=B7=A5=E5=8F=82=E8=A1=A8=E6=8C=89=20?= =?UTF-8?q?CGI=20=E5=92=8C=E5=9F=BA=E7=AB=99=20ID=20=E5=8A=A0=E7=B4=A2?= =?UTF-8?q?=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit CellData_4G 唯一索引改为 (cgi, snapshot_id),另加 (enodeb_id, snapshot_id);CellData_5G 对应 (ncgi, snapshot_id)、(gnodeb_id, snapshot_id),跨日期按小区或基站查询都能走索引。 --- .../versions/0009_celldata_lookup_index.py | 29 +++++++++++++++++++ backend/app/models.py | 6 ++-- docs/gongcan-source.md | 11 +++++-- 3 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 backend/alembic/versions/0009_celldata_lookup_index.py diff --git a/backend/alembic/versions/0009_celldata_lookup_index.py b/backend/alembic/versions/0009_celldata_lookup_index.py new file mode 100644 index 0000000..81afade --- /dev/null +++ b/backend/alembic/versions/0009_celldata_lookup_index.py @@ -0,0 +1,29 @@ +"""按小区标识和基站 ID 跨快照查询:唯一索引改成小区标识在前,另加基站 ID 索引。""" + +from alembic import op + +revision = "0009_celldata_lookup_index" +down_revision = "0008_celldata_split" +branch_labels = None +depends_on = None + +TABLES = ( + ("CellData_4G", "celldata_4g", "cgi", "enodeb_id", "enodeb"), + ("CellData_5G", "celldata_5g", "ncgi", "gnodeb_id", "gnodeb"), +) + + +def upgrade() -> None: + for table, prefix, key, node, node_label in TABLES: + with op.batch_alter_table(table) as batch: + batch.drop_constraint(f"uq_{prefix}_{key}", type_="unique") + batch.create_unique_constraint(f"uq_{prefix}_{key}", [key, "snapshot_id"]) + batch.create_index(f"ix_{prefix}_{node_label}", [node, "snapshot_id"]) + + +def downgrade() -> None: + for table, prefix, key, node, node_label in TABLES: + with op.batch_alter_table(table) as batch: + batch.drop_index(f"ix_{prefix}_{node_label}") + batch.drop_constraint(f"uq_{prefix}_{key}", type_="unique") + batch.create_unique_constraint(f"uq_{prefix}_{key}", ["snapshot_id", key]) diff --git a/backend/app/models.py b/backend/app/models.py index 6eccdbc..79a2f9e 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -106,7 +106,8 @@ class CellData4G(Base): __tablename__ = "CellData_4G" __table_args__ = ( - UniqueConstraint("snapshot_id", "cgi", name="uq_celldata_4g_cgi"), + UniqueConstraint("cgi", "snapshot_id", name="uq_celldata_4g_cgi"), + Index("ix_celldata_4g_enodeb", "enodeb_id", "snapshot_id"), Index("ix_celldata_4g_station", "snapshot_id", "station_name"), ) @@ -134,7 +135,8 @@ class CellData5G(Base): __tablename__ = "CellData_5G" __table_args__ = ( - UniqueConstraint("snapshot_id", "ncgi", name="uq_celldata_5g_ncgi"), + UniqueConstraint("ncgi", "snapshot_id", name="uq_celldata_5g_ncgi"), + Index("ix_celldata_5g_gnodeb", "gnodeb_id", "snapshot_id"), Index("ix_celldata_5g_station", "snapshot_id", "station_name"), ) diff --git a/docs/gongcan-source.md b/docs/gongcan-source.md index 7d3bad0..382428b 100644 --- a/docs/gongcan-source.md +++ b/docs/gongcan-source.md @@ -9,7 +9,7 @@ | --- | --- | | OpenCode 接口、提示词、导出核对 | `backend/app/nqi/opencode.py` | | 快照合并、入库、日期规则、邻站 | `backend/app/collect/gongcan.py` | -| 表结构 | `backend/app/models.py` 的 `GongcanSnapshot`、`CellData4G`、`CellData5G`,迁移 `0007_gongcan_snapshot`、`0008_celldata_split` | +| 表结构 | `backend/app/models.py` 的 `GongcanSnapshot`、`CellData4G`、`CellData5G`,迁移 `0007`–`0009` | ## 一份快照怎么来 @@ -50,8 +50,13 @@ - `loading` 超过 40 分钟没更新视为取数进程已死,下一个任务接手。 - `failed` 冷却 10 分钟,期间别的任务直接报“刚取失败”,不反复打平台。 - 邻站直接用库里全市快照算,不再按方框扩大范围多次导出。 -- 索引:两张小区表各有唯一(快照,`cgi`/`ncgi`)和(快照,`station_name`),地市和日期走 `gongcan_snapshot` 的唯一索引。 - 判断目标在不在某天快照里,按 CGI 和按站点名都只走索引。 +- 索引(地市和日期走 `gongcan_snapshot` 的唯一索引): + + | | `CellData_4G` | `CellData_5G` | 用途 | + | --- | --- | --- | --- | + | 唯一 | (`cgi`, 快照) | (`ncgi`, 快照) | 按小区查,跨所有日期或指定日期 | + | 普通 | (`enodeb_id`, 快照) | (`gnodeb_id`, 快照) | 按基站查,跨所有日期或指定日期 | + | 普通 | (快照, `station_name`) | (快照, `station_name`) | 读整份快照、按站点名查、级联删除 | - 快照默认全部保留,系统不自动删除。要删时手动删 `gongcan_snapshot` 的行,两张小区表按外键级联删除; 不要删 `status = 'loading'` 的行,那是正在取数的快照。 - 实测江门两份快照(约 12.3 万个小区)占 52 MB,其中索引 13 MB,即每份约 26 MB。