QUERY PLAN
Aggregate (cost=1199.98..1199.99 rows=1 width=32)
-> Append (cost=606.64..1199.96 rows=8 width=32)
-> Aggregate (cost=606.64..606.65 rows=1 width=32)
-> Hash Left Join (cost=446.57..604.94 rows=679 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=355.52..511.86 rows=774 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=802 width=8)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..345.50 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-07-11 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.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=424.82..424.83 rows=1 width=32)
-> Nested Loop (cost=74.02..424.81 rows=1 width=10)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=74.02..423.63 rows=1 width=9)
Join Filter: (sc_1.from_project = pd.project_id)
-> Hash Join (cost=74.02..422.54 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..345.50 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-07-11 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=73.99..73.99 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_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.40..6.41 rows=1 width=32)
-> Hash Join (cost=4.58..6.34 rows=26 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=26 width=4)
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=1.07..4.25 rows=26 width=4)
Filter: (((NOT (hashed SubPlan 2)) OR (project_id IS NULL)) AND (created_at >= '2025-07-11 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=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.60..6.61 rows=1 width=32)
-> Nested Loop (cost=2.21..6.57 rows=3 width=10)
Join Filter: (pd_1.project_id = t_4.project_id)
-> Hash Join (cost=1.11..4.26 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=39 width=9)
Filter: ((created_at >= '2025-07-11 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=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))
-> Hash Join (cost=1.09..2.21 rows=8 width=9)
Hash Cond: (am_3.erp_profit_distribute_id = pd_1.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_3 (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_1 (cost=0.00..1.04 rows=4 width=8)
-> Aggregate (cost=50.79..50.80 rows=1 width=32)
-> Hash Join (cost=3.59..50.71 rows=30 width=5)
Hash Cond: (t_6.id = am_4.erp_cost_convert_ticket_id)
-> Nested Loop Left Join (cost=1.35..48.11 rows=6 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=6 width=8)
Filter: ((created_at >= '2025-07-11 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..7.63 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))
-> Hash (cost=1.55..1.55 rows=55 width=9)
-> Seq Scan on tbl_erp_cost_convert_ticket_handlers am_4 (cost=0.00..1.55 rows=55 width=9)
-> Aggregate (cost=57.88..57.89 rows=1 width=32)
-> Nested Loop (cost=1.34..57.87 rows=1 width=11)
Join Filter: (pd_2.id = am_5.erp_profit_distribute_id)
-> Nested Loop (cost=1.34..56.69 rows=1 width=10)
Join Filter: (sc_3.from_project = pd_2.project_id)
-> Nested Loop (cost=1.34..55.60 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..54.34 rows=6 width=10)
-> Nested Loop (cost=0.28..54.31 rows=6 width=10)
-> Seq Scan on tbl_erp_cost_convert_tickets t_7 (cost=0.00..1.19 rows=6 width=10)
Filter: ((created_at >= '2025-07-11 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..7.63 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=22.80..22.81 rows=1 width=32)
-> Hash Right Join (cost=7.38..21.52 rows=254 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=58 width=4)
-> Seq Scan on tbl_erp_sales_gifts t_9 (cost=1.07..6.65 rows=58 width=4)
Filter: (((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (created_at >= '2025-07-11 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=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.88..23.89 rows=1 width=32)
-> Hash Right Join (cost=8.94..23.74 rows=14 width=15)
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=8.90..8.90 rows=3 width=9)
-> Hash Join (cost=3.43..8.90 rows=3 width=9)
Hash Cond: (t_10.project_id = pd_3.project_id)
-> Hash Join (cost=1.11..6.56 rows=2 width=12)
Hash Cond: (t_10.project_id = t_11.project_id)
-> Seq Scan on tbl_erp_sales_gifts t_10 (cost=0.00..5.26 rows=68 width=8)
Filter: ((created_at >= '2025-07-11 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=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 (cost=2.21..2.21 rows=8 width=9)
-> 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)