From b7feed7e763a71e611549361ce23d63fbffd08ed Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 17 Nov 2026 01:47:21 +0800 Subject: [PATCH] =?UTF-8?q?perf(order):=20=E8=A1=A5=E5=A4=8D=E6=A0=B8?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E7=BB=9F=E8=AE=A1=E7=B4=A2=E5=BC=95=EF=BC=8C?= =?UTF-8?q?=E6=B6=88=E9=99=A4=E5=A4=A7=E5=B1=8F=E5=A4=8D=E6=A0=B8=E6=95=B0?= =?UTF-8?q?=E5=85=A8=E8=A1=A8=E6=89=AB=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 大屏「今日复核数」(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> --- app/model/order_delivery_table_schema.js | 32 ++++++++++++++++++- .../order_delivery_dispatch_factory.test.js | 17 +++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/app/model/order_delivery_table_schema.js b/app/model/order_delivery_table_schema.js index b784ddd..f64a805 100644 --- a/app/model/order_delivery_table_schema.js +++ b/app/model/order_delivery_table_schema.js @@ -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' ], + }, ]; } @@ -730,6 +750,16 @@ function buildIndexOptions(warehouseId, type) { 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 分支按 diff --git a/test/app/model/order_delivery_dispatch_factory.test.js b/test/app/model/order_delivery_dispatch_factory.test.js index 316660d..0db2e8e 100644 --- a/test/app/model/order_delivery_dispatch_factory.test.js +++ b/test/app/model/order_delivery_dispatch_factory.test.js @@ -200,6 +200,17 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式 !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')); @@ -260,12 +271,16 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式 'dispatch 分表不应同时维护 current 版本的列表排序索引' ); - // 历史表单独用 archive_time 归档检索:声明归档索引,不建 create_at 时间窗索引 + // 历史表单独用 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() {