QUERY PLAN
Aggregate (cost=1483.86..1483.87 rows=1 width=32)
-> Hash Semi Join (cost=1426.47..1483.73 rows=51 width=6)
Hash Cond: (tbl_sales_contract_invoices.contract_id = tbl_sales_contracts.id)
-> Seq Scan on tbl_sales_contract_invoices (cost=0.00..52.18 rows=1718 width=10)
-> Hash (cost=1425.93..1425.93 rows=43 width=8)
-> Hash Join (cost=1366.68..1425.93 rows=43 width=8)
Hash Cond: (tbl_sales_contracts.id = "ANY_subquery".id)
-> Seq Scan on tbl_sales_contracts (cost=0.00..55.24 rows=1523 width=4)
Filter: including_tax
-> Hash (cost=1366.14..1366.14 rows=43 width=4)
-> Subquery Scan on "ANY_subquery" (cost=1365.61..1366.14 rows=43 width=4)
-> Sort (cost=1365.61..1365.71 rows=43 width=16)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Group (cost=1364.22..1364.44 rows=43 width=16)
Group Key: c.id
-> Sort (cost=1364.22..1364.33 rows=43 width=16)
Sort Key: c.id
-> Hash Right Join (cost=980.98..1363.06 rows=43 width=16)
Hash Cond: (cs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..376.24 rows=1535 width=4)
Filter: (object_type = 105)
-> Hash (cost=980.45..980.45 rows=43 width=16)
-> Hash Right Join (cost=600.12..980.45 rows=43 width=16)
Hash Cond: (rs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships rs (cost=0.00..376.24 rows=1074 width=4)
Filter: (object_type = 106)
-> Hash (cost=599.58..599.58 rows=43 width=16)
-> Hash Right Join (cost=223.23..599.58 rows=43 width=16)
Hash Cond: (vs.object_id = c.id)
-> Seq Scan on tbl_attachement_relationships vs (cost=0.00..376.24 rows=29 width=4)
Filter: (object_type = 107)
-> Hash (cost=222.69..222.69 rows=43 width=16)
-> Hash Right Join (cost=163.64..222.69 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.16..163.16 rows=38 width=16)
-> Nested Loop Left Join (cost=101.82..163.16 rows=38 width=16)
Join Filter: (sgr.sales_id = client.sales_rep)
-> Hash Join (cost=101.82..161.00 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.22..101.22 rows=48 width=8)
-> Bitmap Heap Scan on tbl_clients client (cost=16.12..101.22 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)