全项目数据库性能优化
This commit is contained in:
@@ -47,13 +47,15 @@ export async function createRealtimeEvent(input: {
|
||||
const row = result.rows[0]
|
||||
if (!row) throw new Error('realtime_event_create_failed')
|
||||
|
||||
// 事件仅用于短期断线补发,控制表体积和重连回放成本。
|
||||
await query(
|
||||
`
|
||||
DELETE FROM realtime_events
|
||||
WHERE id < GREATEST(0, (SELECT COALESCE(MAX(id), 0) - 500 FROM realtime_events))
|
||||
`,
|
||||
)
|
||||
// 事件仅用于短期断线补发;每 100 条清理一次,避免每次业务事件都额外触发 DELETE。
|
||||
if (Number(row.id) % 100 === 0) {
|
||||
await query(
|
||||
`
|
||||
DELETE FROM realtime_events
|
||||
WHERE id < GREATEST(0, (SELECT COALESCE(MAX(id), 0) - 500 FROM realtime_events))
|
||||
`,
|
||||
)
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
|
||||
@@ -26,22 +26,21 @@ import { maybeUpgradeWorkerLevelWithClient } from './worker-repo.js'
|
||||
const WORK_ORDER_SELECT = `
|
||||
SELECT
|
||||
wo.*,
|
||||
COALESCE((
|
||||
SELECT SUM(wos.quantity)
|
||||
FROM work_order_shares wos
|
||||
WHERE wos.work_order_id = wo.id AND wos.status != 'cancelled'
|
||||
), 0)::int AS active_share_quantity,
|
||||
COALESCE((
|
||||
SELECT COUNT(*)
|
||||
FROM work_order_shares wos
|
||||
WHERE wos.work_order_id = wo.id AND wos.status = 'joined'
|
||||
), 0)::int AS pending_share_count,
|
||||
COALESCE(share_summary.active_share_quantity, 0)::int AS active_share_quantity,
|
||||
COALESCE(share_summary.pending_share_count, 0)::int AS pending_share_count,
|
||||
wc.name AS category_name,
|
||||
wu.username AS worker_username,
|
||||
wu.display_name AS worker_display_name
|
||||
FROM work_orders wo
|
||||
LEFT JOIN work_categories wc ON wc.id = wo.category_id
|
||||
LEFT JOIN worker_users wu ON wu.id = wo.assigned_worker_id
|
||||
LEFT JOIN LATERAL (
|
||||
SELECT
|
||||
SUM(wos.quantity) FILTER (WHERE wos.status != 'cancelled') AS active_share_quantity,
|
||||
COUNT(*) FILTER (WHERE wos.status = 'joined') AS pending_share_count
|
||||
FROM work_order_shares wos
|
||||
WHERE wos.work_order_id = wo.id
|
||||
) share_summary ON TRUE
|
||||
`
|
||||
|
||||
const WORK_PRODUCT_RULE_SELECT = `
|
||||
@@ -106,7 +105,22 @@ type WorkOrderStatisticsRow = {
|
||||
}
|
||||
|
||||
/** 后台按母工单拼单参与情况计算业务状态,open 只代表大厅生命周期。 */
|
||||
function adminOperationalStatusExpression(alias = 'wo') {
|
||||
function adminOperationalStatusExpression(alias = 'wo', shareSummaryAlias = '') {
|
||||
if (shareSummaryAlias) {
|
||||
return `CASE
|
||||
WHEN ${alias}.status = 'open'
|
||||
AND ${alias}.sharing_enabled IS TRUE
|
||||
AND COALESCE(${shareSummaryAlias}.active_share_quantity, 0) > 0
|
||||
THEN CASE
|
||||
WHEN COALESCE(${shareSummaryAlias}.active_share_quantity, 0) >= ${alias}.sharing_total_quantity
|
||||
AND COALESCE(${shareSummaryAlias}.pending_share_count, 0) = 0
|
||||
THEN 'pending_acceptance'
|
||||
ELSE 'in_progress'
|
||||
END
|
||||
ELSE ${alias}.status
|
||||
END`
|
||||
}
|
||||
|
||||
return `CASE
|
||||
WHEN ${alias}.status = 'open'
|
||||
AND ${alias}.sharing_enabled IS TRUE
|
||||
@@ -1324,7 +1338,7 @@ export async function getWorkOrderStatistics(input: ListInput = {}): Promise<Wor
|
||||
AND NULLIF(wo.acceptance_json->>'evidenceDueAt', '')::timestamptz > NOW()
|
||||
`
|
||||
const operationalStatus = input.useOperationalStatus
|
||||
? adminOperationalStatusExpression()
|
||||
? adminOperationalStatusExpression('wo', 'operational_share_summary')
|
||||
: 'wo.status'
|
||||
const statusExpression = input.vipEvidencePending
|
||||
? `CASE WHEN ${vipEvidencePendingWhere} THEN 'vip_evidence_pending' ELSE ${operationalStatus} END`
|
||||
@@ -4357,6 +4371,29 @@ function buildWorkOrderWhere({
|
||||
let joins = ''
|
||||
let workerSharePlaceholder = ''
|
||||
|
||||
if (useOperationalStatus) {
|
||||
joins += `
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
wos.work_order_id,
|
||||
SUM(wos.quantity) FILTER (WHERE wos.status != 'cancelled') AS active_share_quantity,
|
||||
COUNT(*) FILTER (WHERE wos.status = 'joined') AS pending_share_count
|
||||
FROM work_order_shares wos
|
||||
GROUP BY wos.work_order_id
|
||||
) operational_share_summary ON operational_share_summary.work_order_id = wo.id`
|
||||
}
|
||||
|
||||
if (excludeFilledSharing) {
|
||||
joins += `
|
||||
LEFT JOIN (
|
||||
SELECT
|
||||
wos.work_order_id,
|
||||
SUM(wos.quantity) FILTER (WHERE wos.status != 'cancelled') AS active_share_quantity
|
||||
FROM work_order_shares wos
|
||||
GROUP BY wos.work_order_id
|
||||
) filter_share_summary ON filter_share_summary.work_order_id = wo.id`
|
||||
}
|
||||
|
||||
// 先从打手的整单、历史单和拼单记录组成可见工单集合,再关联工单详情。
|
||||
// 这样 COUNT 和列表都不会先扫描全量工单,再逐行检查该打手的拼单状态。
|
||||
if (workerSharingId) {
|
||||
@@ -4403,7 +4440,7 @@ function buildWorkOrderWhere({
|
||||
if (normalizedStatuses.length > 0) {
|
||||
params.push(normalizedStatuses)
|
||||
filters.push(
|
||||
`(${useOperationalStatus ? adminOperationalStatusExpression() : 'wo.status'} = ANY($${params.length}::text[]) OR (${vipEvidencePendingWhere}))`,
|
||||
`(${useOperationalStatus ? adminOperationalStatusExpression('wo', 'operational_share_summary') : 'wo.status'} = ANY($${params.length}::text[]) OR (${vipEvidencePendingWhere}))`,
|
||||
)
|
||||
} else {
|
||||
filters.push(`(${vipEvidencePendingWhere})`)
|
||||
@@ -4412,12 +4449,10 @@ function buildWorkOrderWhere({
|
||||
params.push(normalizedStatuses)
|
||||
const statusesPlaceholder = `$${params.length}`
|
||||
if (workerSharingId) {
|
||||
filters.push(
|
||||
`${workerOrderStatusExpression()} = ANY(${statusesPlaceholder}::text[])`,
|
||||
)
|
||||
filters.push(`${workerOrderStatusExpression()} = ANY(${statusesPlaceholder}::text[])`)
|
||||
} else {
|
||||
filters.push(
|
||||
`${useOperationalStatus ? adminOperationalStatusExpression() : 'wo.status'} = ANY(${statusesPlaceholder}::text[])`,
|
||||
`${useOperationalStatus ? adminOperationalStatusExpression('wo', 'operational_share_summary') : 'wo.status'} = ANY(${statusesPlaceholder}::text[])`,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -4472,12 +4507,7 @@ function buildWorkOrderWhere({
|
||||
if (excludeFilledSharing) {
|
||||
filters.push(`(
|
||||
wo.sharing_enabled IS NOT TRUE
|
||||
OR COALESCE((
|
||||
SELECT SUM(wos.quantity)
|
||||
FROM work_order_shares wos
|
||||
WHERE wos.work_order_id = wo.id
|
||||
AND wos.status != 'cancelled'
|
||||
), 0) < wo.sharing_total_quantity
|
||||
OR COALESCE(filter_share_summary.active_share_quantity, 0) < wo.sharing_total_quantity
|
||||
)`)
|
||||
}
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user