QUERY PLAN
Aggregate (cost=1356.41..1356.42 rows=1 width=8)
-> Sort (cost=1355.76..1355.87 rows=43 width=998)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=1354.38..1354.59 rows=43 width=998)
Group Key: c.id
-> Sort (cost=1354.38..1354.49 rows=43 width=16)
Sort Key: c.id
-> Hash Right Join (cost=974.49..1353.21 rows=43 width=16)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..372.94 rows=1522 width=4)
Filter: (object_type = 105)
-> Hash (cost=973.95..973.95 rows=43 width=16)
-> Hash Right Join (cost=596.96..973.95 rows=43 width=16)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..372.94 rows=1065 width=4)
Filter: (object_type = 106)
-> Hash (cost=596.42..596.42 rows=43 width=16)
-> Hash Right Join (cost=223.37..596.42 rows=43 width=16)
Hash Cond: (vs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..372.94 rows=29 width=4)
Filter: (object_type = 107)
-> Hash (cost=222.83..222.83 rows=43 width=16)
-> Hash Right Join (cost=163.78..222.83 rows=43 width=16)
Hash Cond: (sci2.contract_id = c.id)
-> Seq Scan on tbl_sales_contract_invoices sci2 (cost=0.00..52.18 rows=1718 width=4)
-> Hash (cost=163.30..163.30 rows=38 width=16)
-> Nested Loop Left Join (cost=101.96..163.30 rows=38 width=16)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Join (cost=101.96..161.14 rows=38 width=20)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..55.24 rows=1498 width=24)
Filter: (from_project IS NOT NULL)
-> Hash (cost=101.36..101.36 rows=48 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=16.12..101.36 rows=48 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.69 rows=55 width=0)
Index Cond: (sales_rep = 1001)
-> Materialize (cost=0.00..1.59 rows=1 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.59 rows=1 width=4)
Filter: (sales_id = 1001)