QUERY PLAN
Aggregate (cost=991.50..991.51 rows=1 width=32)
-> Append (cost=527.88..991.48 rows=8 width=32)
-> Aggregate (cost=527.88..527.89 rows=1 width=32)
-> Nested Loop Left Join (cost=346.89..527.87 rows=3 width=5)
Filter: ((NOT (hashed SubPlan 1)) OR (t.sales_contract_id IS NULL))
-> Hash Join (cost=345.54..501.87 rows=3 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=3 width=8)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..345.50 rows=3 width=8)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = t.sales_contract_id)
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=373.86..373.87 rows=1 width=32)
-> Nested Loop (cost=1.34..373.85 rows=1 width=10)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=1.34..372.67 rows=1 width=9)
Join Filter: (sc_1.from_project = pd.project_id)
-> Nested Loop (cost=1.34..371.58 rows=1 width=13)
Join Filter: (sc_1.from_project = t_2.project_id)
-> 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))
-> Materialize (cost=0.28..370.41 rows=3 width=9)
-> Nested Loop (cost=0.28..370.39 rows=3 width=9)
-> Seq Scan on tbl_erp_cash_cost_tickets t_1 (cost=0.00..345.50 rows=3 width=9)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_1 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = t_1.sales_contract_id)
-> Seq Scan on tbl_erp_profit_distributes pd (cost=0.00..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_1 (cost=0.00..1.08 rows=8 width=9)
-> Aggregate (cost=6.03..6.04 rows=1 width=32)
-> Hash Join (cost=4.27..6.02 rows=1 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=1 width=4)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=1.07..4.25 rows=1 width=4)
Filter: (((NOT (hashed SubPlan 2)) OR (project_id IS NULL)) AND (created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 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.43..6.44 rows=1 width=32)
-> Nested Loop (cost=1.06..6.42 rows=1 width=10)
Join Filter: (pd_1.project_id = t_5.project_id)
-> Nested Loop (cost=0.00..5.30 rows=1 width=18)
Join Filter: (pd_1.id = am_3.erp_profit_distribute_id)
-> Nested Loop (cost=0.00..4.12 rows=1 width=17)
Join Filter: (pd_1.project_id = t_4.project_id)
-> Seq Scan on tbl_erp_cost_tickets t_4 (cost=0.00..3.03 rows=1 width=9)
Filter: ((created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) 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)
-> 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))
-> Aggregate (cost=12.41..12.42 rows=1 width=32)
-> Hash Join (cost=10.59..12.39 rows=5 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=10.57..10.57 rows=1 width=4)
-> Nested Loop Left Join (cost=1.35..10.57 rows=1 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=1 width=8)
Filter: ((created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 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=20.90..20.91 rows=1 width=32)
-> Nested Loop (cost=1.34..20.89 rows=1 width=11)
Join Filter: (sc_3.from_project = t_8.project_id)
-> Nested Loop (cost=0.28..19.78 rows=1 width=19)
Join Filter: (pd_2.id = am_5.erp_profit_distribute_id)
-> Nested Loop (cost=0.28..18.59 rows=1 width=18)
Join Filter: (sc_3.from_project = pd_2.project_id)
-> Nested Loop (cost=0.28..17.50 rows=1 width=10)
-> Seq Scan on tbl_erp_cost_convert_tickets t_7 (cost=0.00..1.19 rows=1 width=10)
Filter: ((created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 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)
-> 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))
-> Aggregate (cost=20.83..20.84 rows=1 width=32)
-> Hash Right Join (cost=6.67..20.81 rows=4 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=1 width=4)
-> Seq Scan on tbl_erp_sales_gifts t_9 (cost=1.07..6.65 rows=1 width=4)
Filter: (((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 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=22.98..22.99 rows=1 width=32)
-> Nested Loop (cost=7.42..22.95 rows=3 width=15)
Join Filter: (pd_3.project_id = t_10.project_id)
-> Nested Loop (cost=6.33..20.63 rows=1 width=18)
Join Filter: (t_10.project_id = t_11.project_id)
-> 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))
-> Materialize (cost=5.27..19.43 rows=4 width=14)
-> Hash Right Join (cost=5.27..19.41 rows=4 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=1 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_10 (cost=0.00..5.26 rows=1 width=8)
Filter: ((created_at >= '2026-06-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-06-30 23:59:59+08'::timestamp with time zone) 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)