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 = '扫码记录表(同 中控+通道+条码+扫描时间 去重;带当时的工作台快照)';