QUERY PLAN
Limit (cost=1356.40..1356.45 rows=20 width=188)
-> Sort (cost=1356.40..1356.51 rows=43 width=188)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=1352.27..1355.26 rows=43 width=188)
Group Key: c.id
-> Nested Loop Left Join (cost=1352.27..1354.83 rows=43 width=180)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Merge Left Join (cost=1352.27..1352.59 rows=43 width=184)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=978.63..978.73 rows=43 width=184)
Sort Key: c.id
-> Hash Right Join (cost=598.73..977.46 rows=43 width=184)
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=598.20..598.20 rows=43 width=184)
-> Hash Right Join (cost=221.21..598.20 rows=43 width=184)
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=220.67..220.67 rows=43 width=184)
-> Hash Right Join (cost=161.62..220.67 rows=43 width=184)
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=161.14..161.14 rows=38 width=184)
-> Hash Join (cost=101.96..161.14 rows=38 width=184)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..55.24 rows=1498 width=180)
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)
-> Sort (cost=373.64..373.71 rows=29 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..372.94 rows=29 width=4)
Filter: (object_type = 107)
-> 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)