QUERY PLAN
Aggregate (cost=1124.52..1124.53 rows=1 width=32)
-> Append (cost=594.62..1124.50 rows=8 width=32)
-> Aggregate (cost=594.62..594.63 rows=1 width=32)
-> Hash Left Join (cost=437.81..594.40 rows=86 width=5)
Hash Cond: (t.sales_contract_id = sc.id)
Filter: ((NOT (hashed SubPlan 1)) OR (t.sales_contract_id IS NULL))
-> Hash Join (cost=346.76..503.10 rows=98 width=9)
Hash Cond: (am.erp_cash_cost_ticket_id = t.id)
-> Seq Scan on tbl_erp_cash_cost_ticket_handlers am (cost=0.00..134.84 rows=8184 width=9)
-> Hash (cost=345.50..345.50 rows=101 width=8)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..345.50 rows=101 width=8)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=68.32..68.32 rows=1732 width=8)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..68.32 rows=1732 width=8)
SubPlan 1
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_12.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_12.project_id
-> Seq Scan on tbl_erp_profit_distributes t_12 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=423.11..423.12 rows=1 width=32)
-> Nested Loop (cost=1.11..423.10 rows=1 width=10)
Join Filter: (sc_1.id = t_1.sales_contract_id)
-> Nested Loop (cost=1.11..76.34 rows=1 width=9)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=1.11..75.16 rows=1 width=8)
Join Filter: (sc_1.from_project = pd.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd (cost=0.00..1.04 rows=4 width=8)
-> Materialize (cost=1.11..74.00 rows=2 width=12)
-> Hash Join (cost=1.11..73.99 rows=2 width=12)
Hash Cond: (sc_1.from_project = t_2.project_id)
-> Seq Scan on tbl_sales_contracts sc_1 (cost=0.00..68.32 rows=1732 width=8)
-> Hash (cost=1.09..1.09 rows=2 width=4)
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_2.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_2.project_id
-> Seq Scan on tbl_erp_profit_distributes t_2 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_1 (cost=0.00..1.08 rows=8 width=9)
-> Seq Scan on tbl_erp_cash_cost_tickets t_1 (cost=0.00..345.50 rows=101 width=9)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Aggregate (cost=6.04..6.05 rows=1 width=32)
-> Hash Join (cost=4.28..6.04 rows=2 width=5)
Hash Cond: (am_2.erp_cost_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_cost_ticket_handlers am_2 (cost=0.00..1.59 rows=59 width=9)
-> Hash (cost=4.25..4.25 rows=2 width=4)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=1.07..4.25 rows=2 width=4)
Filter: (((NOT (hashed SubPlan 2)) OR (project_id IS NULL)) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
SubPlan 2
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_13.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_13.project_id
-> Seq Scan on tbl_erp_profit_distributes t_13 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=6.44..6.45 rows=1 width=32)
-> Nested Loop (cost=1.11..6.43 rows=1 width=10)
Join Filter: (pd_1.id = am_3.erp_profit_distribute_id)
-> Nested Loop (cost=1.11..5.25 rows=1 width=9)
Join Filter: (t_4.project_id = pd_1.project_id)
-> Hash Join (cost=1.11..4.16 rows=1 width=13)
Hash Cond: (t_4.project_id = t_5.project_id)
-> Seq Scan on tbl_erp_cost_tickets t_4 (cost=0.00..3.03 rows=3 width=9)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.09..1.09 rows=2 width=4)
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_5.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_5.project_id
-> Seq Scan on tbl_erp_profit_distributes t_5 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distributes pd_1 (cost=0.00..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_3 (cost=0.00..1.08 rows=8 width=9)
-> Aggregate (cost=20.79..20.80 rows=1 width=32)
-> Hash Join (cost=18.91..20.76 rows=10 width=5)
Hash Cond: (am_4.erp_cost_convert_ticket_id = t_6.id)
-> Seq Scan on tbl_erp_cost_convert_ticket_handlers am_4 (cost=0.00..1.55 rows=55 width=9)
-> Hash (cost=18.88..18.88 rows=2 width=4)
-> Nested Loop Left Join (cost=1.35..18.88 rows=2 width=4)
Filter: ((NOT (hashed SubPlan 3)) OR (t_6.sales_contract_id IS NULL))
-> Seq Scan on tbl_erp_cost_convert_tickets t_6 (cost=0.00..1.19 rows=2 width=8)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_2 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = t_6.sales_contract_id)
SubPlan 3
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_14.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_14.project_id
-> Seq Scan on tbl_erp_profit_distributes t_14 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=29.24..29.25 rows=1 width=32)
-> Nested Loop (cost=1.34..29.23 rows=1 width=11)
Join Filter: (pd_2.id = am_5.erp_profit_distribute_id)
-> Nested Loop (cost=1.34..28.05 rows=1 width=10)
Join Filter: (sc_3.from_project = pd_2.project_id)
-> Nested Loop (cost=1.34..26.96 rows=1 width=14)
Join Filter: (sc_3.from_project = t_8.project_id)
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_8.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_8.project_id
-> Seq Scan on tbl_erp_profit_distributes t_8 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Materialize (cost=0.28..25.81 rows=2 width=10)
-> Nested Loop (cost=0.28..25.80 rows=2 width=10)
-> Seq Scan on tbl_erp_cost_convert_tickets t_7 (cost=0.00..1.19 rows=2 width=10)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_3 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = t_7.sales_contract_id)
-> Seq Scan on tbl_erp_profit_distributes pd_2 (cost=0.00..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_5 (cost=0.00..1.08 rows=8 width=9)
-> Aggregate (cost=21.04..21.05 rows=1 width=32)
-> Hash Right Join (cost=6.74..20.89 rows=31 width=10)
Hash Cond: (eipss.erp_sales_gift_id = t_9.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=6.65..6.65 rows=7 width=4)
-> Seq Scan on tbl_erp_sales_gifts t_9 (cost=1.07..6.65 rows=7 width=4)
Filter: (((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
SubPlan 4
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_15.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_15.project_id
-> Seq Scan on tbl_erp_profit_distributes t_15 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=23.07..23.08 rows=1 width=32)
-> Nested Loop (cost=7.56..23.03 rows=3 width=15)
Join Filter: (pd_3.project_id = t_10.project_id)
-> Hash Join (cost=6.47..20.72 rows=1 width=18)
Hash Cond: (t_10.project_id = t_11.project_id)
-> Hash Right Join (cost=5.36..19.50 rows=35 width=14)
Hash Cond: (eipss_1.erp_sales_gift_id = t_10.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_1 (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=5.26..5.26 rows=8 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_10 (cost=0.00..5.26 rows=8 width=8)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-01-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=1.09..1.09 rows=2 width=4)
-> Group (cost=1.06..1.07 rows=2 width=4)
Group Key: t_11.project_id
-> Sort (cost=1.06..1.06 rows=2 width=4)
Sort Key: t_11.project_id
-> Seq Scan on tbl_erp_profit_distributes t_11 (cost=0.00..1.05 rows=2 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash Join (cost=1.09..2.21 rows=8 width=9)
Hash Cond: (am_6.erp_profit_distribute_id = pd_3.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_6 (cost=0.00..1.08 rows=8 width=9)
-> Hash (cost=1.04..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_profit_distributes pd_3 (cost=0.00..1.04 rows=4 width=8)