QUERY PLAN
Limit (cost=773.46..773.56 rows=40 width=418)
-> Sort (cost=773.46..773.75 rows=113 width=418)
Sort Key: p.bargain_date DESC, p.updated_at DESC
-> Subquery Scan on p (cost=751.53..769.89 rows=113 width=418)
-> GroupAggregate (cost=751.53..767.63 rows=113 width=390)
Group Key: p_1.id, pd.user_id, pd.pct
-> Sort (cost=751.53..751.81 rows=113 width=354)
Sort Key: p_1.id, pd.user_id, pd.pct
-> Hash Left Join (cost=705.53..747.68 rows=113 width=354)
Hash Cond: (p_1.id = esgs.project_id)
-> Hash Left Join (cost=700.79..742.64 rows=113 width=322)
Hash Cond: (p_1.id = eccrta.project_id)
-> Hash Right Join (cost=691.40..732.95 rows=113 width=290)
Hash Cond: (sc_1.from_project = p_1.id)
-> HashAggregate (cost=393.36..412.86 rows=1560 width=36)
Group Key: sc_1.from_project
-> Hash Join (cost=80.90..355.46 rows=7580 width=9)
Hash Cond: (t.sales_contract_id = sc_1.id)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..254.32 rows=7704 width=9)
Filter: (status = 2)
-> Hash (cost=61.10..61.10 rows=1584 width=8)
-> Seq Scan on tbl_sales_contracts sc_1 (cost=0.00..61.10 rows=1584 width=8)
Filter: (from_project IS NOT NULL)
-> Hash (cost=296.63..296.63 rows=113 width=258)
-> Hash Left Join (cost=230.27..296.63 rows=113 width=258)
Hash Cond: (p_1.id = ecta.project_id)
-> Hash Left Join (cost=229.07..295.13 rows=113 width=226)
Hash Cond: (p_1.id = o.project_id)
-> Hash Left Join (cost=224.21..289.98 rows=113 width=162)
Hash Cond: (p_1.id = pd.project_id)
-> Hash Right Join (cost=191.16..256.50 rows=113 width=126)
Hash Cond: (sc.from_project = p_1.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..61.10 rows=1610 width=4)
-> Hash (cost=189.75..189.75 rows=113 width=126)
-> Seq Scan on tbl_projects p_1 (cost=0.00..189.75 rows=113 width=126)
Filter: ((bargain_date >= '2025-01-01'::date) AND (bargain_date <= '2025-12-31'::date) AND (project_status = 3))
-> Hash (cost=33.01..33.01 rows=3 width=40)
-> Subquery Scan on pd (cost=32.92..33.01 rows=3 width=40)
-> GroupAggregate (cost=32.92..32.98 rows=3 width=40)
Group Key: t_1.project_id, r.user_id
-> Sort (cost=32.92..32.92 rows=3 width=40)
Sort Key: t_1.project_id, r.user_id
-> Hash Right Join (cost=13.01..32.89 rows=3 width=40)
Hash Cond: (r.erp_profit_distribute_id = t_1.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..17.80 rows=780 width=40)
-> Hash (cost=13.00..13.00 rows=1 width=8)
-> Seq Scan on tbl_erp_profit_distributes t_1 (cost=0.00..13.00 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=4.84..4.84 rows=1 width=68)
-> Subquery Scan on o (cost=4.82..4.84 rows=1 width=68)
-> HashAggregate (cost=4.82..4.83 rows=1 width=68)
Group Key: t_2.project_id
-> Hash Right Join (cost=1.36..4.79 rows=4 width=13)
Hash Cond: (eipss.erp_sales_outbound_id = t_2.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..3.11 rows=111 width=13)
-> Hash (cost=1.35..1.35 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_2 (cost=0.00..1.35 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=1.19..1.19 rows=1 width=36)
-> Subquery Scan on ecta (cost=1.17..1.19 rows=1 width=36)
-> HashAggregate (cost=1.17..1.18 rows=1 width=36)
Group Key: t_3.project_id
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..1.16 rows=1 width=36)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=9.37..9.37 rows=1 width=36)
-> Subquery Scan on eccrta (cost=9.34..9.37 rows=1 width=36)
-> GroupAggregate (cost=9.34..9.36 rows=1 width=36)
Group Key: sc_2.from_project
-> Sort (cost=9.34..9.35 rows=1 width=36)
Sort Key: sc_2.from_project
-> Nested Loop (cost=0.28..9.33 rows=1 width=36)
-> Seq Scan on tbl_erp_cost_convert_tickets t_4 (cost=0.00..1.02 rows=1 width=36)
Filter: (status = 2)
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_2 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = t_4.sales_contract_id)
Filter: (from_project IS NOT NULL)
-> Hash (cost=4.73..4.73 rows=1 width=36)
-> Subquery Scan on esgs (cost=4.71..4.73 rows=1 width=36)
-> HashAggregate (cost=4.71..4.72 rows=1 width=36)
Group Key: t_5.project_id
-> Hash Right Join (cost=1.25..4.66 rows=6 width=13)
Hash Cond: (eipss_1.erp_sales_gift_id = t_5.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_1 (cost=0.00..3.11 rows=111 width=13)
-> Hash (cost=1.24..1.24 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_5 (cost=0.00..1.24 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))