perf(selfop): paginate sales summary before day coverage enrichment

This commit is contained in:
2026-10-13 02:46:20 +08:00
parent 341b7635c7
commit 1fdd978084
3 changed files with 107 additions and 6 deletions
@@ -95,11 +95,32 @@ public final class AnalyticsReadSql {
String rangedHint=request.from()!=null&&request.until()!=null
&&java.time.temporal.ChronoUnit.DAYS.between(request.from(),request.until())==366
?"/*+ INDEX(d idx_selfop_daily_sku_cover) */ ":"";
sql=sql.substring(0,rangeStart)+" ranged AS (SELECT "+rangedHint+"d.customer_code,d.sku_code,SUM(d.outbound_quantity) observed_sum,"
// Sorting needs only the observed quantity and its validity. The daily unique
// key makes observed_count the union cardinality of actual days and the
// continuous observed date interval: it is positive iff either is nonempty.
// Strict UTC intersection rejects empty/reversed intervals without computing
// every candidate's full/partial day boundaries before the page is selected.
String observedIntersection="e.evidence_at_utc IS NOT NULL AND #{p.coveredFrom} IS NOT NULL AND #{p.coveredUntil} IS NOT NULL "
+"AND GREATEST(e.evidence_at_utc,#{p.coveredFrom},CONVERT_TZ(CAST(#{p.from} AS DATETIME),'+08:00','+00:00'))"
+"<LEAST(#{p.coveredUntil},CONVERT_TZ(CAST(#{p.until} AS DATETIME),'+08:00','+00:00'))";
String sortPage=" sort_sales AS (SELECT "+rangedHint+"d.customer_code,d.sku_code,SUM(d.outbound_quantity) observed_sum,"
+"COUNT(*) actual_days,MIN(d.quantity_unit) min_unit,MAX(d.quantity_unit) max_unit "
+"FROM ll_selfop_sku_daily_sales d WHERE d.sales_run_id=#{p.salesRun} AND"+KEY_FILTER
+"AND d.business_date>=#{p.from} AND d.business_date<#{p.until} GROUP BY d.customer_code,d.sku_code),"
+" rankable AS (SELECT e.*,CASE WHEN e.profile_unit_valid=1 "
+"AND (s.min_unit IS NULL OR (s.min_unit=e.quantity_unit AND s.max_unit=e.quantity_unit)) "
+"AND #{p.salesKnown}=1 AND (COALESCE(s.actual_days,0)>0 OR ("+observedIntersection+")) "
+"THEN COALESCE(s.observed_sum,0) END sort_observed_quantity "
+"FROM evidence e LEFT JOIN sort_sales s ON s.customer_code=e.customer_code AND s.sku_code=e.sku_code),"
+" paged_keys AS (SELECT * FROM rankable ORDER BY sort_observed_quantity IS NULL,sort_observed_quantity DESC,"
+"sku_code ASC,customer_code ASC LIMIT #{p.size} OFFSET #{p.offset}),";
sql=sql.substring(0,rangeStart)+" ranged AS (SELECT d.customer_code,d.sku_code,SUM(d.outbound_quantity) observed_sum,"
+"COUNT(*) actual_days,SUM(d.outbound_order_count) order_sum,MIN(d.quantity_unit) min_unit,MAX(d.quantity_unit) max_unit,"
+"SUM(CASE WHEN d.business_date>=#{p.completeFrom} AND d.business_date<#{p.completeUntil} AND d.business_date<#{p.anchor} THEN 1 ELSE 0 END) actual_complete "
+"FROM ll_selfop_sku_daily_sales d WHERE d.sales_run_id=#{p.salesRun} AND"+KEY_FILTER
+"FROM paged_keys k JOIN ll_selfop_sku_daily_sales d ON d.customer_code=k.customer_code AND d.sku_code=k.sku_code "
+"WHERE d.sales_run_id=#{p.salesRun} AND d.source_account=#{p.account} AND d.warehouse_code=#{p.warehouse}"
+" AND d.business_date>=#{p.from} AND d.business_date<#{p.until} GROUP BY d.customer_code,d.sku_code),"+sql.substring(rangeEnd);
sql=sql.replace(" bounds AS (",sortPage+" bounds AS (").replace("FROM evidence),","FROM paged_keys evidence),");
String scopedCount="SELECT COUNT(*) FROM ll_selfop_sku_daily_sales d2 WHERE d2.sales_run_id=#{p.salesRun} "
+"AND d2.source_account=#{p.account} AND d2.warehouse_code=#{p.warehouse} "
+"AND d2.customer_code=b.customer_code AND d2.sku_code=b.sku_code ";
@@ -175,7 +196,8 @@ public final class AnalyticsReadSql {
return typed(cte(p)+"SELECT customer_code,sku_code,product_name,quantity_unit,evidence_at_utc,unit_valid,observed_quantity,sku_order_count,"
+"complete_day_count,partial_day_count,unknown_day_count,window_quantity,window_sku_order_count,sales30_quantity,today_quantity,"
+"physical_quantity,available_quantity,frozen_quantity,transit_quantity,inventory_observed_at_utc,inventory_quality,inventory_reason "
+"FROM result"+filter(p.kind()).stripTrailing()+" ORDER BY "+order(p.kind())+" LIMIT #{p.size} OFFSET #{p.offset}",p);
+"FROM result"+filter(p.kind()).stripTrailing()+" ORDER BY "+order(p.kind())
+("SUMMARY".equals(p.kind())?"":" LIMIT #{p.size} OFFSET #{p.offset}"),p);
}
/** Rank on sales alone. Inventory enrichment and its quality checks touch only the
* selected page; inventory missing/invalid units cannot erase a valid sales rank. */