134 lines
4.9 KiB
JavaScript
134 lines
4.9 KiB
JavaScript
'use strict';
|
||
|
||
/**
|
||
* queryOrder 波次计数性能优化:为 current/dispatch 分表增加 wave_id 前导索引。
|
||
*
|
||
* 背景:
|
||
* - app/service/systems/workbench.js 的 _countWaveOrders 会对 current + dispatch 两族分表执行
|
||
* `SELECT COUNT(*) FROM ... WHERE wave_id=? AND warehouse_id=?`;
|
||
* - 现有索引 `idx_{wh}_current_wave` 为 (status, wave_id),前导列 status 不在谓词中,
|
||
* 生产 dispatch 分表(约 25.38 万行 / 524MB)需全表扫描约 12.69 万行;
|
||
* - 本迁移动态枚举 `wms_order_delivery_{wh}` 与 `wms_order_delivery_{wh}_dispatch`,
|
||
* 为每张分表新增二级索引 `idx_{wh}_current_wave_id (wave_id)`。
|
||
*
|
||
* 设计说明:
|
||
* - 分表按仓库隔离,warehouse_id 在单张分表内为常量,索引无需包含 warehouse_id;
|
||
* - 使用 ALGORITHM=INPLACE, LOCK=NONE(MySQL 5.6+ 支持在线加二级索引),
|
||
* 不重建表、不锁写,适合低峰期执行;
|
||
* - 幂等:索引已存在则跳过;索引命名与 app/model/order_delivery_table_schema.js
|
||
* 的 buildIndexOptions 即将补充的定义一致,避免应用运行时 drift 同步重复建索引。
|
||
*
|
||
* ⚠️ 本文件为「未执行」草案,仅在用户完成风险确认后,通过迁移流程执行。
|
||
*/
|
||
|
||
const SHARD_PATTERNS = [
|
||
{ type: 'current', pattern: /^wms_order_delivery_(\d+)$/ },
|
||
{ type: 'dispatch', pattern: /^wms_order_delivery_(\d+)_dispatch$/ },
|
||
];
|
||
|
||
/**
|
||
* 解析分表名。
|
||
* @param {string} tableName 表名。
|
||
* @return {{ type: string, warehouseId: string }|null} 解析结果。
|
||
*/
|
||
function parseShardTableName(tableName) {
|
||
for (const item of SHARD_PATTERNS) {
|
||
const match = item.pattern.exec(tableName);
|
||
if (match) {
|
||
return { type: item.type, warehouseId: match[1] };
|
||
}
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* 构建 wave_id 前导索引名(与 buildIndexOptions 命名一致)。
|
||
* @param {string} warehouseId 仓库ID。
|
||
* @return {string} 索引名。
|
||
*/
|
||
function buildWaveIdIndexName(warehouseId) {
|
||
return `idx_${warehouseId}_current_wave_id`;
|
||
}
|
||
|
||
/**
|
||
* 列举库内所有 current/dispatch 分表。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<Array<string>>} 分表名列表。
|
||
*/
|
||
async function listManagedShards(queryInterface) {
|
||
const [ rows ] = await queryInterface.sequelize.query(
|
||
'SELECT TABLE_NAME AS tableName FROM INFORMATION_SCHEMA.TABLES '
|
||
+ 'WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME LIKE \'wms_order_delivery\\_%\' '
|
||
+ 'ORDER BY TABLE_NAME'
|
||
);
|
||
return (rows || [])
|
||
.map(row => String(row.tableName || row.TABLE_NAME || ''))
|
||
.filter(name => parseShardTableName(name));
|
||
}
|
||
|
||
/**
|
||
* 判断索引是否已存在。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @param {string} tableName 表名。
|
||
* @param {string} indexName 索引名。
|
||
* @return {Promise<boolean>} 是否存在。
|
||
*/
|
||
async function indexExists(queryInterface, tableName, indexName) {
|
||
const indexes = await queryInterface.showIndex(tableName);
|
||
return (indexes || []).some(index => {
|
||
const name = String(index && (index.name || index.Key_name || index.key_name) || '');
|
||
return name.toLowerCase() === String(indexName).toLowerCase();
|
||
});
|
||
}
|
||
|
||
module.exports = {
|
||
/**
|
||
* 执行迁移:为 current/dispatch 分表增加 wave_id 前导索引。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async up(queryInterface) {
|
||
const shards = await listManagedShards(queryInterface);
|
||
const results = [];
|
||
|
||
for (const tableName of shards) {
|
||
const parsed = parseShardTableName(tableName);
|
||
const indexName = buildWaveIdIndexName(parsed.warehouseId);
|
||
|
||
if (await indexExists(queryInterface, tableName, indexName)) {
|
||
results.push({ tableName, action: 'skip', reason: 'exists' });
|
||
continue;
|
||
}
|
||
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${tableName}\` ADD INDEX \`${indexName}\` (\`wave_id\`), ALGORITHM=INPLACE, LOCK=NONE`
|
||
);
|
||
results.push({ tableName, action: 'added', indexName });
|
||
}
|
||
|
||
console.log('[20260830100000-add-wave-id-index] up results:', JSON.stringify(results));
|
||
},
|
||
|
||
/**
|
||
* 回滚迁移:移除 wave_id 前导索引。
|
||
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
|
||
* @return {Promise<void>} 无返回值。
|
||
*/
|
||
async down(queryInterface) {
|
||
const shards = await listManagedShards(queryInterface);
|
||
|
||
for (const tableName of shards) {
|
||
const parsed = parseShardTableName(tableName);
|
||
const indexName = buildWaveIdIndexName(parsed.warehouseId);
|
||
|
||
if (!(await indexExists(queryInterface, tableName, indexName))) {
|
||
continue;
|
||
}
|
||
|
||
await queryInterface.sequelize.query(
|
||
`ALTER TABLE \`${tableName}\` DROP INDEX \`${indexName}\`, ALGORITHM=INPLACE, LOCK=NONE`
|
||
);
|
||
}
|
||
},
|
||
};
|