QUERY PLAN
Limit (cost=41511.95..41511.96 rows=1 width=408)
-> Sort (cost=41511.95..41511.96 rows=1 width=408)
Sort Key: sc.signed_at DESC, sc.created_at DESC
-> GroupAggregate (cost=1175.27..41511.94 rows=1 width=408)
Group Key: sc.id
-> Merge Join (cost=1175.27..41511.87 rows=1 width=348)
Merge Cond: (sc.id = tbl_sales_contract_payments_1.contract_id)
-> Merge Left Join (cost=1131.11..41464.84 rows=1133 width=348)
Merge Cond: (sc.id = sc_1.id)
-> Merge Left Join (cost=683.88..689.59 rows=1133 width=284)
Merge Cond: (sc.id = t0_2.id)
-> Sort (cost=631.41..634.24 rows=1133 width=244)
Sort Key: sc.id
-> Hash Left Join (cost=498.57..573.93 rows=1133 width=244)
Hash Cond: (sc.id = sci.contract_id)
-> Hash Left Join (cost=306.40..378.77 rows=1133 width=212)
Hash Cond: (sc.id = scp.contract_id)
-> Hash Left Join (cost=130.43..199.81 rows=1133 width=180)
Hash Cond: (c.sales_rep = sgr.sales_id)
-> Hash Left Join (cost=128.37..187.66 rows=1133 width=184)
Hash Cond: (sc.client_id = c.id)
Filter: ((c.sales_rep = ANY ('{5049,5301,5022,5209,5026,5293,5309,5356,5329,5299,5303,5302,5332,5047,1026,5096,5070,5448,5420,1029,1001,5037,5087,1008,5048,5043,5045,5230,5031,5088,5208,1003,5294,5357,5044,1030,5076,5300,5027,5089,5311,5066,5069,5364,1009,5295,1013,5128,5203,1025,5032,5075,1004,5072,5033,5359,5412,5409,5229,1005,5304,5449,5358,1006,5093}'::integer[])) OR (c.sales_rep IS NULL))
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..55.24 rows=1510 width=180)
Filter: need_analysis
-> Hash (cost=104.83..104.83 rows=1883 width=8)
-> Seq Scan on tbl_clients c (cost=0.00..104.83 rows=1883 width=8)
-> Hash (cost=1.47..1.47 rows=47 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.47 rows=47 width=4)
-> Hash (cost=157.10..157.10 rows=1510 width=36)
-> Subquery Scan on scp (cost=123.12..157.10 rows=1510 width=36)
-> HashAggregate (cost=123.12..142.00 rows=1510 width=36)
Group Key: t0.id
-> Hash Right Join (cost=74.12..113.82 rows=1860 width=10)
Hash Cond: (t1.contract_id = t0.id)
-> Seq Scan on tbl_sales_contract_payments t1 (cost=0.00..34.77 rows=1877 width=10)
-> Hash (cost=55.24..55.24 rows=1510 width=4)
-> Seq Scan on tbl_sales_contracts t0 (cost=0.00..55.24 rows=1510 width=4)
Filter: need_analysis
-> Hash (cost=173.30..173.30 rows=1510 width=36)
-> Subquery Scan on sci (cost=139.33..173.30 rows=1510 width=36)
-> HashAggregate (cost=139.33..158.20 rows=1510 width=36)
Group Key: t0_1.id
-> Hash Right Join (cost=74.12..130.82 rows=1702 width=17)
Hash Cond: (t1_1.contract_id = t0_1.id)
-> Seq Scan on tbl_sales_contract_invoices t1_1 (cost=0.00..52.18 rows=1718 width=10)
-> Hash (cost=55.24..55.24 rows=1510 width=11)
-> Seq Scan on tbl_sales_contracts t0_1 (cost=0.00..55.24 rows=1510 width=11)
Filter: need_analysis
-> GroupAggregate (cost=52.47..52.50 rows=1 width=44)
Group Key: t0_2.id
-> Sort (cost=52.47..52.48 rows=1 width=14)
Sort Key: t0_2.id
-> Nested Loop (cost=0.28..52.46 rows=1 width=14)
-> Seq Scan on tbl_sales_contract_payments t1_2 (cost=0.00..44.16 rows=1 width=14)
Filter: ((collected_date >= '2025-06-18'::date) AND (collected_date <= '2025-07-03'::date))
-> Index Only Scan using tbl_sales_contracts_pkey on tbl_sales_contracts t0_2 (cost=0.28..8.29 rows=1 width=4)
Index Cond: (id = t1_2.contract_id)
-> GroupAggregate (cost=447.23..40768.43 rows=200 width=72)
Group Key: sc_1.id
-> Nested Loop Left Join (cost=447.23..30750.05 rows=454 width=44)
-> Merge Join (cost=447.09..30514.48 rows=454 width=48)
Merge Cond: (scpcrd.contract_id = sc_1.id)
Join Filter: ((CASE WHEN (scp_1.collected IS NOT NULL) THEN scp_1.collected ELSE '0'::numeric END) < (SubPlan 2))
-> Sort (cost=65.50..67.82 rows=929 width=8)
Sort Key: scpcrd.contract_id
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates scpcrd (cost=0.00..19.70 rows=929 width=8)
Filter: (remind_date <= '2025-07-03'::date)
-> Sort (cost=381.60..384.43 rows=1133 width=40)
Sort Key: sc_1.id
-> Hash Left Join (cost=289.86..324.12 rows=1133 width=40)
Hash Cond: (sc_1.client_id = c_1.id)
Filter: ((c_1.sales_rep = ANY ('{5049,5301,5022,5209,5026,5293,5309,5356,5329,5299,5303,5302,5332,5047,1026,5096,5070,5448,5420,1029,1001,5037,5087,1008,5048,5043,5045,5230,5031,5088,5208,1003,5294,5357,5044,1030,5076,5300,5027,5089,5311,5066,5069,5364,1009,5295,1013,5128,5203,1025,5032,5075,1004,5072,5033,5359,5412,5409,5229,1005,5304,5449,5358,1006,5093}'::integer[])) OR (c_1.sales_rep IS NULL))
-> HashAggregate (cost=161.49..176.59 rows=1510 width=1054)
Group Key: sc_1.id, scp_1.collected
-> Hash Left Join (cost=94.73..153.94 rows=1510 width=40)
Hash Cond: (sc_1.id = scp_1.contract_id)
-> Seq Scan on tbl_sales_contracts sc_1 (cost=0.00..55.24 rows=1510 width=8)
Filter: need_analysis
-> Hash (cost=76.67..76.67 rows=1445 width=36)
-> Subquery Scan on scp_1 (cost=44.16..76.67 rows=1445 width=36)
-> HashAggregate (cost=44.16..62.22 rows=1445 width=36)
Group Key: tbl_sales_contract_payments.contract_id
-> Seq Scan on tbl_sales_contract_payments (cost=0.00..34.77 rows=1877 width=10)
-> Hash (cost=104.83..104.83 rows=1883 width=8)
-> Seq Scan on tbl_clients c_1 (cost=0.00..104.83 rows=1883 width=8)
SubPlan 2
-> Aggregate (cost=22.05..22.06 rows=1 width=32)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates scpcrdsub_1 (cost=0.00..22.04 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
-> Index Only Scan using tbl_sales_group_relationships_pkey on tbl_sales_group_relationships sgr_1 (cost=0.14..0.51 rows=1 width=4)
Index Cond: (sales_id = c_1.sales_rep)
SubPlan 1
-> Aggregate (cost=22.05..22.06 rows=1 width=32)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates scpcrdsub (cost=0.00..22.04 rows=1 width=6)
Filter: ((remind_date <= scpcrd.remind_date) AND (contract_id = scpcrd.contract_id))
-> Group (cost=44.16..44.17 rows=1 width=4)
Group Key: tbl_sales_contract_payments_1.contract_id
-> Sort (cost=44.16..44.17 rows=1 width=4)
Sort Key: tbl_sales_contract_payments_1.contract_id
-> Seq Scan on tbl_sales_contract_payments tbl_sales_contract_payments_1 (cost=0.00..44.16 rows=1 width=4)
Filter: ((collected_date >= '2025-06-18'::date) AND (collected_date <= '2025-07-03'::date))