QUERY PLAN
Aggregate (cost=620.93..620.94 rows=1 width=8)
-> Sort (cost=620.29..620.40 rows=43 width=571)
Sort Key: (CASE WHEN (t0.id IS NOT NULL) THEN t0.remind_date ELSE sc.signed_at END) DESC
-> Nested Loop (cost=601.90..619.12 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=601.90..616.93 rows=43 width=16)
Hash Cond: (t0.contract_id = sc.id)
-> HashAggregate (cost=379.20..385.46 rows=626 width=93)
Group Key: t0.id
-> Hash Join (cost=180.87..377.64 rows=626 width=12)
Hash Cond: (p.id = t1.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.contract_id = t1.id)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0 (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 (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)