Compare commits
2 Commits
0a6e82e00a
...
b7feed7e76
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b7feed7e76 | ||
|
|
4f8f453c36 |
@ -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 分支按
|
||||
|
||||
@ -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;
|
||||
}
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user