From 70e823480d2b3b25c430f88f78ed828a5334de3f Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 17 Nov 2026 01:00:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(autoOutbound):=20=E5=B7=B2=E7=A7=B0?= =?UTF-8?q?=E9=87=8D=E8=A7=84=E5=88=99=E6=94=B9=E7=94=A8=20EXISTS=20?= =?UTF-8?q?=E5=8D=8A=E8=BF=9E=E6=8E=A5=EF=BC=8C=E9=81=BF=E5=85=8D=E5=85=A8?= =?UTF-8?q?=E8=A1=A8=E6=89=AB=E6=8F=8F=E8=AE=A2=E5=8D=95=E5=BF=AB=E9=80=92?= =?UTF-8?q?=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 原实现先捞全量称重订单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> --- app/service/systems/order_auto_outbound.js | 98 +++++++++++++++++----- 1 file changed, 75 insertions(+), 23 deletions(-) diff --git a/app/service/systems/order_auto_outbound.js b/app/service/systems/order_auto_outbound.js index 34f3ac8..17097dc 100644 --- a/app/service/systems/order_auto_outbound.js +++ b/app/service/systems/order_auto_outbound.js @@ -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} 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>} 订单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}\``; } /**