QUERY PLAN
Limit (cost=2106.93..2106.98 rows=20 width=202)
-> Sort (cost=2106.93..2107.05 rows=47 width=202)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=2102.45..2105.68 rows=47 width=202)
Group Key: c.id
-> Nested Loop Left Join (cost=2102.45..2105.21 rows=47 width=194)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Merge Left Join (cost=2102.45..2102.89 rows=47 width=198)
Merge Cond: (c.id = vs.object_id)
-> Sort (cost=1476.23..1476.35 rows=47 width=198)
Sort Key: c.id
-> Hash Right Join (cost=844.18..1474.93 rows=47 width=198)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..624.51 rows=1640 width=4)
Filter: (object_type = 105)
-> Hash (cost=843.59..843.59 rows=47 width=198)
-> Hash Right Join (cost=214.71..843.59 rows=47 width=198)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..624.51 rows=1149 width=4)
Filter: (object_type = 106)
-> Hash (cost=214.12..214.12 rows=47 width=198)
-> Hash Right Join (cost=168.29..214.12 rows=47 width=198)
Hash Cond: (sci2.contract_id = c.id)
-> Seq Scan on tbl_sales_contract_invoices sci2 (cost=0.00..38.45 rows=1845 width=4)
-> Hash (cost=167.79..167.79 rows=40 width=198)
-> Hash Join (cost=102.82..167.79 rows=40 width=198)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..60.86 rows=1560 width=194)
Filter: (from_project IS NOT NULL)
-> Hash (cost=102.19..102.19 rows=51 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=14.48..102.19 rows=51 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.71 rows=58 width=0)
Index Cond: (sales_rep = 1001)
-> Sort (cost=626.21..626.36 rows=58 width=4)
Sort Key: vs.object_id
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..624.51 rows=58 width=4)
Filter: (object_type = 107)
-> Materialize (cost=0.00..1.62 rows=1 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.61 rows=1 width=4)
Filter: (sales_id = 1001)