QUERY PLAN
Aggregate (cost=2122.72..2122.73 rows=1 width=8)
-> Sort (cost=2122.69..2122.69 rows=2 width=1099)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=2122.51..2122.68 rows=2 width=1099)
Group Key: c.id
-> Merge Left Join (cost=2122.51..2122.67 rows=2 width=16)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=1443.29..1443.30 rows=2 width=16)
Sort Key: c.id
-> Nested Loop (cost=759.26..1443.28 rows=2 width=16)
-> Hash Right Join (cost=8.31..9.93 rows=1 width=4)
Hash Cond: (sgr.sales_id = client.sales_rep)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.49 rows=49 width=4)
-> Hash (cost=8.29..8.29 rows=1 width=8)
-> Index Scan using tbl_clients_pkey on tbl_clients client (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = 982)
-> Hash Right Join (cost=750.95..1433.34 rows=2 width=20)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..678.01 rows=1164 width=4)
Filter: (object_type = 106)
-> Hash (cost=750.92..750.92 rows=2 width=20)
-> Hash Right Join (cost=68.49..750.92 rows=2 width=20)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..678.01 rows=1685 width=4)
Filter: (object_type = 105)
-> Hash (cost=68.46..68.46 rows=2 width=24)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..68.46 rows=2 width=24)
Filter: (client_id = 982)
-> Sort (cost=679.21..679.32 rows=44 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..678.01 rows=44 width=4)
Filter: (object_type = 107)