perf(selfop): materialize sales bounds without redundant deduplication

This commit is contained in:
2026-10-13 02:31:46 +08:00
parent afd3bbaa3d
commit 341b7635c7
2 changed files with 6 additions and 6 deletions
@@ -35,10 +35,9 @@ public final class AnalyticsReadSql {
String localEvidence="CONVERT_TZ(evidence_at_utc,'+00:00','+08:00')";
String observedStart="CONVERT_TZ(GREATEST(evidence_at_utc,#{p.coveredFrom}),'+00:00','+08:00')";
String observedEnd="CONVERT_TZ(#{p.coveredUntil},'+00:00','+08:00')";
// Candidate identity is already unique. DISTINCT preserves that identity while
// preventing MySQL from merging these per-SKU date calculations into every fact row.
String boundaryProjection="SUMMARY".equals(kind)?"SELECT DISTINCT evidence.*,":"SELECT evidence.*,";
String sql=candidates("SUMMARY".equals(kind))+" bounds AS ("+boundaryProjection
// Candidate identity is already unique; SUMMARY materializes bounds with NO_MERGE
// at its consumer, avoiding a redundant DISTINCT temporary table.
String sql=candidates("SUMMARY".equals(kind))+" bounds AS (SELECT evidence.*,"
+"CASE WHEN evidence_at_utc IS NOT NULL AND #{p.completeFrom} IS NOT NULL AND #{p.completeUntil} IS NOT NULL THEN "
+"GREATEST(#{p.from},#{p.completeFrom},TIMESTAMPADD(DAY,CASE WHEN CAST("+localEvidence+" AS TIME)='00:00:00' THEN 0 ELSE 1 END,DATE("+localEvidence+"))) END full_start,"
+"LEAST(#{p.until},#{p.completeUntil},#{p.anchor}) full_end,"
@@ -62,7 +61,7 @@ public final class AnalyticsReadSql {
+"FROM ll_selfop_sku_daily_sales WHERE sales_run_id=#{p.salesRun} AND"+KEY_FILTER
+" AND business_date>=TIMESTAMPADD(DAY,-30,#{p.anchor}) AND business_date<TIMESTAMPADD(DAY,1,#{p.anchor}) "
+"GROUP BY customer_code,sku_code),"
+" counted AS (SELECT b.*,r.observed_sum,r.order_sum,"
+" counted AS (SELECT "+("SUMMARY".equals(kind)?"/*+ NO_MERGE(b) */ ":"")+"b.*,r.observed_sum,r.order_sum,"
+"CASE WHEN b.profile_unit_valid=1 AND (r.min_unit IS NULL OR (r.min_unit=b.quantity_unit AND r.max_unit=b.quantity_unit)) "
+"AND (m.min_unit IS NULL OR (m.min_unit=b.quantity_unit AND m.max_unit=b.quantity_unit)) THEN 1 ELSE 0 END unit_valid,"
+"CASE WHEN #{p.formalSales}=1 THEN GREATEST(0,COALESCE(TIMESTAMPDIFF(DAY,b.full_start,b.full_end),0)) "
@@ -60,7 +60,8 @@ class AnalyticsReadMapperTest {
assertTrue(page.contains("GROUP BY d.customer_code,d.sku_code")); assertFalse(page.contains("business_date>=DATE_SUB"));
assertFalse(page.contains("ll_selfop_inventory_snapshot"));
assertFalse(page.contains("metric_sales"));assertFalse(page.contains("FROM bounds WHERE 1=0"));
assertTrue(page.contains("bounds AS (SELECT DISTINCT evidence.*,"));
assertTrue(page.contains("bounds AS (SELECT evidence.*,"));
assertTrue(page.contains("counted AS (SELECT /*+ NO_MERGE(b) */ b.*,"));
assertTrue(page.endsWith("ORDER BY observed_quantity IS NULL,observed_quantity DESC,sku_code ASC,customer_code ASC LIMIT ? OFFSET ?"));
assertTrue(page.indexOf("GROUP BY d.customer_code,d.sku_code")<page.lastIndexOf("LIMIT"));
assertFalse(page.contains("FROM bounds b JOIN ll_selfop_sku_daily_sales"));