QUERY PLAN
Limit (cost=283.06..283.07 rows=1 width=211)
-> Sort (cost=283.06..283.07 rows=1 width=211)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=283.04..283.05 rows=1 width=211)
Group Key: c.id
-> Sort (cost=283.04..283.04 rows=1 width=203)
Sort Key: c.id
-> Nested Loop Left Join (cost=184.46..283.03 rows=1 width=203)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Join (cost=184.46..281.38 rows=1 width=207)
Hash Cond: (client.id = c.client_id)
-> 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)
-> Hash (cost=169.55..169.55 rows=36 width=203)
-> Hash Right Join (cost=118.61..169.55 rows=36 width=203)
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=118.25..118.25 rows=29 width=203)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..118.25 rows=29 width=203)
Filter: ((from_project IS NULL) AND build_by_project)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.64 rows=1 width=4)
Filter: (sales_id = 1001)