QUERY PLAN
Aggregate (cost=503.79..503.80 rows=1 width=8)
-> Sort (cost=480.06..484.01 rows=1582 width=726)
Sort Key: p.erp_cost_amount, p.updated_at DESC
-> Subquery Scan on p (cost=352.49..396.00 rows=1582 width=726)
-> HashAggregate (cost=352.49..380.18 rows=1582 width=698)
Group Key: p_1.id, r.user_id, (sum(r.pct))
-> Hash Left Join (cost=313.31..336.67 rows=1582 width=88)
Hash Cond: (p_1.id = o.project_id)
-> Hash Right Join (cost=310.15..329.35 rows=1582 width=56)
Hash Cond: (t.project_id = p_1.id)
-> HashAggregate (cost=25.64..33.23 rows=607 width=40)
Group Key: t.project_id, r.user_id
-> Hash Right Join (cost=1.20..21.09 rows=607 width=40)
Hash Cond: (r.erp_profit_distribute_id = t.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..17.80 rows=780 width=40)
-> Hash (cost=1.11..1.11 rows=7 width=8)
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..1.11 rows=7 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=264.73..264.73 rows=1582 width=20)
-> Hash Right Join (cost=196.62..264.73 rows=1582 width=20)
Hash Cond: (sc.from_project = p_1.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..63.71 rows=1671 width=4)
-> Hash (cost=176.85..176.85 rows=1582 width=20)
-> Seq Scan on tbl_projects p_1 (cost=0.00..176.85 rows=1582 width=20)
Filter: (project_status = 3)
-> Hash (cost=3.15..3.15 rows=1 width=36)
-> Subquery Scan on o (cost=3.13..3.15 rows=1 width=36)
-> HashAggregate (cost=3.13..3.14 rows=1 width=68)
Group Key: t_1.project_id
-> Hash Right Join (cost=1.22..3.11 rows=4 width=9)
Hash Cond: (eipss.erp_sales_outbound_id = t_1.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..1.68 rows=68 width=9)
-> Hash (cost=1.21..1.21 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_1 (cost=0.00..1.21 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))