QUERY PLAN
Aggregate (cost=74373.35..74373.36 rows=1 width=8)
-> Sort (cost=74312.39..74322.55 rows=4064 width=767)
Sort Key: sc.signed_at DESC, sc.created_at DESC
-> Group (cost=73936.70..74068.78 rows=4064 width=767)
Group Key: sc.id, scp.collected, (COALESCE(sum(t1.amount), '0'::numeric)), (max((SubPlan 1))), (max((CASE WHEN (scp_1.collected IS NOT NULL) THEN scp_1.collected ELSE '0'::numeric END))), (max(scpcrd.remind_date)), (CASE WHEN ((sc.amount - scp.collected) > '0'::numeric) THEN CASE WHEN (((max((SubPlan 1))) - (max((CASE WHEN (scp_1.collected IS NOT NULL) THEN scp_1.collected ELSE '0'::numeric END)))) > '0'::numeric) THEN (max(scpcrd_2.remind_date)) ELSE (min(scpcrd_1.remind_date)) END ELSE NULL::date END), (COALESCE(sum(r_4.amount), '0'::numeric))
-> Sort (cost=73936.70..73946.86 rows=4064 width=158)
Sort Key: sc.id, scp.collected, (COALESCE(sum(t1.amount), '0'::numeric)), (max((SubPlan 1))), (max((CASE WHEN (scp_1.collected IS NOT NULL) THEN scp_1.collected ELSE '0'::numeric END))), (max(scpcrd.remind_date)), (CASE WHEN ((sc.amount - scp.collected) > '0'::numeric) THEN CASE WHEN (((max((SubPlan 1))) - (max((CASE WHEN (scp_1.collected IS NOT NULL) THEN scp_1.collected ELSE '0'::numeric END)))) > '0'::numeric) THEN (max(scpcrd_2.remind_date)) ELSE (min(scpcrd_1.remind_date)) END ELSE NULL::date END), (COALESCE(sum(r_4.amount), '0'::numeric))
-> Merge Left Join (cost=5293.21..73693.09 rows=4064 width=158)
Merge Cond: (sc.id = sc_3.id)
-> Merge Left Join (cost=4713.80..43907.10 rows=4064 width=164)
Merge Cond: (sc.id = sc_2.id)
-> Merge Left Join (cost=3203.87..42385.80 rows=4064 width=160)
Merge Cond: (sc.id = r_4.purchase_contract_id)
-> Merge Left Join (cost=1879.59..41051.10 rows=4064 width=128)
Merge Cond: (sc.id = t0_1.id)
-> Merge Left Join (cost=1706.81..40579.71 rows=4064 width=96)
Merge Cond: (sc.id = sc_1.id)
-> Sort (cost=1127.40..1137.56 rows=4064 width=28)
Sort Key: sc.id
-> Hash Left Join (cost=594.39..883.79 rows=4064 width=28)
Hash Cond: (sc.id = scp.contract_id)
-> Hash Right Join (cost=318.07..596.79 rows=4064 width=22)
Hash Cond: ("*SELECT* 1".contract_id = sc.id)
-> Append (cost=86.92..354.94 rows=4064 width=4)
-> Subquery Scan on "*SELECT* 1" (cost=86.92..147.40 rows=2054 width=4)
-> Hash Join (cost=86.92..126.86 rows=2054 width=124)
Hash Cond: (r.erp_payment_ticket_id = t.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..34.54 rows=2054 width=8)
-> Hash (cost=61.24..61.24 rows=2054 width=4)
-> Seq Scan on tbl_erp_payment_tickets t (cost=0.00..61.24 rows=2054 width=4)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Subquery Scan on "*SELECT* 2" (cost=12.79..32.80 rows=12 width=4)
-> Hash Join (cost=12.79..32.68 rows=12 width=124)
Hash Cond: (r_1.erp_other_income_ticket_id = t_1.id)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..17.80 rows=780 width=8)
-> Hash (cost=12.75..12.75 rows=3 width=4)
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..12.75 rows=3 width=4)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Subquery Scan on "*SELECT* 3" (cost=12.65..32.66 rows=12 width=4)
-> Hash Join (cost=12.65..32.54 rows=12 width=124)
Hash Cond: (r_2.erp_cash_cost_ticket_id = t_2.id)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..17.80 rows=780 width=8)
-> Hash (cost=12.61..12.61 rows=3 width=4)
-> Seq Scan on tbl_erp_cash_cost_tickets t_2 (cost=0.00..12.61 rows=3 width=4)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Subquery Scan on "*SELECT* 4" (cost=84.13..142.08 rows=1986 width=4)
-> Hash Join (cost=84.13..122.22 rows=1986 width=124)
Hash Cond: (r_3.erp_collect_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..32.86 rows=1986 width=8)
-> Hash (cost=59.31..59.31 rows=1986 width=4)
-> Seq Scan on tbl_erp_collect_tickets t_3 (cost=0.00..59.31 rows=1986 width=4)
Filter: (status = ANY ('{2,5,1}'::integer[]))
-> Hash (cost=206.07..206.07 rows=2007 width=26)
-> Seq Scan on tbl_purchase_contracts sc (cost=0.00..206.07 rows=2007 width=26)
Filter: ((NOT is_draft) AND (NOT is_refuse))
-> Hash (cost=251.23..251.23 rows=2007 width=10)
-> Subquery Scan on scp (cost=211.09..251.23 rows=2007 width=10)
-> HashAggregate (cost=211.09..231.16 rows=2007 width=10)
Group Key: t0.id
-> Seq Scan on tbl_purchase_contracts t0 (cost=0.00..206.07 rows=2007 width=10)
-> GroupAggregate (cost=579.41..39425.44 rows=200 width=72)
Group Key: sc_1.id
-> Merge Join (cost=579.41..29723.08 rows=713 width=44)
Merge Cond: (sc_1.id = scpcrd.contract_id)
Join Filter: ((CASE WHEN (scp_1.collected IS NOT NULL) THEN scp_1.collected ELSE '0'::numeric END) < (SubPlan 2))
-> Group (cost=542.07..557.12 rows=2007 width=531)
Group Key: sc_1.id, scp_1.collected
-> Sort (cost=542.07..547.09 rows=2007 width=10)
Sort Key: sc_1.id, scp_1.collected
-> Hash Left Join (cost=276.59..431.98 rows=2007 width=10)
Hash Cond: (sc_1.id = scp_1.contract_id)
-> Index Only Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc_1 (cost=0.28..150.38 rows=2007 width=4)
-> Hash (cost=251.23..251.23 rows=2007 width=10)
-> Subquery Scan on scp_1 (cost=211.09..251.23 rows=2007 width=10)
-> HashAggregate (cost=211.09..231.16 rows=2007 width=10)
Group Key: tbl_purchase_contracts.id
-> Seq Scan on tbl_purchase_contracts (cost=0.00..206.07 rows=2007 width=10)
-> Sort (cost=37.34..38.72 rows=553 width=8)
Sort Key: scpcrd.contract_id
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd (cost=0.00..12.15 rows=553 width=8)
Filter: (remind_date <= '2025-08-09'::date)
SubPlan 2
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_1 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
SubPlan 1
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
-> GroupAggregate (cost=172.78..395.50 rows=2007 width=36)
Group Key: t0_1.id
-> Merge Left Join (cost=172.78..359.38 rows=2205 width=10)
Merge Cond: (t0_1.id = t1.contract_id)
-> Index Only Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts t0_1 (cost=0.28..150.38 rows=2007 width=4)
-> Sort (cost=172.50..178.01 rows=2205 width=10)
Sort Key: t1.contract_id
-> Seq Scan on tbl_purchase_contract_invoices t1 (cost=0.00..50.05 rows=2205 width=10)
-> GroupAggregate (cost=1324.28..1324.38 rows=5 width=36)
Group Key: r_4.purchase_contract_id
-> Sort (cost=1324.28..1324.29 rows=5 width=10)
Sort Key: r_4.purchase_contract_id
-> Nested Loop (cost=0.28..1324.22 rows=5 width=10)
-> Seq Scan on tbl_term_taxonomy_relationships dt1 (cost=0.00..1190.81 rows=18 width=4)
Filter: ((term_id = 17) AND (taxonomy_id = 2100000000))
-> Index Scan using tbl_approves_pkey on tbl_approves r_4 (cost=0.28..7.41 rows=1 width=14)
Index Cond: (id = dt1.object_id)
Filter: (status = 2)
-> GroupAggregate (cost=1509.94..1510.36 rows=24 width=8)
Group Key: sc_2.id
-> Sort (cost=1509.94..1510.00 rows=24 width=8)
Sort Key: sc_2.id
-> Hash Join (cost=455.83..1509.39 rows=24 width=8)
Hash Cond: (sc_2.id = scpcrd_1.contract_id)
Join Filter: ((CASE WHEN (scp_2.collected IS NOT NULL) THEN scp_2.collected ELSE '0'::numeric END) < (SubPlan 3))
-> HashAggregate (cost=442.01..462.08 rows=2007 width=531)
Group Key: sc_2.id, scp_2.collected
-> Hash Left Join (cost=276.59..431.98 rows=2007 width=10)
Hash Cond: (sc_2.id = scp_2.contract_id)
-> Index Only Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc_2 (cost=0.28..150.38 rows=2007 width=4)
-> Hash (cost=251.23..251.23 rows=2007 width=10)
-> Subquery Scan on scp_2 (cost=211.09..251.23 rows=2007 width=10)
-> HashAggregate (cost=211.09..231.16 rows=2007 width=10)
Group Key: tbl_purchase_contracts_1.id
-> Seq Scan on tbl_purchase_contracts tbl_purchase_contracts_1 (cost=0.00..206.07 rows=2007 width=10)
-> Hash (cost=13.58..13.58 rows=19 width=8)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd_1 (cost=0.00..13.58 rows=19 width=8)
Filter: (remind_date > now())
SubPlan 3
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_2 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd_1.remind_date) AND (contract_id = scpcrd_1.contract_id))
-> GroupAggregate (cost=579.41..29728.64 rows=200 width=8)
Group Key: sc_3.id
-> Merge Join (cost=579.41..29723.08 rows=713 width=8)
Merge Cond: (sc_3.id = scpcrd_2.contract_id)
Join Filter: ((CASE WHEN (scp_3.collected IS NOT NULL) THEN scp_3.collected ELSE '0'::numeric END) < (SubPlan 4))
-> Group (cost=542.07..557.12 rows=2007 width=531)
Group Key: sc_3.id, scp_3.collected
-> Sort (cost=542.07..547.09 rows=2007 width=10)
Sort Key: sc_3.id, scp_3.collected
-> Hash Left Join (cost=276.59..431.98 rows=2007 width=10)
Hash Cond: (sc_3.id = scp_3.contract_id)
-> Index Only Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc_3 (cost=0.28..150.38 rows=2007 width=4)
-> Hash (cost=251.23..251.23 rows=2007 width=10)
-> Subquery Scan on scp_3 (cost=211.09..251.23 rows=2007 width=10)
-> HashAggregate (cost=211.09..231.16 rows=2007 width=10)
Group Key: tbl_purchase_contracts_2.id
-> Seq Scan on tbl_purchase_contracts tbl_purchase_contracts_2 (cost=0.00..206.07 rows=2007 width=10)
-> Sort (cost=37.34..38.72 rows=553 width=8)
Sort Key: scpcrd_2.contract_id
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd_2 (cost=0.00..12.15 rows=553 width=8)
Filter: (remind_date <= '2025-08-09'::date)
SubPlan 4
-> Aggregate (cost=13.58..13.59 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_3 (cost=0.00..13.58 rows=1 width=6)
Filter: ((remind_date <= scpcrd_2.remind_date) AND (contract_id = scpcrd_2.contract_id))