修复运费对账汇总卡死与订单店铺候选查询过慢

- 运费对账全量汇总:哈希关联的 join buffer 从 16MB 提到 128MB(空间上限 384MB)。
  300 万行明细 × 400 万行最新关联表在 16MB 下会退化成磁盘多趟扫描,实测超过
  max_statement_time(300s) 也跑不完,页面「记录数/运费合计」永远停在计算中;
  调整后约 110 秒出结果并写入缓存。
- 订单售后费用明细店铺候选:改回两个来源各自 DISTINCT 后在 PHP 合并(UNION 会
  把两个上百万行结果一起物化去重排序)。同时完整候选只算一次并缓存 24 小时,
  冷缓存时先返回毫秒级的店铺目录,完整列表放到响应结束后补齐,避免页面为了
  下拉候选把工作进程占住两分钟。
This commit is contained in:
renshen 2026-10-07 10:52:35 +08:00
parent 40832546da
commit af8f1403fb
3 changed files with 115 additions and 22 deletions

View File

@ -224,8 +224,11 @@ class CourierFreightReconciliationMirrorRepository
$settings += [
'optimizer_switch' => 'derived_merge=off,derived_with_keys=off',
'join_cache_level' => 8,
'join_buffer_size' => 16777216,
'join_buffer_space_limit' => 33554432,
// 全量汇总要把 300 万行左右的明细和 400 万行的最新关联表做哈希
// 关联;16MB 的哈希表装不下会退化成磁盘多趟扫描(实测超过
// max_statement_time 也跑不完)。128MB 缓冲约 110 秒出结果。
'join_buffer_size' => 134217728,
'join_buffer_space_limit' => 402653184,
];
}
foreach ($settings as $name => $value) {

View File

@ -4,10 +4,15 @@ namespace Modules\EcommerceFinance\Repositories;
use Illuminate\Database\ConnectionInterface;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Cache;
use Modules\EcommerceFinance\Services\BusinessMirrorDatasetRegistry;
use Throwable;
class OrderDataMirrorRepository
{
/** 完整店铺候选的缓存键(页面店铺多选使用)。 */
public const SHOP_NAMES_CACHE_KEY = 'order-data.shop-candidates:v2';
private const DATASETS = [
'operations_replica.rpt.jst_Orders',
'operations_replica.rpt.jst_OrderItem',
@ -42,20 +47,107 @@ class OrderDataMirrorRepository
*/
public function shopNames(): array
{
// 非淘系 + 淘系两个来源合并去重(淘系店铺名形如"天猫-xx/淘宝-xx")。
$plain = $this->connection->table('ops_jst_orders')
->select('shop_name')->whereNotNull('shop_name')->where('shop_name', '<>', '');
$taobao = $this->connection->table('ops_jst_qmcust_orders')
->select('shop_name')->whereNotNull('shop_name')->where('shop_name', '<>', '');
if (is_array($cached = Cache::get(self::SHOP_NAMES_CACHE_KEY))) {
return array_values($cached);
}
return $plain->union($taobao)
->orderBy('shop_name')
->get()
->pluck('shop_name')
->map(static fn (mixed $name): string => (string) $name)
->unique()
->values()
->all();
// 完整候选要扫订单宽表(shop_name 是 LONGTEXT,非淘系约 30 秒、淘系约
// 2 分钟),不能卡在页面请求里。冷缓存时先返回毫秒级的店铺目录,
// 完整列表放到响应结束后算好再缓存 24 小时。
$this->warmShopNamesCache();
$directory = $this->shopDirectoryNames();
return $directory !== [] ? $directory : $this->computedShopNames();
}
/** 聚水潭店铺目录(约 1300 行):毫秒级返回,作为冷缓存时的候选兜底。 */
private function shopDirectoryNames(): array
{
try {
$rows = $this->connection->table('ops_jst_shop_info')
->select('shop_name')
->distinct()
->whereNotNull('shop_name')
->where('shop_name', '<>', '')
->pluck('shop_name');
} catch (Throwable $exception) {
report($exception);
return [];
}
return $this->normalizeShopNames($rows);
}
private function warmShopNamesCache(): void
{
app()->terminating(function (): void {
$lock = Cache::lock(self::SHOP_NAMES_CACHE_KEY . ':warm', 300);
if (! $lock->get()) {
return;
}
try {
$names = $this->computedShopNames();
if ($names !== []) {
Cache::put(self::SHOP_NAMES_CACHE_KEY, $names, now()->addHours(24));
}
} catch (Throwable $exception) {
report($exception);
} finally {
$lock->release();
}
});
}
/**
* 非淘系 + 淘系两个来源合并去重(淘系店铺名形如"天猫-xx/淘宝-xx")。
*
* 分开各自 DISTINCT 再在 PHP 合并:UNION 会让 MariaDB 把两个上百万行的结果
* 一起物化去重再排序,单次要两三分钟。这里只在响应结束后或缓存不可用时执行。
*
* @return array<int,string>
*/
private function computedShopNames(): array
{
$names = [];
foreach (['ops_jst_orders', 'ops_jst_qmcust_orders'] as $table) {
$rows = $this->connection->table($table)
->select('shop_name')
->distinct()
->whereNotNull('shop_name')
->where('shop_name', '<>', '')
->pluck('shop_name');
foreach ($this->normalizeShopNames($rows) as $name) {
$names[$name] = true;
}
}
$names = array_keys($names);
sort($names, SORT_STRING);
return $names;
}
/** @param iterable<mixed> $rows @return array<int,string> */
private function normalizeShopNames(iterable $rows): array
{
$names = [];
foreach ($rows as $name) {
$name = trim((string) $name);
if ($name !== '') {
$names[$name] = true;
}
}
$names = array_keys($names);
sort($names, SORT_STRING);
return $names;
}
public function promotionReady(): bool

View File

@ -4,7 +4,6 @@ namespace Modules\EcommerceFinance\View;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Cache;
use Modules\EcommerceFinance\Repositories\OrderDataRepository;
use Modules\EcommerceFinance\Repositories\OrderDataMirrorRepository;
use Modules\EcommerceFinance\Repositories\WorkOrderMirrorRepository;
@ -118,7 +117,10 @@ class OrderDataPageData
}
/**
* 店铺多选候选:订单镜像里出现过的店铺名称(缓存 1 小时,避免每次页面请求都扫表去重)。
* 店铺多选候选:订单镜像里出现过的店铺名称。
*
* 完整候选由 OrderDataMirrorRepository::shopNames() 自己缓存 24 小时;冷缓存时
* 它先返回毫秒级的店铺目录,避免页面为了下拉候选卡住两分钟。
*
* @return array<int,string>
*/
@ -126,11 +128,7 @@ class OrderDataPageData
{
try {
$mirror = $this->mirrorOrders ?? app(OrderDataMirrorRepository::class);
$names = Cache::remember(
'order-data.after-sales.shop-options',
now()->addHour(),
fn (): array => $mirror->shopNames(),
);
$names = $mirror->shopNames();
return is_array($names) ? array_values($names) : [];
} catch (Throwable $exception) {