QUERY PLAN
Limit (cost=840.19..840.24 rows=20 width=188)
-> Sort (cost=840.19..841.58 rows=559 width=188)
Sort Key: c.signed_at DESC, c.created_at DESC
-> Hash Left Join (cost=601.49..825.31 rows=559 width=188)
Hash Cond: (client.sales_rep = sgr.sales_id)
-> Hash Left Join (cost=599.43..814.09 rows=559 width=184)
Hash Cond: (c.client_id = client.id)
-> Hash Join (cost=471.07..684.25 rows=559 width=180)
Hash Cond: (p.id = c.project_id)
-> Seq Scan on tbl_projects p (cost=0.00..201.62 rows=1607 width=4)
Filter: (created_by = ANY ('{1001,1004,1005,1013,5087,5295,5300,5311,5329,5332,5420}'::integer[]))
-> Hash (cost=458.88..458.88 rows=975 width=180)
-> Hash Join (cost=399.66..458.88 rows=975 width=180)
Hash Cond: (c.id = sub.id)
-> Seq Scan on tbl_sales_contracts c (cost=0.00..55.24 rows=1510 width=180)
Filter: need_analysis
-> Hash (cost=387.36..387.36 rows=984 width=4)
-> Subquery Scan on sub (cost=365.22..387.36 rows=984 width=4)
-> HashAggregate (cost=365.22..377.52 rows=984 width=12)
Group Key: sc.id
Filter: (CASE WHEN (sum(sci.amount) > '0'::numeric) THEN 1 ELSE 0 END = 1)
-> Hash Right Join (cost=291.87..360.30 rows=984 width=10)
Hash Cond: (sci.contract_id = sc.id)
-> Seq Scan on tbl_sales_contract_invoices sci (cost=0.00..52.18 rows=1718 width=10)
-> Hash (cost=280.96..280.96 rows=873 width=4)
-> Hash Join (cost=221.71..280.96 rows=873 width=4)
Hash Cond: (sc.project_id = p_1.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..55.24 rows=1524 width=8)
-> Hash (cost=201.62..201.62 rows=1607 width=4)
-> Seq Scan on tbl_projects p_1 (cost=0.00..201.62 rows=1607 width=4)
Filter: (created_by = ANY ('{1001,1004,1005,1013,5087,5295,5300,5311,5329,5332,5420}'::integer[]))
-> Hash (cost=104.83..104.83 rows=1883 width=8)
-> Seq Scan on tbl_clients client (cost=0.00..104.83 rows=1883 width=8)
-> Hash (cost=1.47..1.47 rows=47 width=4)
-> Seq Scan on tbl_sales_group_relationships sgr (cost=0.00..1.47 rows=47 width=4)