QUERY PLAN
Aggregate (cost=2193.17..2193.18 rows=1 width=32)
-> Nested Loop (cost=2161.43..2192.93 rows=47 width=10)
-> HashAggregate (cost=2161.15..2161.62 rows=47 width=4)
Group Key: tbl_sales_contracts.from_project
-> Hash Join (cost=2095.99..2161.03 rows=47 width=4)
Hash Cond: (tbl_sales_contracts.id = "ANY_subquery".id)
-> Seq Scan on tbl_sales_contracts (cost=0.00..60.86 rows=1586 width=8)
-> Hash (cost=2095.41..2095.41 rows=47 width=4)
-> Subquery Scan on "ANY_subquery" (cost=2094.82..2095.41 rows=47 width=4)
-> Sort (cost=2094.82..2094.94 rows=47 width=16)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=2093.28..2093.51 rows=47 width=16)
Group Key: c.id
-> Sort (cost=2093.28..2093.40 rows=47 width=16)
Sort Key: c.id
-> Hash Left Join (cost=1463.92..2091.97 rows=47 width=16)
Hash Cond: (client.sales_rep = sgr.sales_id)
-> Hash Right Join (cost=1462.30..2089.70 rows=47 width=20)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..621.20 rows=1631 width=4)
Filter: (object_type = 105)
-> Hash (cost=1461.71..1461.71 rows=47 width=20)
-> Hash Right Join (cost=836.17..1461.71 rows=47 width=20)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..621.20 rows=1142 width=4)
Filter: (object_type = 106)
-> Hash (cost=835.58..835.58 rows=47 width=20)
-> Hash Right Join (cost=214.15..835.58 rows=47 width=20)
Hash Cond: (vs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..621.20 rows=58 width=4)
Filter: (object_type = 107)
-> Hash (cost=213.56..213.56 rows=47 width=20)
-> Hash Right Join (cost=167.73..213.56 rows=47 width=20)
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.23..167.23 rows=40 width=20)
-> Hash Join (cost=102.27..167.23 rows=40 width=20)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..60.86 rows=1560 width=24)
Filter: (from_project IS NOT NULL)
-> Hash (cost=101.64..101.64 rows=50 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=14.48..101.64 rows=50 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)
-> 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 = 1001)
-> Index Scan using tbl_projects_pkey on tbl_projects (cost=0.28..0.67 rows=1 width=14)
Index Cond: (id = tbl_sales_contracts.from_project)