QUERY PLAN
Aggregate (cost=508.22..508.23 rows=1 width=32)
-> Nested Loop Semi Join (cost=153.47..507.96 rows=104 width=17)
Join Filter: ("*SELECT* 1".contract_id = t.id)
-> Append (cost=74.57..417.76 rows=207 width=21)
-> Subquery Scan on "*SELECT* 1" (cost=74.57..120.48 rows=123 width=10)
-> Hash Join (cost=74.57..119.25 rows=123 width=106)
Hash Cond: (r.erp_payment_ticket_id = t_1.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..38.50 rows=2350 width=14)
-> Hash (cost=73.12..73.12 rows=116 width=4)
-> Seq Scan on tbl_erp_payment_tickets t_1 (cost=0.00..73.12 rows=116 width=4)
Filter: (is_payment_settle AND (bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=0.00..2.36 rows=1 width=36)
-> Nested Loop (cost=0.00..2.35 rows=1 width=132)
Join Filter: (r_1.erp_other_income_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_other_income_tickets t_2 (cost=0.00..1.28 rows=1 width=4)
Filter: ((bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.03 rows=3 width=40)
-> Subquery Scan on "*SELECT* 3" (cost=0.28..185.55 rows=2 width=36)
-> Nested Loop (cost=0.28..185.53 rows=2 width=132)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.27 rows=27 width=40)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_3 (cost=0.28..6.82 rows=1 width=4)
Index Cond: (id = r_2.erp_cash_cost_ticket_id)
Filter: (is_payment_settle AND (bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=68.12..109.37 rows=81 width=36)
-> Hash Join (cost=68.12..108.56 rows=81 width=132)
Hash Cond: (r_3.erp_collect_ticket_id = t_4.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..34.77 rows=2077 width=14)
-> Hash (cost=67.09..67.09 rows=83 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_4 (cost=0.00..67.09 rows=83 width=4)
Filter: ((bank_settle_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Materialize (cost=78.90..87.09 rows=1 width=8)
-> Nested Loop (cost=78.90..87.09 rows=1 width=8)
-> GroupAggregate (cost=78.62..78.63 rows=1 width=12)
Group Key: t_5.purchase_contract_id
-> Sort (cost=78.62..78.62 rows=1 width=4)
Sort Key: t_5.purchase_contract_id
-> Seq Scan on tbl_erp_purchase_orders t_5 (cost=0.00..78.61 rows=1 width=4)
Filter: ((purchase_contract_id IS NOT NULL) AND (cancel_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (cancel_at <= '2025-12-31 23:59:59+08'::timestamp with time zone) AND (status = 8))
-> Index Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts t (cost=0.28..8.30 rows=1 width=4)
Index Cond: (id = t_5.purchase_contract_id)
Filter: (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone)