perf: 全量物化视图优化 — 按月预计算所有慢函数,API响应从106秒降至0.09秒

- 创建12个按月物化视图替代实时函数调用
- phase10: mv_dish_sku_summary_monthly, mv_dish_sku_abc_monthly
- phase11: mv_store_risk_rating/platform_economics/category_mix/member_opportunity/benchmark_composite/deep_diagnosis/dish_basket/dish_store_summary/theoretical_actual_cost (全部_monthly)
- 后端所有路由从 fn_xxx() 改为 mv_xxx_monthly WHERE month_start =
- 新增 analytics.refresh_all_monthly_views() 刷新函数
- 优化 fn_dish_sku_summary: CTE物化避免重复调用
- 优化 fn_dish_sku_abc: CTE物化 fn_dish_sku_summary 只调用一次
- 优化 fn_dish_sales: 添加 dish_name IS NOT NULL 提前过滤
This commit is contained in:
freedakgmail
2026-08-01 14:05:12 +08:00
parent dab06ba109
commit 25db0f3854
22 changed files with 2761 additions and 71 deletions
@@ -480,9 +480,16 @@ RETURNS TABLE (
b.taobao_delivery_received,
b.jd_delivery_received
FROM public.dish_sales_details d
JOIN analytics.bill_fact b ON b.store_code = d.store_code AND b.bill_no = d.bill_no
WHERE b.closed_at >= analytics.fn_month_start_ts(p_month)
AND b.closed_at < analytics.fn_next_month_start_ts(p_month);
JOIN (
SELECT store_code, bill_no, closed_at, meal_period, guest_count, consumption,
received_total, discount_total, member_id, member_level, marketing_plan,
order_tag, theoretical_cost, theoretical_profit, theoretical_margin,
meituan_delivery_received, taobao_delivery_received, jd_delivery_received
FROM analytics.bill_fact
WHERE closed_at >= analytics.fn_month_start_ts(p_month)
AND closed_at < analytics.fn_next_month_start_ts(p_month)
) b ON b.store_code = d.store_code AND b.bill_no = d.bill_no
WHERE d.dish_name IS NOT NULL;
$$ LANGUAGE sql STABLE;
-- ============================================================