QUERY PLAN
Aggregate (cost=1107.94..1107.95 rows=1 width=32)
-> Append (cost=593.91..1107.92 rows=8 width=32)
-> Aggregate (cost=593.91..593.92 rows=1 width=32)
-> Hash Left Join (cost=437.29..593.78 rows=51 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.25..502.58 rows=58 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=60 width=8)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..345.50 rows=60 width=8)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2026-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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=422.60..422.61 rows=1 width=32)
-> Nested Loop (cost=1.11..422.59 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=60 width=9)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (created_at >= '2026-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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=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-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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=21.83..21.84 rows=1 width=32)
-> Hash Right Join (cost=7.03..21.17 rows=131 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=30 width=4)
-> Seq Scan on tbl_erp_sales_gifts t_9 (cost=1.07..6.65 rows=30 width=4)
Filter: (((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (created_at >= '2026-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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.63..23.64 rows=1 width=32)
-> Hash Right Join (cost=8.77..23.56 rows=7 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.75..8.75 rows=2 width=9)
-> Nested Loop (cost=1.11..8.75 rows=2 width=9)
Join Filter: (pd_3.id = am_6.erp_profit_distribute_id)
-> Nested Loop (cost=1.11..7.57 rows=1 width=8)
Join Filter: (t_10.project_id = pd_3.project_id)
-> Hash Join (cost=1.11..6.48 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..5.26 rows=35 width=8)
Filter: ((created_at >= '2026-02-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-02-28 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))
-> Seq Scan on tbl_erp_profit_distributes pd_3 (cost=0.00..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_6 (cost=0.00..1.08 rows=8 width=9)