From 18f258014febf2170491ab705b365f1d68ae1759 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 17 Nov 2026 01:20:26 +0800 Subject: [PATCH] =?UTF-8?q?=EF=BB=BFperf(backend):=20=E6=89=AB=E7=A0=81?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=88=97=E8=A1=A8=E8=A1=A5=E8=A6=86=E7=9B=96?= =?UTF-8?q?=E7=B4=A2=E5=BC=95=EF=BC=8Ccount(*)=20=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E9=80=90=E8=A1=8C=E5=9B=9E=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit /api/web/records 是 findAndCountAll,列表本身走 idx_scan_time 只要 0.5ms, 慢的是后面那条 count(id):MySQL 只能选 (subject_id, barcode),索引里没有 scanner_id,于是按主键逐行回表判断 —— 线上 16.5 万条时 Rows_examined ≈ 33 万, 慢查询日志里 3.2s~9.9s(冷缓存时走磁盘)。 补 (subject_id, scanner_id, scan_time) 后 count 变成纯索引扫描(Using index), 实测 0.45s(热)/9.9s(冷) → 0.09s。 - 新增迁移 20261116200000-add-scan-record-list-index.js(幂等,已在生产执行) - sql/schema.sql 基线同步该索引 - README 补索引用途与实测数字 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- backend/README.md | 8 ++++ ...261116200000-add-scan-record-list-index.js | 46 +++++++++++++++++++ backend/sql/schema.sql | 4 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 backend/database/migrations/20261116200000-add-scan-record-list-index.js diff --git a/backend/README.md b/backend/README.md index a12e1e8..3b4d2c7 100644 --- a/backend/README.md +++ b/backend/README.md @@ -640,6 +640,14 @@ node scripts/backfill-snapshot.js --dry-run > 本表**不用软删除**(paranoid 关闭):软删除会让记录占用唯一键,导致同一条扫码数据再也无法入库; > 需要清理请按时间物理删除:`DELETE FROM scan_records WHERE scan_time < ?`。 +索引(和 `/api/web/records` 列表页强相关,2026-11-16 补的): + +- `(subject_id, barcode)`:按运单号精确定位(回放查询用); +- `(subject_id, scanner_id, scan_time)`:列表页 `findAndCountAll` 那条 **`count(*)` 的覆盖索引**。 + 少了它 MySQL 只能走 `(subject_id, barcode)` 再逐行回表判断 `scanner_id`——线上 16.5 万条时 + `Rows_examined` ≈ 33 万,冷缓存 9.9 s / 热缓存 0.45 s;补完是纯索引扫描,0.09 s(迁移 `20261116200000`); +- `(scanner_id, channel_id, scan_time)`:散单要取同通道「上一条 / 下一条」的扫码时间。 + ## 页面(在前端项目里) ⚠️ **后端不再渲染页面**:`app/view/`、nunjucks、session、CSRF 都已移除,`app/controller/page/` 也删掉了。 diff --git a/backend/database/migrations/20261116200000-add-scan-record-list-index.js b/backend/database/migrations/20261116200000-add-scan-record-list-index.js new file mode 100644 index 0000000..0bd2a46 --- /dev/null +++ b/backend/database/migrations/20261116200000-add-scan-record-list-index.js @@ -0,0 +1,46 @@ +'use strict'; + +/** + * 扫码记录列表(`/api/web/records`):补一个能被「覆盖」的索引 + * + * 该接口是 `findAndCountAll`,一次请求会跑两条 SQL: + * 1. 列表:`WHERE subject_id = ? AND scanner_id IN (...) ORDER BY scan_time DESC, id DESC LIMIT 20` + * 2. 总数:`WHERE` 同上,但**没有 LIMIT**(`count(id)` 要数完整个集合) + * + * 2026-11-16 线上实测(主体 1,约 16.5 万条): + * - 列表:走 `idx_scan_time` 反向扫 + 过滤,约 28 ms,本来不慢; + * - 总数:走 `idx_scan_subject_barcode`(`subject_id, barcode`)——索引里没有 `scanner_id`, + * 只能**逐行回表**判断,`Rows_examined` ≈ 33 万;冷缓存 9.9 s、热缓存 0.45 s。 + * 「第一次打开扫码记录很慢」就是它。 + * + * 加 `(subject_id, scanner_id, scan_time)` 后,总数变成纯索引扫描(不回表), + * 顺带让「按设备过滤 + 按时间倒序」的列表也能直接吃索引顺序。 + * + * 注意:`sql/schema.sql` 基线已同步这个索引,空库执行时幂等。 + */ + +const INDEX_NAME = 'idx_scan_subject_scanner_time'; + +/** 判断索引是否存在(重跑/已经手工加过的库不报错) */ +async function hasIndex(queryInterface, table, name) { + try { + const indexes = await queryInterface.showIndex(table); + return indexes.some(item => item.name === name); + } catch (error) { + return false; + } +} + +module.exports = { + async up(queryInterface) { + if (await hasIndex(queryInterface, 'scan_records', INDEX_NAME)) return; + await queryInterface.addIndex('scan_records', [ 'subject_id', 'scanner_id', 'scan_time' ], { + name: INDEX_NAME, + }); + }, + + async down(queryInterface) { + if (!(await hasIndex(queryInterface, 'scan_records', INDEX_NAME))) return; + await queryInterface.removeIndex('scan_records', INDEX_NAME); + }, +}; diff --git a/backend/sql/schema.sql b/backend/sql/schema.sql index 0d1abaa..b6e30ef 100644 --- a/backend/sql/schema.sql +++ b/backend/sql/schema.sql @@ -153,7 +153,9 @@ CREATE TABLE IF NOT EXISTS `scan_records` ( KEY `idx_barcode` (`barcode`), KEY `idx_channel_scan_time` (`channel_id`, `scan_time`), KEY `idx_scan_subject_barcode` (`subject_id`, `barcode`), - KEY `idx_scan_channel_seq` (`scanner_id`, `channel_id`, `scan_time`) + KEY `idx_scan_channel_seq` (`scanner_id`, `channel_id`, `scan_time`), + -- ↓ 2026-11-16 新增(迁移 20261116200000):列表页 count(*) 走覆盖索引,不再逐行回表 + KEY `idx_scan_subject_scanner_time` (`subject_id`, `scanner_id`, `scan_time`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT = '扫码记录表(同 中控+通道+条码+扫描时间 去重;带当时的工作台快照)';