diff --git a/app/model/order_delivery_table_schema.js b/app/model/order_delivery_table_schema.js index ffdef38..0de44c4 100644 --- a/app/model/order_delivery_table_schema.js +++ b/app/model/order_delivery_table_schema.js @@ -17,6 +17,19 @@ const MANAGED_TYPES = Object.freeze({ */ const AUTO_INCREMENT_SAFE_MARGIN = 50; +/** + * 列表排序索引名称后缀。 + * + * 列表接口固定带 warehouse_id 等值条件且默认按 updated_at / delivery_time 排序, + * 对应的索引在 dispatch 分表中需要额外携带 dispatch_lane 才能覆盖 count 统计 + * (否则统计仍需逐行回表核对 dispatch_lane),因此 dispatch 分支需要识别并 + * 剔除从 current 继承来的同名后缀索引,改用自身版本,避免同一张表维护两份功能重叠的索引。 + */ +const LIST_SORT_INDEX_SUFFIXES = Object.freeze([ + '_list_updated_at', + '_list_delivery_time', +]); + const TABLE_PATTERNS = Object.freeze([ { type: MANAGED_TYPES.DISPATCH, @@ -63,6 +76,17 @@ function isHistoryType(type) { return normalizeManagedType(type) === MANAGED_TYPES.HISTORY; } +/** + * 判断索引名是否属于列表排序索引。 + * @param {string} name 索引名。 + * @return {boolean} 是否为列表排序索引。 + */ +function isListSortIndexName(name) { + const normalizedName = String(name || ''); + + return LIST_SORT_INDEX_SUFFIXES.some(suffix => normalizedName.endsWith(suffix)); +} + /** * 判断是否为 outsourced 类型。 * @param {string} type 模型类型。 @@ -522,6 +546,41 @@ function buildTableOptions(app, warehouseId, type) { return options; } +/** + * 构建列表排序索引选项。 + * + * 工作台订单列表(listByModule)的 SQL 形态固定为 + * `WHERE warehouse_id = ? [AND dispatch_lane = ?] ORDER BY DESC, id DESC LIMIT ?` + * 且并行执行同条件的 COUNT(*)。若没有与「过滤列前缀 + 排序列」一致的索引: + * - 排序需要全表扫描 + filesort; + * - COUNT(*) 需要逐行回表核对不在索引中的过滤列。 + * 生产 wms_order_delivery_1000028_dispatch(59 万行 / 1.1GB)实测单请求因此达到 2 秒, + * 故在此显式声明 updated_at(前端请求的实际排序字段)与 delivery_time(deliveryOrder 模块默认排序字段)两份索引。 + * + * @param {number|string} warehouseId 仓库ID。 + * @param {string} type 模型类型。 + * @return {Array} 列表排序索引选项。 + */ +function buildListSortIndexes(warehouseId, type) { + const modelType = normalizeManagedType(type); + const dispatchType = isDispatchType(modelType); + const namePrefix = dispatchType ? `idx_dispatch_${warehouseId}` : `idx_${warehouseId}_${modelType}`; + // dispatch 分表的 where 恒定带 dispatch_lane(m4/m5),把该常量列放进索引前缀 + // 才能让 COUNT(*) 变成纯索引扫描;非 dispatch 分表没有该列。 + const scopeFields = dispatchType ? [ 'warehouse_id', 'dispatch_lane' ] : [ 'warehouse_id' ]; + + return [ + { + name: `${namePrefix}_list_updated_at`, + fields: [ ...scopeFields, 'updated_at', 'id' ], + }, + { + name: `${namePrefix}_list_delivery_time`, + fields: [ ...scopeFields, 'delivery_time', 'id' ], + }, + ]; +} + /** * 构建索引选项。 * @param {number|string} warehouseId 仓库ID。 @@ -538,13 +597,19 @@ function buildIndexOptions(warehouseId, type) { name: `idx_dispatch_${warehouseId}_dispatch_lane`, fields: [ 'dispatch_lane', 'status' ], }; + // 继承 current 的索引,但剔除列表排序索引:current 版本的前缀不含 dispatch_lane, + // 在 dispatch 分表上无法覆盖 count,须改用带 dispatch_lane 的版本(追加在末尾) + const inheritedIndexes = baseIndexes + .slice(3) + .filter(index => !isListSortIndexName(index.name)); return [ baseIndexes[0], baseIndexes[1], baseIndexes[2], dispatchLaneIndex, - ...baseIndexes.slice(3), + ...inheritedIndexes, + ...buildListSortIndexes(warehouseId, modelType), ]; } @@ -608,6 +673,10 @@ function buildIndexOptions(warehouseId, type) { }); } + // 列表排序索引统一追加在末尾:已有索引的下标位置保持不变(dispatch 分支按 + // 下标继承 current 的前 3 个索引,测试与运行时缓存均依赖该顺序)。 + indexes.push(...buildListSortIndexes(warehouseId, modelType)); + return indexes; } @@ -681,6 +750,8 @@ module.exports = { parseNumericConstraint, buildTableAttributes, buildTableName, + buildListSortIndexes, + isListSortIndexName, buildTableComment, buildTableOptions, buildIndexOptions, diff --git a/test/app/model/order_delivery_dispatch_factory.test.js b/test/app/model/order_delivery_dispatch_factory.test.js index d6ec904..6913d75 100644 --- a/test/app/model/order_delivery_dispatch_factory.test.js +++ b/test/app/model/order_delivery_dispatch_factory.test.js @@ -182,7 +182,10 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式 baseIndexes[1], baseIndexes[2], 'KEY `idx_dispatch_100000_dispatch_lane` (`dispatch_lane`,`status`)', - ...baseIndexes.slice(3), + // 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`)', ]; const expectedDispatchIndexOptions = [ baseIndexOptions[0], @@ -192,7 +195,15 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式 name: 'idx_dispatch_100000_dispatch_lane', fields: [ 'dispatch_lane', 'status' ], }, - ...baseIndexOptions.slice(3), + ...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' ], + }, ]; assert.equal(baseColumns.length, Object.keys(orderDeliveryModel.rawAttributes).length); @@ -203,6 +214,60 @@ describe('test/app/model/order_delivery_dispatch_factory.test.js dispatch 模式 assert.deepEqual(dispatchIndexOptions, expectedDispatchIndexOptions); }); + it('should declare list sort indexes covering warehouse scope and id tiebreaker', () => { + const orderFactory = createOrderDeliveryFactory({ + Sequelize: createSequelizeTypes(), + model: { + OrderDelivery: orderDeliveryModel, + define(name, attributes, options) { + return { + name, + tableName: options.tableName, + rawAttributes: attributes, + }; + }, + }, + }); + + const currentIndexes = orderFactory.getIndexes(TEST_WH, 'current'); + const historyIndexes = orderFactory.getIndexes(TEST_WH, 'history'); + const dispatchIndexes = factory.getDispatchTableIndexOptions(TEST_WH); + const indexByName = list => new Map(list.map(index => [ index.name, index ])); + const currentByName = indexByName(currentIndexes); + const historyByName = indexByName(historyIndexes); + const dispatchByName = indexByName(dispatchIndexes); + + // 列表默认排序 updated_at / deliveryOrder 默认 delivery_time 都需被索引覆盖, + // 且以 id 作尾列以匹配 `ORDER BY DESC, id DESC` + assert.deepEqual(currentByName.get('idx_100000_current_list_updated_at').fields, [ + 'warehouse_id', 'updated_at', 'id', + ]); + assert.deepEqual(currentByName.get('idx_100000_current_list_delivery_time').fields, [ + 'warehouse_id', 'delivery_time', 'id', + ]); + assert.deepEqual(historyByName.get('idx_100000_history_list_updated_at').fields, [ + 'warehouse_id', 'updated_at', 'id', + ]); + assert.deepEqual(historyByName.get('idx_100000_history_list_delivery_time').fields, [ + 'warehouse_id', 'delivery_time', 'id', + ]); + // dispatch 额外把常量条件列 dispatch_lane 放进前缀,否则 count 仍需回表 + assert.deepEqual(dispatchByName.get('idx_dispatch_100000_list_updated_at').fields, [ + 'warehouse_id', 'dispatch_lane', 'updated_at', 'id', + ]); + assert.deepEqual(dispatchByName.get('idx_dispatch_100000_list_delivery_time').fields, [ + 'warehouse_id', 'dispatch_lane', 'delivery_time', 'id', + ]); + assert.ok( + !currentByName.has('idx_dispatch_100000_list_updated_at'), + 'current 分表不应声明带 dispatch_lane 的索引' + ); + assert.ok( + !dispatchByName.has('idx_100000_current_list_updated_at'), + 'dispatch 分表不应同时维护 current 版本的列表排序索引' + ); + }); + it('should create one physical table on first getModel; getM4Model and getM5Model share the dispatch model', async function() { const mysqlIntegration = process.env.DISPATCH_FACTORY_TEST_MYSQL === '1'; if (!mysqlIntegration && dialect() !== 'mysql') {