Compare commits

...

2 Commits

Author SHA1 Message Date
liufumingren
b7feed7e76 perf(order): 补复核时间统计索引,消除大屏复核数全表扫描
大屏「今日复核数」(warehouse_panel.queryFulfillment)固定按 recheck_time 区间 + status 过滤 + dispatch_lane IN ('m4','m5') 做 count,而 dispatch 分表上 recheck_time 无任何索引:EXPLAIN 为 type=ALL / rows=1,400,782,生产 7 天出现 138 次、平均 35.8s(同形状的 delivery_time 版本命中 idx_dispatch_1000028_panel_fulfillment 走 range,可直接对比)。

索引形态:current/outsourced 为 (recheck_time,status);dispatch 为 (recheck_time,status,dispatch_lane),比 current 版本多带 dispatch_lane 以便 count(*) 完全走索引不回表,并由 isPanelRecheckIndexName 保证 dispatch 不继承 current 版本。历史表不参与大屏统计,不建该索引。

依据:抽样 20 万行 recheck_time 非空 97.9%、status<>cancel 98.8%,时间窗口是唯一有效过滤条件。索引仍由模型定义在分表首次访问时懒创建。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-17 01:47:26 +08:00
liufumingren
4f8f453c36 perf(order): 订单分表补报表时间窗与归档时间索引
1. 发货单分表(current/dispatch/outsourced)补 (create_at,status,delete_at) 索引:工作台热销商品排行等报表固定按 create_at 区间 + status/delete_at 过滤订单再关联明细汇总,缺索引时优化器只能全扫明细表并逐行回表核对窗口,生产 wms_order_delivery_1000028_dispatch(140 万行/4.4GB)实测单次 38.9s。历史表按 archive_time 检索,沿用已有 idx_{warehouseId}_history_archive_time,不补建该索引。

2. 订单明细历史分表补 archive_time 索引:history 分表独有归档时间列,原先只有 created_at 索引,按归档时间检索会全表扫描。

3. 同步更新分表工厂索引用例:改为按集合断言(前 3 位继承 / create_at 同名继承 / 不继承 current 版列表索引 / history 仅 archive_time),修正此前因 logistics_code 与 is_b2b 列表索引新增而过期的期望值。

索引由模型定义在分表首次访问时懒创建(不改动存量表结构),按低峰发布。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-17 01:45:11 +08:00
3 changed files with 104 additions and 42 deletions

View File

@ -95,6 +95,19 @@ function isListSortIndexName(name) {
return LIST_SORT_INDEX_SUFFIXES.some(suffix => normalizedName.endsWith(suffix));
}
/**
* 判断索引名是否为「复核时间」面板统计索引(current / outsourced 版本)。
*
* dispatch 分表的复核统计多带 dispatch_lane,索引独立命名
* (idx_dispatch_{warehouseId}_panel_recheck),因此 dispatch 不继承该索引。
*
* @param {string} name 索引名。
* @return {boolean} 是否为复核时间统计索引。
*/
function isPanelRecheckIndexName(name) {
return /^idx_\d+_(?:current|outsourced)_recheck_time$/.test(String(name || ''));
}
/**
* 判断是否为 outsourced 类型。
* @param {string} type 模型类型。
@ -633,7 +646,7 @@ function buildIndexOptions(warehouseId, type) {
// 在 dispatch 分表上无法覆盖 count,须改用带 dispatch_lane 的版本(追加在末尾)
const inheritedIndexes = baseIndexes
.slice(3)
.filter(index => !isListSortIndexName(index.name));
.filter(index => !isListSortIndexName(index.name) && !isPanelRecheckIndexName(index.name));
return [
baseIndexes[0],
@ -651,6 +664,13 @@ function buildIndexOptions(warehouseId, type) {
name: `idx_dispatch_${warehouseId}_panel_fulfillment`,
fields: [ 'delivery_time', 'outbound_status' ],
},
// 大屏「今日复核数」计数索引:面板按 recheck_time 区间统计(该列原先无任何索引,
// 全表扫描 140 万行实测单次 35.8s)。比 current 版本多带 dispatch_lane
// (WHERE 恒为 IN ('m4','m5')),带上该列后 count(*) 可完全走索引不回表。
{
name: `idx_dispatch_${warehouseId}_panel_recheck`,
fields: [ 'recheck_time', 'status', 'dispatch_lane' ],
},
];
}
@ -720,6 +740,26 @@ function buildIndexOptions(warehouseId, type) {
name: `idx_${warehouseId}_history_archive_time`,
fields: [ 'archive_time' ],
});
} else {
// 报表类时间窗查询(如工作台热销商品排行 dashboard.hot_order_rank)以
// `create_at` 区间 + status/delete_at 过滤订单,再按 id 关联明细汇总。
// 缺该索引时优化器只能先全扫明细表再逐行回表核对时间窗(生产 1000028_dispatch
// 140 万行实测 38.9s);把过滤列一并放进索引后,窗口裁剪与状态过滤都在索引内完成,
// 无需回表即可完成 INNER JOIN。历史表按 archive_time 归档检索,故不补建该索引。
indexes.push({
name: `idx_${prefix}_create_at`,
fields: [ 'create_at', 'status', 'delete_at' ],
});
// 大屏「今日复核数」按 recheck_time 区间 + status 统计
// (warehouse_panel.queryFulfillment)。该列原先无任何索引,dispatch 分表
// 140 万行只能全表扫描(实测单次 35.8s);以时间区间做前导列裁剪,
// status 作为第二列在索引内过滤(抽样 20 万行:recheck_time 非空 97.9%、
// 非取消 98.8%,时间窗口是唯一有效过滤条件)。
indexes.push({
name: `idx_${prefix}_recheck_time`,
fields: [ 'recheck_time', 'status' ],
});
}
// 列表排序索引统一追加在末尾:已有索引的下标位置保持不变(dispatch 分支按

View File

@ -267,7 +267,7 @@ module.exports = app => {
timestamps: true,
underscored: true,
paranoid: false,
indexes: this.generateIndexes(warehouseId),
indexes: this.generateIndexes(warehouseId, type),
comment: modelComment,
});
}
@ -303,9 +303,10 @@ module.exports = app => {
/**
* 生成索引配置
* @param {number} warehouseId 仓库ID
* @param {string} [type] 模型类型(current/dispatch/history),history 额外补归档时间索引
*/
generateIndexes(warehouseId) {
return [
generateIndexes(warehouseId, type = null) {
const indexes = [
{
name: `idx_${warehouseId}_order_id`,
fields: [ 'order_id' ],
@ -339,6 +340,17 @@ module.exports = app => {
fields: [ 'created_at' ],
},
];
// 历史表独有 archive_time(归档时间)列:归档库按归档时间检索,
// 缺索引时只能全表扫描,故为 history 分表补建该索引(其余分表无此列)。
if (type === 'history') {
indexes.push({
name: `idx_${warehouseId}_history_archive_time`,
fields: [ 'archive_time' ],
});
}
return indexes;
}
/**
@ -412,7 +424,7 @@ module.exports = app => {
}
}
const expectedIndexes = this.generateIndexes(warehouseId);
const expectedIndexes = this.generateIndexes(warehouseId, type);
const actualIndexes = Array.isArray(indexes) ? indexes : [];
for (const expectedIndex of expectedIndexes) {
@ -582,7 +594,7 @@ module.exports = app => {
.map(index => index.Key_name || index.name || '')
.filter(Boolean));
for (const indexDef of this.generateIndexes(warehouseId)) {
for (const indexDef of this.generateIndexes(warehouseId, type)) {
if (existingIndexNames.has(indexDef.name)) {
continue;
}

View File

@ -176,47 +176,46 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式
const dispatchIndexes = factory.getDispatchTableIndexLines(TEST_WH);
const baseIndexOptions = orderFactory.getIndexes(TEST_WH, 'current');
const dispatchIndexOptions = factory.getDispatchTableIndexOptions(TEST_WH);
const expectedDispatchIndexLines = [
baseIndexes[0],
baseIndexes[1],
baseIndexes[2],
'KEY `idx_dispatch_100000_dispatch_lane` (`dispatch_lane`,`status`)',
// current 的列表排序索引前缀不含 dispatch_lane,dispatch 分表不继承,改用带 dispatch_lane 的版本
...baseIndexes.slice(3).filter(line => !/_list_updated_at|_list_delivery_time/.test(line)),
'KEY `idx_dispatch_100000_list_updated_at` (`warehouse_id`,`dispatch_lane`,`updated_at`,`id`)',
'KEY `idx_dispatch_100000_list_delivery_time` (`warehouse_id`,`dispatch_lane`,`delivery_time`,`id`)',
'KEY `idx_dispatch_100000_panel_fulfillment` (`delivery_time`,`outbound_status`)',
];
const expectedDispatchIndexOptions = [
baseIndexOptions[0],
baseIndexOptions[1],
baseIndexOptions[2],
{
name: 'idx_dispatch_100000_dispatch_lane',
fields: [ 'dispatch_lane', 'status' ],
},
...baseIndexOptions.slice(3).filter(index => !/_list_updated_at|_list_delivery_time/.test(index.name)),
{
name: 'idx_dispatch_100000_list_updated_at',
fields: [ 'warehouse_id', 'dispatch_lane', 'updated_at', 'id' ],
},
{
name: 'idx_dispatch_100000_list_delivery_time',
fields: [ 'warehouse_id', 'dispatch_lane', 'delivery_time', 'id' ],
},
{
name: 'idx_dispatch_100000_panel_fulfillment',
fields: [ 'delivery_time', 'outbound_status' ],
},
];
const dispatchIndexByName = new Map(dispatchIndexOptions.map(index => [ index.name, index ]));
assert.equal(baseColumns.length, Object.keys(orderDeliveryModel.rawAttributes).length);
assert.equal(dispatchColumns.length, baseColumns.length + 2);
assert.ok(dispatchColumns.some(line => line.includes('dispatch_lane')));
assert.ok(dispatchColumns.some(line => line.includes('dispath_time')));
assert.deepEqual(dispatchIndexes, expectedDispatchIndexLines);
assert.deepEqual(dispatchIndexOptions, expectedDispatchIndexOptions);
// dispatch 前 3 个索引按位置继承 current(store_id / warehouse_id / delivery_code)
assert.deepEqual(dispatchIndexOptions.slice(0, 3), baseIndexOptions.slice(0, 3));
assert.deepEqual(dispatchIndexes.slice(0, 3), baseIndexes.slice(0, 3));
// 非列表类索引整体继承:报表时间窗索引 current 与 dispatch 同名共用
const baseCreateAtIndex = baseIndexOptions.find(index => index.name === 'idx_100000_current_create_at');
assert.deepEqual(baseCreateAtIndex.fields, [ 'create_at', 'status', 'delete_at' ]);
assert.deepEqual(dispatchIndexByName.get('idx_100000_current_create_at'), baseCreateAtIndex);
assert.ok(
dispatchIndexes.includes('KEY `idx_100000_current_create_at` (`create_at`,`status`,`delete_at`)')
);
// current 的列表排序索引前缀不含 dispatch_lane,dispatch 不继承,改用带 dispatch_lane 的版本
assert.ok(
!dispatchIndexOptions.some(index => /^idx_100000_current_list_/.test(index.name)),
'dispatch 分表不应继承 current 版本的列表排序索引'
);
// 复核统计索引 dispatch 形态不同(多带 dispatch_lane),不继承 current 版本
assert.deepEqual(baseIndexOptions.find(index => index.name === 'idx_100000_current_recheck_time').fields, [
'recheck_time', 'status',
]);
assert.ok(
!dispatchIndexByName.has('idx_100000_current_recheck_time'),
'dispatch 分表不应继承 current 版本的复核统计索引'
);
assert.deepEqual(dispatchIndexByName.get('idx_dispatch_100000_panel_recheck').fields, [
'recheck_time', 'status', 'dispatch_lane',
]);
assert.ok(dispatchIndexByName.has('idx_dispatch_100000_list_updated_at'));
assert.ok(dispatchIndexByName.has('idx_dispatch_100000_list_delivery_time'));
assert.ok(dispatchIndexByName.has('idx_dispatch_100000_dispatch_lane'));
assert.ok(dispatchIndexByName.has('idx_dispatch_100000_panel_fulfillment'));
assert.ok(dispatchIndexes.includes('KEY `idx_dispatch_100000_panel_fulfillment` (`delivery_time`,`outbound_status`)'));
});
it('should declare list sort indexes covering warehouse scope and id tiebreaker', () => {
@ -271,6 +270,17 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式
!dispatchByName.has('idx_100000_current_list_updated_at'),
'dispatch 分表不应同时维护 current 版本的列表排序索引'
);
// 历史表单独用 archive_time 归档检索:声明归档索引,不建 create_at / recheck_time 索引
assert.deepEqual(historyByName.get('idx_100000_history_archive_time').fields, [ 'archive_time' ]);
assert.ok(
!historyByName.has('idx_100000_history_create_at'),
'历史表按 archive_time 检索,不应声明 create_at 时间窗索引'
);
assert.ok(
!historyByName.has('idx_100000_history_recheck_time'),
'历史表不参与大屏复核统计,不应声明 recheck_time 索引'
);
});
it('should create one physical table on first getModel; getM4Model and getM5Model share the dispatch model', async function() {