perf(order): quickSearch 改为逐级短路搜索
命中顺序:运单号(waybill_code) -> 发货单号(delivery_order_code) -> 平台订单号(source_order_codes), 每一级只要命中即停止后续查询,直接进入订单详情。 - 运单号:等值 + 剥离一单多件分段后缀主体等值 + 主体前缀 LIKE,走 waybill_code 索引 - 发货单号:等值走 idx_delivery_code_store;未命中再 %kw% 模糊兜底 - 平台订单号:%kw% 模糊仅作最后兜底 避免原单条 OR 查询混合等值与 %kw% 模糊导致优化器放弃索引、 退化为按 warehouse_id 全仓库扫描(生产单仓库 66w+ 行)。 dbg 调试日志改为持久写流异步写入,消除 appendFileSync 同步磁盘 IO。
This commit is contained in:
parent
c6224b471e
commit
ce04778021
@ -7,12 +7,39 @@ const { stripWaybillSegmentSuffix, escapeLikeWildcards } = require('../../../lib
|
||||
|
||||
const DEBUG_LOG = path.join(__dirname, '..', '..', '..', '..', 'temp', 'quickSearch_debug.log');
|
||||
|
||||
function dbg(msg) {
|
||||
const ts = new Date().toISOString();
|
||||
const line = `[${ts}] ${msg}\n`;
|
||||
try { fs.appendFileSync(DEBUG_LOG, line, 'utf8'); } catch (_) { /* ignore */ }
|
||||
// 持久可写流:dbg 使用异步缓冲写,避免 appendFileSync 每次 open/write/close 的同步磁盘 IO
|
||||
let debugStream = null;
|
||||
|
||||
function getDebugStream() {
|
||||
if (!debugStream) {
|
||||
try {
|
||||
debugStream = fs.createWriteStream(DEBUG_LOG, { flags: 'a' });
|
||||
debugStream.on('error', () => {
|
||||
debugStream = null;
|
||||
});
|
||||
} catch (_) {
|
||||
debugStream = null;
|
||||
}
|
||||
}
|
||||
return debugStream;
|
||||
}
|
||||
|
||||
function dbg(msg) {
|
||||
const stream = getDebugStream();
|
||||
if (!stream) return;
|
||||
const line = `[${new Date().toISOString()}] ${msg}\n`;
|
||||
try { stream.write(line); } catch (_) { /* ignore */ }
|
||||
}
|
||||
|
||||
// express 表查询列(1a/1b/1c 三段共用)
|
||||
const EXPRESS_ATTRIBUTES = [
|
||||
'id', 'order_id', 'waybill_code', 'delivery_order_code', 'source_order_codes', 'store_id',
|
||||
'logistics_code', 'logistics_name',
|
||||
'logistics_interface_id', 'logistics_interface_name',
|
||||
'platform_code', 'status', 'is_back', 'is_intercept', 'is_galaxy_receive',
|
||||
'weighing_weight',
|
||||
];
|
||||
|
||||
class DeliveryOrderService extends OrderBaseService {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
@ -63,8 +90,10 @@ class DeliveryOrderService extends OrderBaseService {
|
||||
|
||||
/**
|
||||
* 订单快速搜索。
|
||||
* 跨表联合搜索:运单号(wms_order_express,精确匹配,含剥离一单多件分段后缀后的主体等值匹配)、
|
||||
* 发货单号/外部订单号/平台交易单号(dispatch 表,模糊)。
|
||||
* 逐级短路搜索(wms_order_express 表),命中即停:
|
||||
* 1. 运单号 waybill_code(等值 + 剥离一单多件分段后缀后的主体等值 + 主体前缀 LIKE);
|
||||
* 2. 发货单号 delivery_order_code(等值优先,未命中再 %kw% 模糊);
|
||||
* 3. 平台订单号 source_order_codes(%kw% 模糊兜底)。
|
||||
* 返回匹配的订单列表(最多 20 条),包含订单关键信息及关联运单号。
|
||||
*
|
||||
* @param {Object} data 请求体。
|
||||
@ -96,10 +125,14 @@ class DeliveryOrderService extends OrderBaseService {
|
||||
// 收集匹配的订单 ID(去重)
|
||||
const matchedOrderIds = new Set();
|
||||
|
||||
// ====== 第一步:express 表 OR 搜索 ======
|
||||
dbg('--- Step1: express OR query ---');
|
||||
// ====== 第一步:express 表逐级短路搜索 ======
|
||||
// 命中顺序:运单号(waybill_code) → 发货单号(delivery_order_code) → 平台订单号(source_order_codes)。
|
||||
// 每一级只要命中即停止后续查询,直接进入订单详情查询。
|
||||
// 性能背景:单仓库 wms_order_express 行数达 60w+,逐级短路保证绝大多数场景
|
||||
// (运单号/完整发货单号)只走索引点查,模糊全扫仅作为最后兜底。
|
||||
dbg('--- Step1: express level search ---');
|
||||
// 业务规则:运单号先剥离一单多件包裹序号后缀(如 -1-1-),主体必须等值匹配,
|
||||
// 不得落入模糊 like;发货单号/平台交易单号保留模糊匹配能力
|
||||
// 不得落入模糊 like
|
||||
const waybillBase = stripWaybillSegmentSuffix(keyword);
|
||||
const escapedWaybillBase = escapeLikeWildcards(waybillBase);
|
||||
const waybillConditions = [
|
||||
@ -107,36 +140,86 @@ class DeliveryOrderService extends OrderBaseService {
|
||||
...(waybillBase !== keyword ? [{ waybill_code: waybillBase }] : []),
|
||||
...(escapedWaybillBase ? [{ waybill_code: { [Op.like]: `${escapedWaybillBase}-%` } }] : []),
|
||||
];
|
||||
const expressWhere = {
|
||||
warehouse_id: warehouseId,
|
||||
[Op.or]: [
|
||||
...waybillConditions,
|
||||
{ delivery_order_code: { [Op.like]: `%${keyword}%` } },
|
||||
{ source_order_codes: { [Op.like]: `%${keyword}%` } },
|
||||
],
|
||||
};
|
||||
dbg(`expressWhere: ${JSON.stringify(expressWhere)}`);
|
||||
dbg(`waybillBase="${waybillBase}" escapedWaybillBase="${escapedWaybillBase}"`);
|
||||
|
||||
let expressRecords;
|
||||
let expressRecords = [];
|
||||
let hitLevel = null;
|
||||
|
||||
// Level 1:运单号(等值/主体等值/主体前缀,走 waybill_code 索引)
|
||||
try {
|
||||
expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
attributes: [
|
||||
'id', 'order_id', 'waybill_code', 'delivery_order_code', 'source_order_codes', 'store_id',
|
||||
'logistics_code', 'logistics_name',
|
||||
'logistics_interface_id', 'logistics_interface_name',
|
||||
'platform_code', 'status', 'is_back', 'is_intercept', 'is_galaxy_receive',
|
||||
'weighing_weight',
|
||||
],
|
||||
where: expressWhere,
|
||||
attributes: EXPRESS_ATTRIBUTES,
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
[Op.or]: waybillConditions,
|
||||
},
|
||||
raw: true,
|
||||
limit: MAX_RESULTS,
|
||||
});
|
||||
dbg(`expressRecords count: ${expressRecords.length}`);
|
||||
if (expressRecords.length > 0) hitLevel = 'waybill_code';
|
||||
dbg(`L1 waybill_code records: ${expressRecords.length}`);
|
||||
} catch (e) {
|
||||
dbg(`expressRecords ERROR: ${e.message}`);
|
||||
expressRecords = [];
|
||||
dbg(`L1 ERROR: ${e.message}`);
|
||||
}
|
||||
|
||||
// Level 2a:发货单号等值(走 idx_delivery_code_store 索引)
|
||||
if (!hitLevel) {
|
||||
try {
|
||||
expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
attributes: EXPRESS_ATTRIBUTES,
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
delivery_order_code: keyword,
|
||||
},
|
||||
raw: true,
|
||||
limit: MAX_RESULTS,
|
||||
});
|
||||
if (expressRecords.length > 0) hitLevel = 'delivery_order_code';
|
||||
dbg(`L2a delivery_order_code exact records: ${expressRecords.length}`);
|
||||
} catch (e) {
|
||||
dbg(`L2a ERROR: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Level 2b:发货单号模糊兜底(中缀 LIKE 无法走索引,仅在等值未命中时执行)
|
||||
if (!hitLevel) {
|
||||
try {
|
||||
expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
attributes: EXPRESS_ATTRIBUTES,
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
delivery_order_code: { [Op.like]: `%${keyword}%` },
|
||||
},
|
||||
raw: true,
|
||||
limit: MAX_RESULTS,
|
||||
});
|
||||
if (expressRecords.length > 0) hitLevel = 'delivery_order_code_like';
|
||||
dbg(`L2b delivery_order_code like records: ${expressRecords.length}`);
|
||||
} catch (e) {
|
||||
dbg(`L2b ERROR: ${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
// Level 3:平台订单号(无索引全扫兜底,最后一级,仅前两级均未命中时执行)
|
||||
if (!hitLevel) {
|
||||
try {
|
||||
expressRecords = await ctx.model.OrderExpress.findAll({
|
||||
attributes: EXPRESS_ATTRIBUTES,
|
||||
where: {
|
||||
warehouse_id: warehouseId,
|
||||
source_order_codes: { [Op.like]: `%${keyword}%` },
|
||||
},
|
||||
raw: true,
|
||||
limit: MAX_RESULTS,
|
||||
});
|
||||
if (expressRecords.length > 0) hitLevel = 'source_order_codes';
|
||||
dbg(`L3 source_order_codes records: ${expressRecords.length}`);
|
||||
} catch (e) {
|
||||
dbg(`L3 ERROR: ${e.message}`);
|
||||
}
|
||||
}
|
||||
dbg(`hitLevel=${hitLevel}`);
|
||||
|
||||
const waybillMap = new Map();
|
||||
for (const er of expressRecords) {
|
||||
dbg(` er: id=${er.id} order_id=${er.order_id} waybill_code=${er.waybill_code} delivery_order_code=${er.delivery_order_code}`);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user