QUERY PLAN
Aggregate (cost=573.22..573.23 rows=1 width=8)
-> Sort (cost=547.67..551.93 rows=1703 width=870)
Sort Key: p.erp_profit_amount, p.updated_at DESC
-> Subquery Scan on p (cost=409.44..456.28 rows=1703 width=870)
-> HashAggregate (cost=409.44..439.25 rows=1703 width=842)
Group Key: p_1.id, pd.user_id, pd.pct
-> Hash Left Join (cost=254.49..388.16 rows=1703 width=120)
Hash Cond: (p_1.id = o.project_id)
-> Hash Left Join (cost=220.95..350.14 rows=1703 width=56)
Hash Cond: (p_1.id = pd.project_id)
-> Hash Right Join (cost=206.84..329.63 rows=1703 width=20)
Hash Cond: (sc.from_project = p_1.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1725 width=4)
-> Hash (cost=185.55..185.55 rows=1703 width=20)
-> Seq Scan on tbl_projects p_1 (cost=0.00..185.55 rows=1703 width=20)
Filter: (project_status = 3)
-> Hash (cost=14.10..14.10 rows=1 width=40)
-> Subquery Scan on pd (cost=14.06..14.10 rows=1 width=40)
-> GroupAggregate (cost=14.06..14.09 rows=1 width=40)
Group Key: t.project_id, r.user_id
-> Sort (cost=14.06..14.07 rows=1 width=13)
Sort Key: t.project_id, r.user_id
-> Nested Loop Left Join (cost=0.00..14.05 rows=1 width=13)
Join Filter: (t.id = r.erp_profit_distribute_id)
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..12.88 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..1.08 rows=8 width=13)
-> Hash (cost=31.88..31.88 rows=133 width=68)
-> Subquery Scan on o (cost=28.55..31.88 rows=133 width=68)
-> HashAggregate (cost=28.55..30.55 rows=133 width=68)
Group Key: t_1.project_id
-> Hash Right Join (cost=10.69..24.67 rows=518 width=13)
Hash Cond: (eipss.erp_sales_outbound_id = t_1.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.51 rows=551 width=13)
-> Hash (cost=8.90..8.90 rows=143 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_1 (cost=0.00..8.90 rows=143 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))