From 4f8f453c361b4fa11a9a03bd946ccf2ace3bfb12 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 17 Nov 2026 01:45:11 +0800 Subject: [PATCH] =?UTF-8?q?perf(order):=20=E8=AE=A2=E5=8D=95=E5=88=86?= =?UTF-8?q?=E8=A1=A8=E8=A1=A5=E6=8A=A5=E8=A1=A8=E6=97=B6=E9=97=B4=E7=AA=97?= =?UTF-8?q?=E4=B8=8E=E5=BD=92=E6=A1=A3=E6=97=B6=E9=97=B4=E7=B4=A2=E5=BC=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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> --- app/model/order_delivery_table_schema.js | 10 +++ app/model/order_items_factory.js | 22 ++++-- .../order_delivery_dispatch_factory.test.js | 67 +++++++++---------- 3 files changed, 58 insertions(+), 41 deletions(-) diff --git a/app/model/order_delivery_table_schema.js b/app/model/order_delivery_table_schema.js index 71d310b..b784ddd 100644 --- a/app/model/order_delivery_table_schema.js +++ b/app/model/order_delivery_table_schema.js @@ -720,6 +720,16 @@ 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' ], + }); } // 列表排序索引统一追加在末尾:已有索引的下标位置保持不变(dispatch 分支按 diff --git a/app/model/order_items_factory.js b/app/model/order_items_factory.js index 5f5d04e..93a3683 100644 --- a/app/model/order_items_factory.js +++ b/app/model/order_items_factory.js @@ -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; } diff --git a/test/app/model/order_delivery_dispatch_factory.test.js b/test/app/model/order_delivery_dispatch_factory.test.js index 490f812..316660d 100644 --- a/test/app/model/order_delivery_dispatch_factory.test.js +++ b/test/app/model/order_delivery_dispatch_factory.test.js @@ -176,47 +176,35 @@ 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 版本的列表排序索引' + ); + 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 +259,13 @@ 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 时间窗索引 + 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 时间窗索引' + ); }); it('should create one physical table on first getModel; getM4Model and getM5Model share the dispatch model', async function() {