perf(match): 模糊档改前缀键+扫描护栏,大库点清单提速

- 新增 x_name_pre 前缀键(决策 339),前置通配 LIKE 改前缀等值,并补建次精确档索引(决策 338)
- 启动迁移回填老库前缀键与索引
- 检索加独立扫描预算哨,超时中断降级,宁少结果不白等
- 采纳链路加分段计时日志,定位慢段
This commit is contained in:
lianjianwei2024
2026-11-15 09:04:53 +08:00
parent b400454f74
commit 935c830135
10 changed files with 274 additions and 206 deletions

View File

@@ -308,10 +308,22 @@ def migrate_match_keys():
con.execute("SELECT id, Name, Specification, Unit FROM lmes WHERE x_setkey IS NULL")])
print("迁移:lmes 字集键回填 %d 行" % nl)
con.execute("CREATE INDEX IF NOT EXISTS ix_bill_setkey ON bills(x_setkey)")
con.execute("CREATE INDEX IF NOT EXISTS ix_bill_semi ON bills(x_hash_semi)") # 决策 338:次精确档索引(老库启动补齐;schema.sql 已带,这里保旧库不漏)
con.execute("CREATE INDEX IF NOT EXISTS ix_lme_setkey ON lmes(x_setkey)")
con.commit()
finally:
con.close()
# 决策 339:模糊档前缀键回填+建索引(列由 _ensure_batch_schema 补;回填要 match.name_pre 现算,
# 必须在该列存在后跑——放 migrate_match_keys 之后,与 setkey 回填同一启动序列)
con = sqlite3.connect(DB)
try:
nb = match.backfill_name_pre(con)
if nb:
print("迁移:bills 模糊档前缀键回填 %d 行(决策 339)" % nb, flush=True)
con.execute("CREATE INDEX IF NOT EXISTS ix_bill_namepre ON bills(x_name_pre)")
con.commit()
finally:
con.close()
scon = sess.connect()
try:
if not _hash9_current(scon, "s_bills"):
@@ -1594,6 +1606,9 @@ def api_sessions():
# 决策 213 补记七:五个统计数取消(不再逐会话 COUNT)
"total": g.get("x_total"), # 工程总造价(原文件口径,不是清单合价求和)
"dir": _dir_path(_dmap.get((g["name"] or "").strip(), 0)), # 所属目录全路径(未归类=空串)
# 决策 334(2026-11-14 主人定 A 案):创建时间=该名称下最早一次打开时间(取 all 里最早 imported_at,
# 字符串即时间序,格式 YYYY-MM-DD HH:MM:SS 直接比大小;老项目立即可显,不改表结构)
"created_at": min([s["at"] for s in g["all"] if s["at"]], default=None),
"all": g["all"], "dup": len(g["all"]),
"manual": _manual,
"can_reparse": bool(g["src_name"]) and (source_names is None or _src_exists_in_names(g["src_name"], source_names)) and not _manual,
@@ -6593,7 +6608,8 @@ def api_search(body):
pmin=_num_or_none(body.get("pmin")),
pmax=_num_or_none(body.get("pmax")),
att=body.get("att") or "all", # 决策 208:价格范围/附件清单侧同生效
only_batch_ids=only_ids) # 决策 277:同后缀硬过滤
only_batch_ids=only_ids, # 决策 277:同后缀硬过滤
self_unit=(s["bulid_unit"] if "bulid_unit" in s.keys() else "") or "") # 决策 337:同档内建设单位一致优先(空不加分)
# 规格 20:本组其他清单的批次记录(bill_id 集合)——组价不一致对比按它过滤
allowed = None
if scope_self and members is not None:
@@ -6847,40 +6863,49 @@ def api_hist_search(body):
(a.get("district") or "").strip())
if t[0]:
area.append(t)
if body.get("target") == "lme":
# 工料机标签:查全部入库项目工料机 + 信息价/市场价明细;
# 筛选=来源(srcs)/单位(orgs 跨来源联查)/地区/期次/价格范围/附件(单价类型/类别不参与)
res = match.hist_search_lme(hist,
orgs=body.get("orgs") or [],
area=area,
pfrom=body.get("period_from"), pto=body.get("period_to"),
kw=body.get("kw") or "",
pmin=_num_or_none(body.get("pmin")),
pmax=_num_or_none(body.get("pmax")),
att=body.get("att") or "all",
kinds=body.get("file_kinds") or [],
srcs=body.get("srcs") or ["project", "info", "market"],
limit=min(int(body.get("limit", 200)), 500),
_restore = match.search_deadline(hist) # 决策 339 ③:独立检索扫描护栏——超预算中断,宁少结果不白等
try:
if body.get("target") == "lme":
# 工料机标签:查全部入库项目工料机 + 信息价/市场价明细;
# 筛选=来源(srcs)/单位(orgs 跨来源联查)/地区/期次/价格范围/附件(单价类型/类别不参与)
res = match.hist_search_lme(hist,
orgs=body.get("orgs") or [],
area=area,
pfrom=body.get("period_from"), pto=body.get("period_to"),
kw=body.get("kw") or "",
pmin=_num_or_none(body.get("pmin")),
pmax=_num_or_none(body.get("pmax")),
att=body.get("att") or "all",
kinds=body.get("file_kinds") or [],
srcs=body.get("srcs") or ["project", "info", "market"],
limit=min(int(body.get("limit", 200)), 500),
self_name=self_name, self_src=self_src,
page=int(body.get("page", 1) or 1),
projs=body.get("projs") or [],
stop=match.stop_words(body.get("stop")))
else:
res = match.hist_search(hist,
body.get("file_kinds") or [],
body.get("bulid_units") or [],
area,
body.get("period_from"), body.get("period_to"),
body.get("kw") or "",
min(int(body.get("limit", 200)), 500),
self_name=self_name, self_src=self_src,
page=int(body.get("page", 1) or 1),
projs=body.get("projs") or [],
stop=match.stop_words(body.get("stop")))
else:
res = match.hist_search(hist,
body.get("file_kinds") or [],
body.get("bulid_units") or [],
area,
body.get("period_from"), body.get("period_to"),
body.get("kw") or "",
min(int(body.get("limit", 200)), 500),
self_name=self_name, self_src=self_src,
page=int(body.get("page", 1) or 1),
projs=body.get("projs") or [],
stop=match.stop_words(body.get("stop")),
only_batch_ids=_bill_search_batch_ids(hist, sess_src_name), # 决策 277:同后缀硬过滤(工料机分支不受限)
pmin=_num_or_none(body.get("pmin")),
pmax=_num_or_none(body.get("pmax")),
att=body.get("att") or "all") # 决策 208:价格范围/附件清单侧同生效)
stop=match.stop_words(body.get("stop")),
only_batch_ids=_bill_search_batch_ids(hist, sess_src_name), # 决策 277:同后缀硬过滤(工料机分支不受限)
pmin=_num_or_none(body.get("pmin")),
pmax=_num_or_none(body.get("pmax")),
att=body.get("att") or "all") # 决策 208:价格范围/附件清单侧同生效)
except sqlite3.OperationalError as _e:
if "interrupt" not in str(_e).lower():
raise
return {"ok": False, "msg": "检索超时(%g 秒预算内没扫完):关键词太泛或库太大,"
"请加条件缩小范围后再搜" % match.SEARCH_BUDGET_S}
finally:
_restore()
finally:
hist.close()
for r in res: # 单价类型码 → 名称(0/空=未标注 → 前端显示 —)