QUERY PLAN
Limit (cost=54758.93..54758.94 rows=3 width=417)
-> Sort (cost=54758.93..54758.94 rows=3 width=417)
Sort Key: (COALESCE(sum(r.amount), '0'::numeric)) DESC NULLS LAST, sc.signed_at DESC, sc.created_at DESC
-> Group (cost=54758.76..54758.90 rows=3 width=417)
Group Key: sc.id, scp.collected, sci.invoiced, (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.amount), '0'::numeric))
-> Sort (cost=54758.76..54758.77 rows=3 width=285)
Sort Key: sc.id, scp.collected, sci.invoiced, (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.amount), '0'::numeric)) DESC NULLS LAST
-> Merge Left Join (cost=2588.81..54758.74 rows=3 width=285)
Merge Cond: (sc.id = sc_3.id)
-> Merge Left Join (cost=2262.48..32405.12 rows=3 width=285)
Merge Cond: (sc.id = sc_2.id)
-> Merge Left Join (cost=1894.50..32037.03 rows=3 width=281)
Merge Cond: (sc.id = sc_1.id)
-> Merge Left Join (cost=1568.16..2364.92 rows=3 width=213)
Merge Cond: (sc.id = r.purchase_contract_id)
-> Nested Loop Left Join (cost=344.97..1141.61 rows=3 width=181)
Join Filter: (sci.contract_id = sc.id)
-> Nested Loop Left Join (cost=156.47..821.93 rows=3 width=149)
Join Filter: (scp.contract_id = sc.id)
-> Nested Loop Left Join (cost=0.28..534.56 rows=3 width=117)
Join Filter: (scp2.contract_id = sc.id)
-> Nested Loop (cost=0.28..408.58 rows=3 width=117)
Join Filter: (sc.supplier_id = c.id)
-> Index Scan using tbl_purchase_contracts_pkey on tbl_purchase_contracts sc (cost=0.28..352.34 rows=1867 width=117)
Filter: (NOT is_chargeback)
-> Materialize (cost=0.00..28.24 rows=1 width=4)
-> Seq Scan on tbl_suppliers c (cost=0.00..28.24 rows=1 width=4)
Filter: ((name)::text ~~* '%bjhlxt%'::text)
-> Materialize (cost=0.00..43.95 rows=1930 width=4)
-> Seq Scan on tbl_purchase_contract_payments scp2 (cost=0.00..34.30 rows=1930 width=4)
-> Materialize (cost=156.19..207.73 rows=1874 width=36)
-> Subquery Scan on scp (cost=156.19..198.36 rows=1874 width=36)
-> HashAggregate (cost=156.19..179.62 rows=1874 width=36)
Group Key: t0.id
-> Hash Right Join (cost=107.17..146.54 rows=1930 width=10)
Hash Cond: (t1.contract_id = t0.id)
-> Seq Scan on tbl_purchase_contract_payments t1 (cost=0.00..34.30 rows=1930 width=10)
-> Hash (cost=83.74..83.74 rows=1874 width=4)
-> Seq Scan on tbl_purchase_contracts t0 (cost=0.00..83.74 rows=1874 width=4)
-> Materialize (cost=188.50..240.03 rows=1874 width=36)
-> Subquery Scan on sci (cost=188.50..230.66 rows=1874 width=36)
-> HashAggregate (cost=188.50..211.92 rows=1874 width=36)
Group Key: t0_1.id
-> Hash Right Join (cost=107.17..178.48 rows=2004 width=10)
Hash Cond: (t1_1.contract_id = t0_1.id)
-> Seq Scan on tbl_purchase_contract_invoices t1_1 (cost=0.00..66.04 rows=2004 width=10)
-> Hash (cost=83.74..83.74 rows=1874 width=4)
-> Seq Scan on tbl_purchase_contracts t0_1 (cost=0.00..83.74 rows=1874 width=4)
-> GroupAggregate (cost=1223.19..1223.25 rows=3 width=36)
Group Key: r.purchase_contract_id
-> Sort (cost=1223.19..1223.20 rows=3 width=10)
Sort Key: r.purchase_contract_id
-> Nested Loop (cost=0.28..1223.17 rows=3 width=10)
-> Seq Scan on tbl_term_taxonomy_relationships dt1 (cost=0.00..1147.89 rows=11 width=4)
Filter: ((term_id = 17) AND (taxonomy_id = 2100000000))
-> Index Scan using tbl_approves_pkey on tbl_approves r (cost=0.28..6.84 rows=1 width=14)
Index Cond: (id = dt1.object_id)
Filter: (status = 2)
-> GroupAggregate (cost=326.34..29669.60 rows=200 width=72)
Group Key: sc_1.id
-> Merge Join (cost=326.34..22345.58 rows=698 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=298.19..312.25 rows=1874 width=487)
Group Key: sc_1.id, scp_1.collected
-> Sort (cost=298.19..302.88 rows=1874 width=36)
Sort Key: sc_1.id, scp_1.collected
-> Hash Left Join (cost=107.65..196.32 rows=1874 width=36)
Hash Cond: (sc_1.id = scp_1.contract_id)
-> Seq Scan on tbl_purchase_contracts sc_1 (cost=0.00..83.74 rows=1874 width=4)
-> Hash (cost=84.90..84.90 rows=1820 width=36)
-> Subquery Scan on scp_1 (cost=43.95..84.90 rows=1820 width=36)
-> HashAggregate (cost=43.95..66.70 rows=1820 width=36)
Group Key: tbl_purchase_contract_payments.contract_id
-> Seq Scan on tbl_purchase_contract_payments (cost=0.00..34.30 rows=1930 width=10)
-> Sort (cost=28.15..29.22 rows=429 width=8)
Sort Key: scpcrd.contract_id
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd (cost=0.00..9.39 rows=429 width=8)
Filter: (remind_date <= '2025-07-03'::date)
SubPlan 2
-> Aggregate (cost=10.47..10.48 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_1 (cost=0.00..10.46 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
SubPlan 1
-> Aggregate (cost=10.47..10.48 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub (cost=0.00..10.46 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
-> GroupAggregate (cost=367.98..368.03 rows=3 width=8)
Group Key: sc_2.id
-> Sort (cost=367.98..367.99 rows=3 width=8)
Sort Key: sc_2.id
-> Hash Join (cost=216.18..367.96 rows=3 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=205.69..224.43 rows=1874 width=487)
Group Key: sc_2.id, scp_2.collected
-> Hash Left Join (cost=107.65..196.32 rows=1874 width=36)
Hash Cond: (sc_2.id = scp_2.contract_id)
-> Seq Scan on tbl_purchase_contracts sc_2 (cost=0.00..83.74 rows=1874 width=4)
-> Hash (cost=84.90..84.90 rows=1820 width=36)
-> Subquery Scan on scp_2 (cost=43.95..84.90 rows=1820 width=36)
-> HashAggregate (cost=43.95..66.70 rows=1820 width=36)
Group Key: tbl_purchase_contract_payments_1.contract_id
-> Seq Scan on tbl_purchase_contract_payments tbl_purchase_contract_payments_1 (cost=0.00..34.30 rows=1930 width=10)
-> Hash (cost=10.46..10.46 rows=2 width=8)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd_1 (cost=0.00..10.46 rows=2 width=8)
Filter: (remind_date > now())
SubPlan 3
-> Aggregate (cost=10.47..10.48 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_2 (cost=0.00..10.46 rows=1 width=6)
Filter: ((remind_date <= scpcrd_1.remind_date) AND (contract_id = scpcrd_1.contract_id))
-> GroupAggregate (cost=326.34..22351.07 rows=200 width=8)
Group Key: sc_3.id
-> Merge Join (cost=326.34..22345.58 rows=698 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=298.19..312.25 rows=1874 width=487)
Group Key: sc_3.id, scp_3.collected
-> Sort (cost=298.19..302.88 rows=1874 width=36)
Sort Key: sc_3.id, scp_3.collected
-> Hash Left Join (cost=107.65..196.32 rows=1874 width=36)
Hash Cond: (sc_3.id = scp_3.contract_id)
-> Seq Scan on tbl_purchase_contracts sc_3 (cost=0.00..83.74 rows=1874 width=4)
-> Hash (cost=84.90..84.90 rows=1820 width=36)
-> Subquery Scan on scp_3 (cost=43.95..84.90 rows=1820 width=36)
-> HashAggregate (cost=43.95..66.70 rows=1820 width=36)
Group Key: tbl_purchase_contract_payments_2.contract_id
-> Seq Scan on tbl_purchase_contract_payments tbl_purchase_contract_payments_2 (cost=0.00..34.30 rows=1930 width=10)
-> Sort (cost=28.15..29.22 rows=429 width=8)
Sort Key: scpcrd_2.contract_id
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrd_2 (cost=0.00..9.39 rows=429 width=8)
Filter: (remind_date <= '2025-07-03'::date)
SubPlan 4
-> Aggregate (cost=10.47..10.48 rows=1 width=32)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates scpcrdsub_3 (cost=0.00..10.46 rows=1 width=6)
Filter: ((remind_date <= scpcrd_2.remind_date) AND (contract_id = scpcrd_2.contract_id))