perf(backend): 扫码记录列表补覆盖索引,count(*) 不再逐行回表
/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>
This commit is contained in:
parent
7a82bf13c7
commit
18f258014f
@ -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/` 也删掉了。
|
||||
|
||||
@ -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);
|
||||
},
|
||||
};
|
||||
@ -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 = '扫码记录表(同 中控+通道+条码+扫描时间 去重;带当时的工作台快照)';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user