订单售后费用明细:订单号查询改为先解析候选订单再过滤

订单号原来在「订单 ⋈ 明细」关联后做三列 %x% 匹配,没有日期条件时优化器会放弃
索引,淘系那套 520 万订单 × 680 万明细必须全表扫描,线上实测单条跑到 19 分钟
还没结束(还占满 Apache 工作进程)。

改成两段式:先用带前缀索引的单表查询把订单号解析成两套来源各自的候选 o_id
(线上订单号 / 子订单号从头匹配、内部订单号按数值等值),再让分支按 o_id 过滤。
两套来源分开解析,避免两侧 o_id 撞号。候选超过 500 条或订单号不足 4 个字符时
退回原来的包含匹配,保持既有行为。

顺带给 jst_OrderItem 补 outer_oi_id 前缀索引(非淘系子订单号原来要扫 70 秒),
已在生产建好索引。
This commit is contained in:
renshen 2026-10-07 11:31:52 +08:00
parent e8b8b816f4
commit af83d75a00
3 changed files with 133 additions and 9 deletions

View File

@ -228,6 +228,9 @@ return [
'keys' => ['Id'],
'indexes' => [
['o_id', 'Id'],
// 订单售后费用明细支持按子订单号查询;没有这个索引会让
// outer_oi_id 的匹配退化成上百万行全表扫描(实测 70 秒)。
['outer_oi_id(64)', 'Id'],
],
],
'operations_replica.rpt.jst_sku' => [

View File

@ -13,6 +13,9 @@ class OrderDataMirrorRepository
/** 完整店铺候选的缓存键(页面店铺多选使用)。 */
public const SHOP_NAMES_CACHE_KEY = 'order-data.shop-candidates:v2';
/** 订单号搜索一次最多解析出多少候选订单,超过就退回模糊匹配。 */
private const ORDER_NUMBER_CANDIDATE_LIMIT = 500;
private const DATASETS = [
'operations_replica.rpt.jst_Orders',
'operations_replica.rpt.jst_OrderItem',
@ -239,12 +242,18 @@ class OrderDataMirrorRepository
//
// 筛选条件必须分别下推到两个分支:否则 MariaDB 会先合并上千万行再筛,
// 既用不上各自订单日期索引,也会把连接拖到超时。
//
// 订单号先去两套来源里解析成候选 o_id(单表、走前缀索引),再让分支按 o_id
// 过滤;直接在订单和明细关联后做 %x% 匹配时优化器会放弃索引,淘系那套
// 600 多万行明细要全表扫描,实测 120 秒都跑不完。
$orderCandidates = $this->orderNumberCandidates((string) ($filters['order_no'] ?? ''));
$plain = $this->sourceQuery('ops_jst_orders', 'ops_jst_order_item', 'COALESCE(i.send_warehouse, o.wms_co_id)', 'i.send_warehouse', 'plain');
$this->applyFilters($plain, $filters);
$this->applyFilters($plain, $filters, $orderCandidates['plain'] ?? null);
$plain->orderByDesc('o.order_date')->orderByDesc('o.o_id')->limit($perBranch);
$taobao = $this->sourceQuery('ops_jst_qmcust_orders', 'ops_jst_qmcust_order_items', 'o.wms_co_id', 'o.wms_co_id', 'taobao');
$this->applyFilters($taobao, $filters);
$this->applyFilters($taobao, $filters, $orderCandidates['taobao'] ?? null);
$taobao->orderByDesc('o.order_date')->orderByDesc('o.o_id')->limit($perBranch);
return $this->connection->query()->fromSub(
@ -253,6 +262,60 @@ class OrderDataMirrorRepository
);
}
/**
* 把订单号解析成两套来源各自的候选 o_id。
*
* 订单号字段(内部/线上/子订单号)在镜像里都有前缀索引,等值或"从头匹配"能走索引;
* 返回 null 表示不能用快路径(订单号太短、候选过多,或本身就是模糊匹配),
* 调用方退回原来的 %x% 匹配。
*
* @return array{plain:list<int>,taobao:list<int>}|null
*/
private function orderNumberCandidates(string $raw): ?array
{
$needle = trim($raw);
if ($needle === '' || mb_strlen($needle) < 4) {
return null;
}
$prefix = $this->likePrefix($needle);
$digits = ctype_digit($needle) ? (int) $needle : null;
$candidates = [];
foreach ([
'plain' => ['ops_jst_orders', 'ops_jst_order_item'],
'taobao' => ['ops_jst_qmcust_orders', 'ops_jst_qmcust_order_items'],
] as $branch => [$ordersTable, $itemsTable]) {
$ids = [];
$orders = $this->connection->table($ordersTable)
->select('o_id')
->whereRaw("so_id LIKE ? ESCAPE '!'", [$prefix]);
if ($digits !== null) {
$orders->orWhere('o_id', '=', $digits);
}
foreach ($orders->limit(self::ORDER_NUMBER_CANDIDATE_LIMIT + 1)->pluck('o_id') as $id) {
$ids[(int) $id] = true;
}
foreach ($this->connection->table($itemsTable)
->select('o_id')
->whereRaw("outer_oi_id LIKE ? ESCAPE '!'", [$prefix])
->limit(self::ORDER_NUMBER_CANDIDATE_LIMIT + 1)
->pluck('o_id') as $id) {
$ids[(int) $id] = true;
}
if (count($ids) > self::ORDER_NUMBER_CANDIDATE_LIMIT) {
return null;
}
$candidates[$branch] = array_map('intval', array_keys($ids));
}
return $candidates;
}
private function sourceQuery(
string $ordersTable,
string $itemsTable,
@ -269,7 +332,8 @@ class OrderDataMirrorRepository
->selectRaw('? as source_branch', [$branch]);
}
private function applyFilters(Builder $query, array $filters): void
/** @param list<int>|null $orderIds 订单号已解析出的候选 o_id;null 表示退回模糊匹配 */
private function applyFilters(Builder $query, array $filters, ?array $orderIds = null): void
{
if (($filters['from'] ?? '') !== '') {
$query->where('o.order_date', '>=', $filters['from'] . ' 00:00:00');
@ -278,12 +342,19 @@ class OrderDataMirrorRepository
$query->where('o.order_date', '<', date('Y-m-d 00:00:00', strtotime($filters['to'] . ' +1 day')));
}
if (($filters['order_no'] ?? '') !== '') {
$needle = $this->contains((string) $filters['order_no']);
$query->where(function (Builder $order) use ($needle): void {
$order->whereRaw("CAST(o.o_id AS CHAR) LIKE ? ESCAPE '!'", [$needle])
->orWhereRaw("o.so_id LIKE ? ESCAPE '!'", [$needle])
->orWhereRaw("i.outer_oi_id LIKE ? ESCAPE '!'", [$needle]);
});
if ($orderIds === null) {
$needle = $this->contains((string) $filters['order_no']);
$query->where(function (Builder $order) use ($needle): void {
$order->whereRaw("CAST(o.o_id AS CHAR) LIKE ? ESCAPE '!'", [$needle])
->orWhereRaw("o.so_id LIKE ? ESCAPE '!'", [$needle])
->orWhereRaw("i.outer_oi_id LIKE ? ESCAPE '!'", [$needle]);
});
} elseif ($orderIds === []) {
// 两套来源都没有候选订单,直接让分支返回空集,避免白扫一遍。
$query->whereRaw('1 = 0');
} else {
$query->whereIn('o.o_id', $orderIds);
}
}
$shopNames = array_values(array_filter(array_map(
static fn (mixed $name): string => trim((string) $name),
@ -556,4 +627,10 @@ class OrderDataMirrorRepository
{
return '%' . str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($value)) . '%';
}
/** 转义后的"从头匹配"模式,配合 so_id / outer_oi_id 的前缀索引使用。 */
private function likePrefix(string $value): string
{
return str_replace(['!', '%', '_'], ['!!', '!%', '!_'], trim($value)) . '%';
}
}

View File

@ -198,6 +198,50 @@ class OrderDataMirrorRepositoryTest extends TestCase
$this->assertContains('天猫-卓禾家具旗舰店', $names);
}
public function test_after_sales_order_number_is_resolved_to_candidates_before_filtering(): void
{
$db = DB::connection('business_mirror');
$this->insertOrder($db, 1, 'SO-2026-0001', '拼多多-甲店');
$this->insertOrder($db, 2, 'SO-2026-0002', '拼多多-乙店');
$repository = $this->repository($db);
$base = ['from' => '', 'to' => '', 'shop_name' => '', 'sku_id' => ''];
// 完整线上订单号:只返回这一单。
$exact = $repository->afterSales(array_replace($base, ['order_no' => 'SO-2026-0001']), 1, 25);
$this->assertSame(['SO-2026-0001'], $this->sortedOnlineOrders($exact['rows']));
// 子订单号(明细表的 outer_oi_id)同样能定位到订单。
$sub = $repository->afterSales(array_replace($base, ['order_no' => 'SUB-2']), 1, 25);
$this->assertSame(['SO-2026-0002'], $this->sortedOnlineOrders($sub['rows']));
// 查不到的单号直接返回空,不再对整张宽表做包含匹配。
$missing = $repository->afterSales(array_replace($base, ['order_no' => 'SO-9999-0000']), 1, 25);
$this->assertSame([], $missing['rows']);
}
public function test_after_sales_short_order_fragment_keeps_contains_match(): void
{
$db = DB::connection('business_mirror');
$this->insertOrder($db, 1, 'SO-2026-0001', '拼多多-甲店');
$this->insertOrder($db, 2, 'SO-2026-0002', '拼多多-乙店');
$result = $this->repository($db)->afterSales([
'from' => '', 'to' => '', 'order_no' => 'SO-', 'shop_name' => '', 'sku_id' => '',
], 1, 25);
$this->assertSame(['SO-2026-0001', 'SO-2026-0002'], $this->sortedOnlineOrders($result['rows']));
}
/** @param array<int,array<string,mixed>> $rows @return list<string> */
private function sortedOnlineOrders(array $rows): array
{
$numbers = array_map(static fn (array $row): string => (string) ($row['online_order_no'] ?? ''), $rows);
sort($numbers);
return array_values($numbers);
}
private function repository(\Illuminate\Database\ConnectionInterface $db): OrderDataMirrorRepository
{
$mirror = Mockery::mock(GenericBusinessMirrorRepository::class);