perf(finance): 管道增量索引与异常批量入队

生产副本 42 万单压测发现:
- carrier_missing 查询在 26 万条上 15s 超时 → 改 anti-join,异常去重改批量
- 缺成本异常逐条 3 条 SQL,12 万人工单会跑几十分钟 → 按 scope 批量入队
- 新增 117/118 部分索引,成本快照/人工挂配置/载体检查走 id 顺序扫描
- 人工挂配置查询改为按配置 SKU 数组过滤,避免全量人工单排序
This commit is contained in:
yml2213
2026-11-01 16:22:52 +08:00
parent 5d7904f6ee
commit 8391912bda
5 changed files with 184 additions and 29 deletions
@@ -1,7 +1,11 @@
import type { JsonObject } from '../../types/json.js'
import { query } from '../../db/client.js'
import { assignPendingOrderItemChannels } from '../fulfillment/channel-assign-service.js'
import { resolveExceptionsForItemsByReason } from '../cost-center/cost-exception-service.js'
import {
resolveExceptionsForItemsByReason,
upsertAggregatedExceptionsBatch,
type AggregatedExceptionEntry,
} from '../cost-center/cost-exception-service.js'
import { readWatermarkItemId } from '../watermark-store.js'
import { refreshFactsForDates } from '../finance/refresh-facts.js'
import { rebuildChannelSalesDailyFacts } from '../finance/sales-facts-channel-service.js'
@@ -14,7 +18,6 @@ import {
syncChannelCostSnapshots,
syncCostSnapshotsForItems,
} from '../cost-center/cost-snapshot-service.js'
import { upsertAggregatedException } from '../cost-center/cost-exception-service.js'
import { formatShanghaiDate, shiftDate } from '../../utils/finance-date-range.js'
import {
recordPipelineError,
@@ -84,6 +87,10 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
? Math.max(0, Math.trunc(Number(config.afterId)))
: await readWatermarkItemId('carrier_dispatch')
let maxId = 0
const configKeysResult = await query<{ sku_key: string }>(
'SELECT sku_key FROM manual_fulfillment_configs WHERE enabled',
)
const configKeys = configKeysResult.rows.map((row) => row.sku_key)
const manual = await query<{ id: number; sku_key: string; config_id: number }>(
`
@@ -93,11 +100,12 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
WHERE oi.fulfillment_status = 'assigned'
AND oi.fulfillment_channel = 'manual'
AND oi.fulfillment_config_id IS NULL
AND oi.manual_sku_key = ANY($3::text[])
AND oi.id > $2
ORDER BY oi.id
LIMIT $1
`,
[limit, cursor],
[limit, cursor, configKeys],
)
let attached = 0
@@ -121,10 +129,7 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
WHERE oi.fulfillment_status = 'assigned'
AND oi.fulfillment_channel = 'manual'
AND oi.fulfillment_config_id IS NULL
AND NOT EXISTS (
SELECT 1 FROM manual_fulfillment_configs cfg
WHERE cfg.sku_key = oi.manual_sku_key AND cfg.enabled
)
AND NOT (oi.manual_sku_key = ANY($3::text[]))
AND NOT EXISTS (
SELECT 1
FROM cost_exception_items i
@@ -137,14 +142,15 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
ORDER BY oi.id
LIMIT $1
`,
[limit, cursor],
[limit, cursor, configKeys],
)
const exceptionEntries: AggregatedExceptionEntry[] = []
for (const row of missingManual.rows) {
await upsertAggregatedException({
exceptionEntries.push({
reason: 'manual_config_missing',
scopeKey: `manual_config_missing|-|-|${row.sku_key}|manual`,
orderItemId: row.id,
orderItemId: Number(row.id),
detail: { skuKey: row.sku_key },
})
}
@@ -179,37 +185,28 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
)
for (const row of pendingManualCost.rows) {
await upsertAggregatedException({
exceptionEntries.push({
reason: 'manual_cost_missing',
scopeKey: `manual_cost_missing|-|-|${row.sku_key}|manual`,
orderItemId: row.id,
orderItemId: Number(row.id),
detail: { skuKey: row.sku_key },
})
}
// 自动发货 / 接单平台被运营指定后却没有载体:不能静默卡住,进问题队列。
const missingCarrier = await query<{ id: number; channel: string; sku_code: string }>(
const missingCarrierRaw = await query<{ id: number; channel: string; sku_code: string }>(
`
SELECT oi.id, oi.fulfillment_channel AS channel, oi.sku_code
FROM order_items oi
LEFT JOIN work_orders wo ON wo.order_item_id = oi.id
WHERE oi.fulfillment_status = 'assigned'
AND oi.fulfillment_channel IN ('affiliate_dash', 'work_order')
AND wo.id IS NULL
AND NOT EXISTS (
SELECT 1 FROM fulfillment_tasks ft
WHERE ft.order_item_id = oi.id
AND ft.executor_key IN ('affiliate_dash', 'third_party_voucher')
)
AND NOT EXISTS (
SELECT 1 FROM work_orders wo WHERE wo.order_item_id = oi.id
)
AND NOT EXISTS (
SELECT 1
FROM cost_exception_items i
JOIN cost_exceptions ce ON ce.id = i.exception_id
WHERE i.order_item_id = oi.id
AND ce.reason = 'carrier_missing'
AND ce.status <> 'resolved'
)
AND oi.id > $2
ORDER BY oi.id
LIMIT $1
@@ -217,14 +214,33 @@ export async function runCarrierDispatchJob(job: JsonObject = {}) {
[limit, cursor],
)
const queuedCarrier = await query<{ order_item_id: number }>(
`
SELECT DISTINCT i.order_item_id
FROM cost_exception_items i
JOIN cost_exceptions ce ON ce.id = i.exception_id
WHERE i.order_item_id = ANY($1::bigint[])
AND ce.reason = 'carrier_missing'
AND ce.status <> 'resolved'
`,
[missingCarrierRaw.rows.map((row) => row.id)],
)
const queuedSet = new Set(queuedCarrier.rows.map((row) => Number(row.order_item_id)))
const missingCarrier = {
rows: missingCarrierRaw.rows.filter((row) => !queuedSet.has(Number(row.id))),
}
for (const row of missingCarrier.rows) {
await upsertAggregatedException({
exceptionEntries.push({
reason: 'carrier_missing',
scopeKey: `carrier_missing|${row.channel}|${row.sku_code}`,
orderItemId: row.id,
orderItemId: Number(row.id),
detail: { channel: row.channel, skuCode: row.sku_code },
})
}
if (exceptionEntries.length > 0) {
await upsertAggregatedExceptionsBatch(exceptionEntries)
}
for (const row of [
...manual.rows,