QUERY PLAN
Aggregate (cost=210.94..210.95 rows=1 width=8)
-> Sort (cost=210.01..210.17 rows=62 width=1104)
Sort Key: c.updated_at DESC
-> Group (cost=207.86..208.17 rows=62 width=1104)
Group Key: c.id
-> Sort (cost=207.86..208.01 rows=62 width=12)
Sort Key: c.id
-> Nested Loop Left Join (cost=109.42..206.01 rows=62 width=12)
-> Nested Loop Left Join (cost=109.14..185.81 rows=53 width=12)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Join (cost=109.14..183.37 rows=53 width=16)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..69.66 rows=1736 width=20)
Filter: (from_project IS NOT NULL)
-> Hash (cost=108.38..108.38 rows=61 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=13.11..108.38 rows=61 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.80 rows=70 width=0)
Index Cond: (sales_rep = 1001)
-> Materialize (cost=0.00..1.64 rows=1 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.64 rows=1 width=4)
Filter: (sales_id = 1001)
-> Index Only Scan using tbl_sales_contract_invoices_contract_id_idx on tbl_sales_contract_invoices sci2 (cost=0.28..0.37 rows=1 width=4)
Index Cond: (contract_id = c.id)