QUERY PLAN
Aggregate (cost=2136.43..2136.44 rows=1 width=8)
-> Sort (cost=2133.34..2133.85 rows=206 width=1067)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=2123.72..2125.42 rows=206 width=1067)
Group Key: c.id
-> Merge Left Join (cost=2123.72..2124.91 rows=206 width=16)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=1479.99..1480.51 rows=206 width=16)
Sort Key: c.id
-> Hash Left Join (cost=820.08..1472.08 rows=206 width=16)
Hash Cond: (client.sales_rep = sgr.sales_id)
-> Hash Right Join (cost=818.46..1467.62 rows=206 width=20)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..642.56 rows=1655 width=4)
Filter: (object_type = 105)
-> Hash (cost=815.88..815.88 rows=206 width=20)
-> Hash Right Join (cost=168.75..815.88 rows=206 width=20)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..642.56 rows=1146 width=4)
Filter: (object_type = 106)
-> Hash (cost=166.18..166.18 rows=206 width=20)
-> Hash Join (cost=98.07..166.18 rows=206 width=20)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..63.71 rows=1671 width=24)
-> Hash (cost=95.10..95.10 rows=238 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=6.12..95.10 rows=238 width=8)
Recheck Cond: (sales_rep = 5332)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..6.06 rows=238 width=0)
Index Cond: (sales_rep = 5332)
-> Hash (cost=1.61..1.61 rows=1 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.61 rows=1 width=4)
Filter: (sales_id = 5332)
-> Sort (cost=643.73..643.84 rows=43 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..642.56 rows=43 width=4)
Filter: (object_type = 107)