QUERY PLAN
Aggregate (cost=1581.37..1581.38 rows=1 width=32)
-> Append (cost=772.30..1581.35 rows=8 width=32)
-> Aggregate (cost=772.30..772.31 rows=1 width=32)
-> Hash Left Join (cost=613.61..771.94 rows=141 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=510.43..668.34 rows=161 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..136.09 rows=8309 width=9)
-> Hash (cost=508.38..508.38 rows=164 width=8)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..508.38 rows=164 width=8)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=68.46..68.46 rows=1746 width=8)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..68.46 rows=1746 width=8)
SubPlan 1
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_12.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_12.project_id
-> Seq Scan on tbl_erp_profit_distributes t_12 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=612.76..612.77 rows=1 width=32)
-> Nested Loop (cost=12.91..612.75 rows=1 width=10)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=12.91..611.57 rows=1 width=9)
Join Filter: (sc_1.from_project = pd.project_id)
-> Nested Loop (cost=12.91..596.40 rows=1 width=13)
Join Filter: (sc_1.id = t_1.sales_contract_id)
-> Hash Join (cost=12.91..85.97 rows=1 width=12)
Hash Cond: (sc_1.from_project = t_2.project_id)
-> Seq Scan on tbl_sales_contracts sc_1 (cost=0.00..68.46 rows=1746 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_2.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_2.project_id
-> Seq Scan on tbl_erp_profit_distributes t_2 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_cash_cost_tickets t_1 (cost=0.00..508.38 rows=164 width=9)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distributes pd (cost=0.00..12.30 rows=230 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_1 (cost=0.00..1.08 rows=8 width=9)
-> Aggregate (cost=19.19..19.20 rows=1 width=32)
-> Hash Join (cost=17.56..19.19 rows=1 width=32)
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.49 rows=49 width=36)
-> Hash (cost=17.54..17.54 rows=2 width=4)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=12.89..17.54 rows=2 width=4)
Filter: (((NOT (hashed SubPlan 2)) OR (project_id IS NULL)) AND (created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
SubPlan 2
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_13.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_13.project_id
-> Seq Scan on tbl_erp_profit_distributes t_13 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=31.49..31.50 rows=1 width=32)
-> Nested Loop (cost=12.91..31.48 rows=1 width=10)
Join Filter: (pd_1.project_id = t_4.project_id)
-> Nested Loop (cost=12.91..27.01 rows=1 width=13)
Join Filter: (pd_1.id = am_3.erp_profit_distribute_id)
-> Hash Join (cost=12.91..25.83 rows=1 width=12)
Hash Cond: (pd_1.project_id = t_5.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_1 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_5.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_5.project_id
-> Seq Scan on tbl_erp_profit_distributes t_5 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_3 (cost=0.00..1.08 rows=8 width=9)
-> Seq Scan on tbl_erp_cost_tickets t_4 (cost=0.00..4.44 rows=3 width=9)
Filter: ((created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Aggregate (cost=24.08..24.09 rows=1 width=32)
-> Hash Join (cost=22.39..24.07 rows=3 width=32)
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.49 rows=49 width=36)
-> Hash (cost=22.38..22.38 rows=1 width=4)
-> Nested Loop Left Join (cost=13.17..22.38 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.18 rows=1 width=8)
Filter: ((created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-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=12.88..12.89 rows=1 width=4)
Group Key: t_14.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_14.project_id
-> Seq Scan on tbl_erp_profit_distributes t_14 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=36.51..36.52 rows=1 width=32)
-> Nested Loop (cost=13.19..36.50 rows=1 width=37)
Join Filter: (pd_2.id = am_5.erp_profit_distribute_id)
-> Nested Loop (cost=13.19..35.32 rows=1 width=36)
Join Filter: (pd_2.project_id = sc_3.from_project)
-> Hash Join (cost=12.91..25.83 rows=1 width=12)
Hash Cond: (pd_2.project_id = t_8.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_2 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_8.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_8.project_id
-> Seq Scan on tbl_erp_profit_distributes t_8 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Nested Loop (cost=0.28..9.48 rows=1 width=36)
-> Seq Scan on tbl_erp_cost_convert_tickets t_7 (cost=0.00..1.18 rows=1 width=36)
Filter: ((created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-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_distribute_pcts am_5 (cost=0.00..1.08 rows=8 width=9)
-> Aggregate (cost=35.43..35.44 rows=1 width=32)
-> Hash Right Join (cost=21.39..35.36 rows=13 width=9)
Hash Cond: (eipss.erp_sales_gift_id = t_9.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.51 rows=551 width=13)
-> Hash (cost=21.36..21.36 rows=3 width=4)
-> Seq Scan on tbl_erp_sales_gifts t_9 (cost=12.89..21.36 rows=3 width=4)
Filter: (((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
SubPlan 4
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_15.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_15.project_id
-> Seq Scan on tbl_erp_profit_distributes t_15 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Aggregate (cost=49.42..49.43 rows=1 width=32)
-> Nested Loop (cost=21.12..49.40 rows=1 width=14)
Join Filter: (pd_3.id = am_6.erp_profit_distribute_id)
-> Nested Loop (cost=21.12..48.22 rows=1 width=13)
Join Filter: (pd_3.project_id = t_10.project_id)
-> Hash Join (cost=12.91..25.83 rows=1 width=12)
Hash Cond: (pd_3.project_id = t_11.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_3 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_11.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_11.project_id
-> Seq Scan on tbl_erp_profit_distributes t_11 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash Right Join (cost=8.20..22.17 rows=18 width=13)
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.51 rows=551 width=13)
-> Hash (cost=8.15..8.15 rows=4 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_10 (cost=0.00..8.15 rows=4 width=8)
Filter: ((created_at >= '2025-10-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-10-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_6 (cost=0.00..1.08 rows=8 width=9)