QUERY PLAN
Aggregate (cost=291.27..291.28 rows=1 width=32)
-> Nested Loop (cost=290.87..291.27 rows=1 width=10)
-> HashAggregate (cost=290.59..290.60 rows=1 width=4)
Group Key: tbl_sales_contracts.from_project
-> Nested Loop (cost=282.54..290.59 rows=1 width=4)
-> Group (cost=282.26..282.27 rows=1 width=4)
Group Key: c.id
-> Sort (cost=282.26..282.27 rows=1 width=4)
Sort Key: c.id
-> Nested Loop Left Join (cost=184.60..282.25 rows=1 width=4)
-> Hash Join (cost=184.46..281.38 rows=1 width=8)
Hash Cond: (client.id = c.client_id)
-> Bitmap Heap Scan on tbl_clients client (cost=14.46..108.61 rows=58 width=8)
Recheck Cond: (sales_rep = 1001)
-> Bitmap Index Scan on tbl_clients_sales_rep_idx (cost=0.00..4.78 rows=67 width=0)
Index Cond: (sales_rep = 1001)
-> Hash (cost=169.55..169.55 rows=36 width=8)
-> Hash Right Join (cost=118.61..169.55 rows=36 width=8)
Hash Cond: (sci2.contract_id = c.id)
-> Seq Scan on tbl_sales_contract_invoices sci2 (cost=0.00..45.33 rows=2133 width=4)
-> Hash (cost=118.25..118.25 rows=29 width=12)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..118.25 rows=29 width=12)
Filter: ((from_project IS NULL) AND build_by_project)
-> Index Only Scan using tbl_sales_group_relationships_pkey on tbl_sales_group_relationships sgr (cost=0.14..0.87 rows=1 width=4)
Index Cond: ((sales_id = client.sales_rep) AND (sales_id = 1001))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = c.id)
-> 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)