QUERY PLAN
Limit (cost=6562.36..6562.41 rows=20 width=322)
-> Sort (cost=6562.36..6562.46 rows=39 width=322)
Sort Key: p.bargain_date DESC, p.created_at DESC
-> GroupAggregate (cost=6558.01..6561.33 rows=39 width=322)
Group Key: sc.id, p.id
-> Sort (cost=6558.01..6558.11 rows=39 width=238)
Sort Key: sc.id, p.id
-> Nested Loop Left Join (cost=512.57..6556.98 rows=39 width=238)
Join Filter: (od.id = sc.id)
-> Hash Left Join (cost=101.55..383.45 rows=39 width=198)
Hash Cond: (sc.id = tcollect.project_used_client_id)
-> Hash Left Join (cost=69.61..351.42 rows=39 width=158)
Hash Cond: (sc.id = sci.project_used_client_id)
-> Hash Left Join (cost=36.40..318.10 rows=39 width=126)
Hash Cond: (c.sales_rep = sgr.sales_id)
-> Nested Loop Left Join (cost=34.29..315.65 rows=39 width=130)
Filter: ((c.sales_rep = ANY ('{1001,5049,5301,5022,5209,5026,5293,5309,5356,5329,5299,5303,5302,5332,5047,1026,5096,5070,5448,5420,1029,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))
-> Hash Join (cost=34.02..283.09 rows=52 width=126)
Hash Cond: (p.id = sc.project_id)
-> Seq Scan on tbl_projects p (cost=0.00..242.68 rows=1567 width=20)
Filter: (project_status = 3)
-> Hash (cost=32.80..32.80 rows=97 width=106)
-> Hash Right Join (cost=28.50..32.80 rows=97 width=106)
Hash Cond: (t0.id = sc.id)
-> HashAggregate (cost=24.57..26.71 rows=171 width=36)
Group Key: t0.id
-> Hash Right Join (cost=4.85..21.82 rows=550 width=36)
Hash Cond: (t1.project_used_client_id = t0.id)
-> Seq Scan on tbl_project_used_client_payments t1 (cost=0.00..15.50 rows=550 width=36)
-> Hash (cost=2.71..2.71 rows=171 width=4)
-> Seq Scan on tbl_project_used_clients t0 (cost=0.00..2.71 rows=171 width=4)
-> Hash (cost=2.71..2.71 rows=97 width=74)
-> Seq Scan on tbl_project_used_clients sc (cost=0.00..2.71 rows=97 width=74)
Filter: (used_client IS NOT NULL)
-> Index Scan using tbl_clients_pkey on tbl_clients c (cost=0.28..0.53 rows=1 width=8)
Index Cond: (p.client_id = id)
-> Hash (cost=1.49..1.49 rows=49 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.49 rows=49 width=4)
-> Hash (cost=31.08..31.08 rows=171 width=36)
-> Subquery Scan on sci (cost=27.23..31.08 rows=171 width=36)
-> HashAggregate (cost=27.23..29.37 rows=171 width=36)
Group Key: t0_1.id
-> Hash Right Join (cost=4.85..23.73 rows=700 width=36)
Hash Cond: (t1_1.project_used_client_id = t0_1.id)
-> Seq Scan on tbl_project_used_client_invoices t1_1 (cost=0.00..17.00 rows=700 width=36)
-> Hash (cost=2.71..2.71 rows=171 width=4)
-> Seq Scan on tbl_project_used_clients t0_1 (cost=0.00..2.71 rows=171 width=4)
-> Hash (cost=29.80..29.80 rows=171 width=44)
-> Subquery Scan on tcollect (cost=25.95..29.80 rows=171 width=44)
-> HashAggregate (cost=25.95..28.09 rows=171 width=44)
Group Key: t0_2.id
-> Hash Right Join (cost=4.85..21.82 rows=550 width=40)
Hash Cond: (t1_2.project_used_client_id = t0_2.id)
-> Seq Scan on tbl_project_used_client_payments t1_2 (cost=0.00..15.50 rows=550 width=40)
-> Hash (cost=2.71..2.71 rows=171 width=4)
-> Seq Scan on tbl_project_used_clients t0_2 (cost=0.00..2.71 rows=171 width=4)
-> Materialize (cost=411.02..6137.41 rows=62 width=44)
-> Subquery Scan on od (cost=411.02..6137.10 rows=62 width=44)
-> GroupAggregate (cost=411.02..6136.48 rows=62 width=48)
Group Key: sc_1.id
-> Merge Join (cost=411.02..4705.52 rows=62 width=44)
Merge Cond: (sc_1.id = scpcrd.project_used_client_id)
Join Filter: (sc_1.collected < ((SubPlan 2))::numeric)
-> Sort (cost=378.28..378.60 rows=128 width=36)
Sort Key: sc_1.id
-> Hash Left Join (cost=36.93..373.80 rows=128 width=36)
Hash Cond: (c_1.sales_rep = sgr_1.sales_id)
-> Nested Loop Left Join (cost=34.82..370.56 rows=128 width=40)
Filter: ((c_1.sales_rep = ANY ('{1001,5049,5301,5022,5209,5026,5293,5309,5356,5329,5299,5303,5302,5332,5047,1026,5096,5070,5448,5420,1029,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))
-> Hash Right Join (cost=34.55..282.60 rows=171 width=40)
Hash Cond: (p_1.id = sc_1.project_id)
-> Seq Scan on tbl_projects p_1 (cost=0.00..235.34 rows=2934 width=8)
-> Hash (cost=32.41..32.41 rows=171 width=40)
-> Subquery Scan on sc_1 (cost=28.99..32.41 rows=171 width=40)
-> HashAggregate (cost=28.99..30.70 rows=171 width=220)
Group Key: sc_2.id, (sum(tbl_project_used_client_payments.amount))
-> Hash Right Join (cost=23.10..28.13 rows=171 width=40)
Hash Cond: (tbl_project_used_client_payments.project_used_client_id = sc_2.id)
-> HashAggregate (cost=18.25..20.75 rows=200 width=36)
Group Key: tbl_project_used_client_payments.project_used_client_id
-> Seq Scan on tbl_project_used_client_payments (cost=0.00..15.50 rows=550 width=36)
-> Hash (cost=2.71..2.71 rows=171 width=8)
-> Seq Scan on tbl_project_used_clients sc_2 (cost=0.00..2.71 rows=171 width=8)
-> Index Scan using tbl_clients_pkey on tbl_clients c_1 (cost=0.28..0.42 rows=1 width=8)
Index Cond: (p_1.client_id = id)
-> Hash (cost=1.49..1.49 rows=49 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr_1 (cost=0.00..1.49 rows=49 width=4)
-> Sort (cost=32.74..33.46 rows=290 width=8)
Sort Key: scpcrd.project_used_client_id
-> Seq Scan on tbl_project_used_client_payment_collection_remind_dates scpcrd (cost=0.00..20.88 rows=290 width=8)
Filter: (remind_date <= '2025-08-15'::date)
SubPlan 2
-> Aggregate (cost=23.05..23.06 rows=1 width=8)
-> Seq Scan on tbl_project_used_client_payment_collection_remind_dates scpcrdsub_1 (cost=0.00..23.05 rows=1 width=4)
Filter: ((remind_date <= scpcrd.remind_date) AND (project_used_client_id = scpcrd.project_used_client_id))
SubPlan 1
-> Aggregate (cost=23.05..23.06 rows=1 width=8)
-> Seq Scan on tbl_project_used_client_payment_collection_remind_dates scpcrdsub (cost=0.00..23.05 rows=1 width=4)
Filter: ((remind_date <= scpcrd.remind_date) AND (project_used_client_id = scpcrd.project_used_client_id))