QUERY PLAN
Aggregate (cost=1597.08..1597.09 rows=1 width=32)
-> Append (cost=783.29..1597.06 rows=8 width=32)
-> Aggregate (cost=783.29..783.30 rows=1 width=32)
-> Hash Left Join (cost=621.59..781.56 rows=691 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=518.40..676.32 rows=787 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=802 width=8)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..508.38 rows=802 width=8)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2025-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-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=613.74..613.75 rows=1 width=32)
-> Nested Loop (cost=85.98..613.73 rows=1 width=10)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=85.98..612.55 rows=1 width=9)
Join Filter: (sc_1.from_project = pd.project_id)
-> Hash Join (cost=85.98..597.38 rows=1 width=13)
Hash Cond: (t_1.sales_contract_id = sc_1.id)
-> Seq Scan on tbl_erp_cash_cost_tickets t_1 (cost=0.00..508.38 rows=802 width=9)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2025-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Hash (cost=85.97..85.97 rows=1 width=12)
-> 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_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.71..19.72 rows=1 width=32)
-> Hash Join (cost=18.02..19.65 rows=23 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=39 width=4)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=12.89..17.54 rows=39 width=4)
Filter: (((NOT (hashed SubPlan 2)) OR (project_id IS NULL)) AND (created_at >= '2025-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-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.89..31.90 rows=1 width=32)
-> Nested Loop (cost=17.53..31.88 rows=1 width=10)
Join Filter: (pd_1.id = am_3.erp_profit_distribute_id)
-> Hash Join (cost=17.53..30.70 rows=1 width=9)
Hash Cond: (pd_1.project_id = t_4.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_1 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=17.52..17.52 rows=1 width=13)
-> Hash Join (cost=12.91..17.52 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..4.44 rows=60 width=9)
Filter: ((created_at >= '2025-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> 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)
-> 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-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-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-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-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=37.42..37.43 rows=1 width=32)
-> Hash Right Join (cost=22.11..36.07 rows=269 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=60 width=4)
-> Seq Scan on tbl_erp_sales_gifts t_9 (cost=12.89..21.36 rows=60 width=4)
Filter: (((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (created_at >= '2025-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-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=50.24..50.25 rows=1 width=32)
-> Nested Loop (cost=34.46..50.23 rows=1 width=14)
Join Filter: (pd_3.id = am_6.erp_profit_distribute_id)
-> Hash Right Join (cost=34.46..49.05 rows=1 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=34.45..34.45 rows=1 width=8)
-> Hash Join (cost=21.28..34.45 rows=1 width=8)
Hash Cond: (pd_3.project_id = t_10.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_3 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=21.27..21.27 rows=1 width=12)
-> Hash Join (cost=12.91..21.27 rows=1 width=12)
Hash Cond: (t_10.project_id = t_11.project_id)
-> Seq Scan on tbl_erp_sales_gifts t_10 (cost=0.00..8.15 rows=71 width=8)
Filter: ((created_at >= '2025-08-05 00:00:00+08'::timestamp with time zone) AND (created_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> 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))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_6 (cost=0.00..1.08 rows=8 width=9)