fix(autoOutbound): 已称重规则改用 EXISTS 半连接,避免全表扫描订单快递表

原实现先捞全量称重订单ID(wms_order_express 全表扫描,生产 167 万行/165s)再拼 id IN 约束,既慢又会产生巨型 IN 列表。改为关联 EXISTS 子查询:订单表驱动,快递表按 order_id 唯一索引逐行点查,命中语义不变(同一订单任一快递记录 weighing_weight > 0 即算已称重)。生产实测该查询由 165s 降至 0.03~0.07s。

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
liufumingren 2026-11-17 01:00:18 +08:00
parent 9af7dcf8fb
commit 70e823480d

View File

@ -1,7 +1,8 @@
'use strict';
const MainService = require('../main');
const { Op } = require('sequelize');
const Sequelize = require('sequelize');
const { Op } = Sequelize;
const OUTBOUND_STATUS = 'fin';
const OUTBOUND_STEP = 4;
@ -200,7 +201,7 @@ class OrderAutoOutboundService extends MainService {
* 扫描仓库下命中任一自动出库规则的订单ID。
*
* 逐条规则查询(规则内条件 AND),命中即纳入结果;订单ID 去重并保留首个命中的规则下标。
* 需要称重的规则先取称重订单ID(同仓库只查一次,跨规则复用)。
* 需要称重的规则通过关联 EXISTS 子查询(订单表驱动、快递表按 order_id 点查)约束。
*
* @param {number} warehouseId 仓库ID。
* @param {Array<Object>} rules 自动化出库规则数组。
@ -224,7 +225,6 @@ class OrderAutoOutboundService extends MainService {
const ruleList = Array.isArray(rules) ? rules : [];
const orderIds = [];
const ruleIndexById = new Map();
let weighOrderIds = null;
for (let index = 0; index < ruleList.length; index += 1) {
const remaining = limit - orderIds.length;
@ -236,13 +236,14 @@ class OrderAutoOutboundService extends MainService {
const where = this.buildAutoOutboundWhere(rule);
if (rule.require_weigh) {
if (weighOrderIds === null) {
weighOrderIds = await this.findOrderIdsWithWeigh(warehouseId);
}
if (weighOrderIds.length === 0) {
const weighCondition = this.buildWeighExistsCondition(orderModel, warehouseId);
if (!weighCondition) {
ctx.logger.warn(
`[auto-outbound] 仓库 ${warehouseId} 缺少订单快递模型或仓库ID非法,跳过「需已称重」规则${index + 1}`
);
continue;
}
where.id = { [Op.in]: weighOrderIds };
where[Op.and] = [ ...(Array.isArray(where[Op.and]) ? where[Op.and] : []), weighCondition ];
}
const orders = await orderModel.findAll({
@ -400,31 +401,82 @@ class OrderAutoOutboundService extends MainService {
}
/**
* 查找有称重记录的订单ID列表。
* 构造「已称重」半连接条件(关联 EXISTS 子查询)。
*
* `wms_order_express.weighing_weight` 无索引且历史命中率极高,若先捞出全量称重订单ID
* 再用 `id IN (...)` 约束,会全表扫描快递大表,并把百万级订单ID 塞进 SQL 与内存。
* 改为关联 EXISTS 后由订单表驱动,快递表按 order_id 唯一索引逐行点查。
*
* @param {Object} orderModel 当前仓库的订单模型(Sequelize 模型)。
* @param {number} warehouseId 仓库ID。
* @return {Promise<Array<number>>} 订单ID列表。
* @return {Object|null} Sequelize literal 条件;模型缺失或仓库ID非法时返回 null。
*/
async findOrderIdsWithWeigh(warehouseId) {
buildWeighExistsCondition(orderModel, warehouseId) {
const { ctx } = this;
const { OrderExpress } = ctx.model;
if (!OrderExpress) {
return [];
return null;
}
const rows = await OrderExpress.findAll({
where: {
warehouse_id: warehouseId,
weighing_weight: { [Op.gt]: 0 },
},
attributes: [ 'order_id' ],
raw: true,
});
const safeWarehouseId = Number(warehouseId);
if (!Number.isInteger(safeWarehouseId) || safeWarehouseId <= 0) {
return null;
}
return rows
.map(row => Number(row.order_id))
.filter(id => Number.isFinite(id) && id > 0);
const orderTableRef = this.buildSqlTableRef(orderModel);
const expressTableRef = this.buildSqlTableRef(OrderExpress);
if (!orderTableRef || !expressTableRef) {
return null;
}
return Sequelize.literal(
`EXISTS (SELECT 1 FROM ${expressTableRef}`
+ ` WHERE ${expressTableRef}.\`order_id\` = ${orderTableRef}.\`id\``
+ ` AND ${expressTableRef}.\`warehouse_id\` = ${safeWarehouseId}`
+ ` AND ${expressTableRef}.\`weighing_weight\` > 0)`
);
}
/**
* 把模型解析为 SQL 中可直接引用的表名(含 schema)。
*
* @param {Object} model Sequelize 模型。
* @return {string|null} 形如 `table` 或 `schema`.`table` 的表引用;无法解析时返回 null。
*/
buildSqlTableRef(model) {
if (!model) {
return null;
}
const raw = typeof model.getTableName === 'function' ? model.getTableName() : model;
const tableName = typeof raw === 'string' ? raw : (raw && raw.tableName);
const schema = raw && typeof raw === 'object' ? raw.schema : null;
const safeTable = this.quoteSqlIdentifier(tableName);
if (!safeTable) {
return null;
}
if (schema) {
const safeSchema = this.quoteSqlIdentifier(schema);
return safeSchema ? `${safeSchema}.${safeTable}` : null;
}
return safeTable;
}
/**
* 转义 SQL 标识符(仅允许字母、数字、下划线与 $,避免拼接注入)。
*
* @param {string} name 标识符。
* @return {string|null} 反引号包裹的标识符;非法时返回 null。
*/
quoteSqlIdentifier(name) {
if (typeof name !== 'string' || !/^[A-Za-z0-9_$]+$/.test(name)) {
return null;
}
return `\`${name}\``;
}
/**