QUERY PLAN
Aggregate (cost=870.19..870.20 rows=1 width=32)
-> GroupAggregate (cost=868.90..869.65 rows=43 width=36)
Group Key: sub.id2
-> Sort (cost=868.90..869.00 rows=43 width=36)
Sort Key: sub.id2
-> Subquery Scan on sub (cost=867.19..867.73 rows=43 width=36)
-> Sort (cost=867.19..867.30 rows=43 width=571)
Sort Key: (CASE WHEN (t0_1.id IS NOT NULL) THEN t0_1.remind_date ELSE sc.signed_at END) DESC
-> Nested Loop (cost=818.56..866.02 rows=43 width=571)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.77 rows=1 width=4)
Filter: ((sales_id = 1001) AND (group_id = 1))
-> Hash Right Join (cost=818.56..863.83 rows=43 width=52)
Hash Cond: (t0.id = sc.id)
-> HashAggregate (cost=201.10..222.45 rows=1708 width=36)
Group Key: t0.id
-> Hash Right Join (cost=139.60..190.54 rows=2112 width=17)
Hash Cond: (t1.contract_id = t0.id)
-> Seq Scan on tbl_sales_contract_invoices t1 (cost=0.00..45.33 rows=2133 width=10)
-> Hash (cost=118.25..118.25 rows=1708 width=11)
-> Seq Scan on tbl_sales_contracts t0 (cost=0.00..118.25 rows=1708 width=11)
Filter: need_analysis
-> Hash (cost=616.93..616.93 rows=43 width=20)
-> Hash Right Join (cost=601.90..616.93 rows=43 width=20)
Hash Cond: (t0_1.contract_id = sc.id)
-> HashAggregate (cost=379.20..385.46 rows=626 width=93)
Group Key: t0_1.id
-> Hash Join (cost=180.87..377.64 rows=626 width=12)
Hash Cond: (p.id = t1_1.from_project)
-> Seq Scan on tbl_projects p (cost=0.00..184.28 rows=1691 width=4)
Filter: (project_status = 3)
-> Hash (cost=166.34..166.34 rows=1163 width=16)
-> Hash Join (cost=139.24..166.34 rows=1163 width=16)
Hash Cond: (t0_1.contract_id = t1_1.id)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0_1 (cost=0.00..23.95 rows=1195 width=12)
-> Hash (cost=118.25..118.25 rows=1679 width=8)
-> Seq Scan on tbl_sales_contracts t1_1 (cost=0.00..118.25 rows=1679 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Hash (cost=222.16..222.16 rows=43 width=12)
-> Hash Join (cost=99.40..222.16 rows=43 width=12)
Hash Cond: (sc.client_id = c.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1713 width=16)
Filter: build_by_project
-> Hash (cost=98.78..98.78 rows=50 width=8)
-> Bitmap Heap Scan on tbl_clients c (cost=4.79..98.78 rows=50 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)