QUERY PLAN
Aggregate (cost=293.79..293.80 rows=1 width=8)
-> Sort (cost=292.88..293.03 rows=61 width=1104)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=290.76..291.07 rows=61 width=1104)
Group Key: c.id
-> Sort (cost=290.76..290.92 rows=61 width=16)
Sort Key: c.id
-> Hash Right Join (cost=235.04..288.95 rows=61 width=16)
Hash Cond: (sci2.contract_id = c.id)
-> Seq Scan on tbl_sales_contract_invoices sci2 (cost=0.00..45.33 rows=2133 width=4)
-> Hash (cost=234.42..234.42 rows=49 width=16)
-> Nested Loop Left Join (cost=109.33..234.42 rows=49 width=16)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Join (cost=109.33..232.05 rows=49 width=20)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..118.25 rows=1696 width=24)
Filter: (from_project IS NOT NULL)
-> Hash (cost=108.61..108.61 rows=58 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=14.46..108.61 rows=58 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)
-> 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)