QUERY PLAN
Aggregate (cost=2123.93..2123.94 rows=1 width=8)
-> Sort (cost=2123.72..2123.75 rows=14 width=1099)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=2123.19..2123.45 rows=14 width=1099)
Group Key: c.id
-> Merge Left Join (cost=2123.19..2123.42 rows=14 width=16)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=1443.98..1444.01 rows=14 width=16)
Sort Key: c.id
-> Nested Loop (cost=759.56..1443.71 rows=14 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 = 518)
-> Hash Right Join (cost=751.25..1433.65 rows=14 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=751.07..751.07 rows=14 width=20)
-> Hash Right Join (cost=68.64..751.07 rows=14 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=14 width=24)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..68.46 rows=14 width=24)
Filter: (client_id = 518)
-> 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)