Files
SupplierSystem/migrations/20261075_report_push_queue_and_indexes.sql
T
Ubuntu d0206ff395
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
perf(报告推送): 全量轮询改事件驱动队列(变化点入队+按库存行/订单号精确推送)+ 补 6 类高频索引;全量扫描降为每日兜底
2026-10-24 12:45:49 +08:00

83 lines
4.0 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
-- 批次报告推送:**事件驱动队列** + 增量/查询索引(2026-10-24 开发者:
-- 「肯定不能每次推送都扫描全部呀 应该是存在改动变化的才会进行推送 而不是全盘扫描 且 优化也需要做」)
--
-- 背景(规模风险,代码事实):
-- · 旧实现每 15 分钟对**每个分公司全量**跑一遍报告候选查询(matches→reports→inventory→order_items→orders
-- + 两个 EXISTS:picking_items 的 JSONB 数组、inventory_logs 的批次数);
-- · 生产索引里 `picking_items` 没有 `order_item_id`、`inventory_logs` 没有 `(related_order_code, …)`、
-- `order_items` 没有 `(branch_code, sku_code)` ⇒ 几万批次 × 几百家时会退化成全表扫。
--
-- 本迁移只做 DDL:
-- ① 新建 public.main_system_report_push_queue —— "哪里变了就入队一条",worker 只处理队列里的精确目标;
-- ② 给上面那几个高频过滤列补索引(含 inventory_batch_report_matches.batch_no,删报告时要按批次反查)。
CREATE TABLE IF NOT EXISTS public.main_system_report_push_queue (
id BIGSERIAL PRIMARY KEY,
branch_code VARCHAR(20) NOT NULL,
/** register=该批次/该订单行报告有变化,需要(重新)登记到主系统;void=本地删了报告,需要撤下主系统那份 */
kind VARCHAR(16) NOT NULL,
/** 报告侧变化的定位键:库存行 id(有索引,最精确) */
inventory_id UUID,
/** 订单侧变化的定位键:我方订单号(导入/追加后入队) */
our_order_code VARCHAR(50),
/** 诊断用 */
batch_no VARCHAR(80),
sku_code VARCHAR(64),
reason VARCHAR(120),
status VARCHAR(16) NOT NULL DEFAULT 'pending',
attempts INTEGER NOT NULL DEFAULT 0,
last_error TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
-- 同一目标只保留一条待处理(重复入队直接忽略)
CREATE UNIQUE INDEX IF NOT EXISTS uq_report_push_queue_pending
ON public.main_system_report_push_queue (
branch_code, kind,
COALESCE(inventory_id::text, ''),
COALESCE(our_order_code, '')
)
WHERE status = 'pending';
CREATE INDEX IF NOT EXISTS idx_report_push_queue_pick
ON public.main_system_report_push_queue (status, branch_code, id);
CREATE INDEX IF NOT EXISTS idx_report_push_queue_created
ON public.main_system_report_push_queue (created_at);
-- ② 逐分公司补索引(缺索引会让上面的候选查询退化为全表扫)
DO $$
DECLARE
sch text;
BEGIN
FOR sch IN
SELECT nspname FROM pg_namespace
WHERE nspname = 'tenant_template' OR nspname LIKE 'tenant\_%'
LOOP
IF to_regclass(format('%I.order_items', sch)) IS NOT NULL THEN
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_order_items_branch_sku ON %I.order_items (branch_code, sku_code)', sch);
END IF;
IF to_regclass(format('%I.picking_items', sch)) IS NOT NULL THEN
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_picking_items_order_item ON %I.picking_items (order_item_id)', sch);
END IF;
IF to_regclass(format('%I.inventory_logs', sch)) IS NOT NULL THEN
-- 报告候选的"源②实际出库流水"按 (related_order_code, sku_code, batch_no, operation_type) 精确命中
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_inventory_logs_order_sku_batch ON %I.inventory_logs (related_order_code, sku_code, batch_no, operation_type)', sch);
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_inventory_logs_batch_no ON %I.inventory_logs (batch_no)', sch);
END IF;
IF to_regclass(format('%I.inventory_batch_report_matches', sch)) IS NOT NULL THEN
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_batch_report_matches_batch_no ON %I.inventory_batch_report_matches (batch_no)', sch);
END IF;
IF to_regclass(format('%I.inventory', sch)) IS NOT NULL THEN
EXECUTE format(
'CREATE INDEX IF NOT EXISTS idx_inventory_batch_no ON %I.inventory (batch_no)', sch);
END IF;
END LOOP;
END $$;