perf(order): 订单分表补列表排序索引,修复配送列表 2 秒慢查询

原因:生产 wms_order_delivery_1000028_dispatch(59.4 万行 / data 1.13GB)没有任何
索引覆盖列表接口的固定形态

  WHERE warehouse_id = ? [AND dispatch_lane = ?] ORDER BY updated_at DESC, id DESC LIMIT 20

导致主查询全表扫 + filesort(实测 1.9s);与之并行执行的同条件 COUNT(*) 因
dispatch_lane 不在索引中需逐行回表(实测 1.5s),单请求合计约 2 秒。

新增 order_delivery_table_schema.buildListSortIndexes():

- current / history / outsourced:(warehouse_id, updated_at, id) 与 (warehouse_id, delivery_time, id)
  (updated_at 为前端实际排序字段,delivery_time 为 deliveryOrder 模块默认排序字段)
- dispatch:把恒定条件列 dispatch_lane 放进前缀
  (warehouse_id, dispatch_lane, updated_at, id) 与 (warehouse_id, dispatch_lane, delivery_time, id);
  缺少该列时 COUNT(*) 仍会退回回表(实测 1.2s),且 dispatch 分支会剔除从 current
  继承来的同名后缀索引,避免同一张表维护两份功能重叠的索引
- 索引统一追加在索引数组末尾,保持既有索引下标顺序不变

验证(开发库,同一张 59.4 万行同规模表 + 应用日志中的真实 SQL):

- 主查询 1511ms(Using filesort)→ 1.0ms(Backward index scan)
- COUNT(*)  1217ms(Using where 回表)→ 197ms(Using index)

生产建议低峰期先手工建同名索引,避免部署后首个请求在建索引期间阻塞。
This commit is contained in:
liufumingren 2026-09-15 16:03:29 +08:00
parent 55f766f643
commit a59793daf2
2 changed files with 139 additions and 3 deletions

View File

@ -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 <sortField> 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<Object>} 列表排序索引选项。
*/
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,

View File

@ -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 <sortField> 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') {