QUERY PLAN
Aggregate (cost=1930.94..1930.95 rows=1 width=32)
-> Hash Join (cost=1699.82..1930.82 rows=47 width=6)
Hash Cond: (tbl_sales_contracts.id = "ANY_subquery".id)
-> Seq Scan on tbl_sales_contracts (cost=0.00..226.84 rows=1583 width=10)
Filter: including_tax
-> Hash (cost=1699.23..1699.23 rows=47 width=4)
-> Subquery Scan on "ANY_subquery" (cost=1698.64..1699.23 rows=47 width=4)
-> Sort (cost=1698.64..1698.76 rows=47 width=16)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=1697.10..1697.34 rows=47 width=16)
Group Key: c.id
-> Sort (cost=1697.10..1697.22 rows=47 width=16)
Sort Key: c.id
-> Hash Right Join (cost=1256.22..1695.80 rows=47 width=16)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..433.06 rows=1714 width=4)
Filter: (object_type = 105)
-> Hash (cost=1255.63..1255.63 rows=47 width=16)
-> Hash Right Join (cost=818.01..1255.63 rows=47 width=16)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..433.06 rows=1198 width=4)
Filter: (object_type = 106)
-> Hash (cost=817.43..817.43 rows=47 width=16)
-> Hash Right Join (cost=384.23..817.43 rows=47 width=16)
Hash Cond: (vs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..433.06 rows=32 width=4)
Filter: (object_type = 107)
-> Hash (cost=383.65..383.65 rows=47 width=16)
-> Hash Right Join (cost=337.96..383.65 rows=47 width=16)
Hash Cond: (sci2.contract_id = c.id)
-> Seq Scan on tbl_sales_contract_invoices sci2 (cost=0.00..38.34 rows=1834 width=4)
-> Hash (cost=337.45..337.45 rows=41 width=16)
-> Nested Loop Left Join (cost=104.28..337.45 rows=41 width=16)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Join (cost=104.28..335.22 rows=41 width=20)
Hash Cond: (c.client_id = client.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..226.84 rows=1558 width=24)
Filter: (from_project IS NOT NULL)
-> Hash (cost=103.63..103.63 rows=52 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=14.49..103.63 rows=52 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.72 rows=59 width=0)
Index Cond: (sales_rep = 1001)
-> 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)