QUERY PLAN
Limit (cost=106.79..106.84 rows=20 width=1102)
-> Sort (cost=106.79..106.97 rows=70 width=1102)
Sort Key: c.signed_at DESC, c.created_at DESC
-> HashAggregate (cost=103.71..104.93 rows=70 width=1102)
Group Key: c.id
-> Nested Loop Left Join (cost=78.10..103.41 rows=120 width=1094)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Right Join (cost=54.64..67.57 rows=12 width=1098)
Hash Cond: (sci2.contract_id = c.id)
-> Seq Scan on tbl_sales_contract_invoices sci2 (cost=0.00..12.10 rows=210 width=4)
-> Hash (cost=54.59..54.59 rows=4 width=1098)
-> Hash Join (cost=43.70..54.59 rows=4 width=1098)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..10.70 rows=70 width=1094)
Filter: (from_project IS NOT NULL)
-> Hash (cost=43.69..43.69 rows=1 width=8)
-> Index Scan using tbl_clients_sales_rep_idx on tbl_clients client (cost=35.66..43.69 rows=1 width=8)
Index Cond: (sales_rep = 5493)
-> Materialize (cost=23.45..34.06 rows=10 width=4)
-> Bitmap Heap Scan on tbl_sales_group_relationships sgr (cost=23.45..34.01 rows=10 width=4)
Recheck Cond: (sales_id = 5493)
-> Bitmap Index Scan on tbl_sales_group_relationships_pkey (cost=0.00..23.45 rows=10 width=0)
Index Cond: (sales_id = 5493)