QUERY PLAN
Limit (cost=851.99..852.09 rows=40 width=623)
-> Sort (cost=851.99..852.26 rows=107 width=623)
Sort Key: p.bargain_date DESC, p.updated_at DESC
-> GroupAggregate (cost=833.36..848.61 rows=107 width=623)
Group Key: p.id, pd.user_id, pd.pct
-> Sort (cost=833.36..833.63 rows=107 width=583)
Sort Key: p.id, pd.user_id, pd.pct
-> Nested Loop Left Join (cost=782.13..829.76 rows=107 width=583)
Join Filter: (p.id = esgs.project_id)
-> Nested Loop Left Join (cost=779.04..825.04 rows=107 width=551)
Join Filter: (p.id = eccrta.project_id)
-> Nested Loop Left Join (cost=769.71..814.07 rows=107 width=519)
Join Filter: (p.id = ecta.project_id)
-> Nested Loop Left Join (cost=768.58..811.31 rows=107 width=487)
Join Filter: (p.id = o.project_id)
-> Hash Left Join (cost=765.44..806.54 rows=107 width=423)
Hash Cond: (p.id = pd.project_id)
-> Hash Right Join (cost=732.39..773.08 rows=107 width=387)
Hash Cond: (sc.from_project = p.id)
-> HashAggregate (cost=541.11..561.35 rows=1619 width=36)
Group Key: sc.from_project
-> Hash Join (cost=84.26..504.49 rows=7325 width=9)
Hash Cond: (t.sales_contract_id = sc.id)
-> Seq Scan on tbl_erp_cash_cost_tickets t (cost=0.00..400.68 rows=7445 width=9)
Filter: (status = 2)
-> Hash (cost=63.71..63.71 rows=1644 width=8)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..63.71 rows=1644 width=8)
Filter: (from_project IS NOT NULL)
-> Hash (cost=189.94..189.94 rows=107 width=355)
-> Seq Scan on tbl_projects p (cost=0.00..189.94 rows=107 width=355)
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))
-> Materialize (cost=3.14..3.17 rows=1 width=68)
-> Subquery Scan on o (cost=3.14..3.16 rows=1 width=68)
-> HashAggregate (cost=3.14..3.15 rows=1 width=68)
Group Key: t_2.project_id
-> Hash Right Join (cost=1.22..3.11 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..1.68 rows=68 width=13)
-> Hash (cost=1.21..1.21 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_2 (cost=0.00..1.21 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Materialize (cost=1.13..1.16 rows=1 width=36)
-> Subquery Scan on ecta (cost=1.13..1.15 rows=1 width=36)
-> HashAggregate (cost=1.13..1.14 rows=1 width=36)
Group Key: t_3.project_id
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..1.12 rows=1 width=36)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Materialize (cost=9.33..9.36 rows=1 width=36)
-> Subquery Scan on eccrta (cost=9.33..9.36 rows=1 width=36)
-> GroupAggregate (cost=9.33..9.35 rows=1 width=36)
Group Key: sc_1.from_project
-> Sort (cost=9.33..9.33 rows=1 width=36)
Sort Key: sc_1.from_project
-> Nested Loop (cost=0.28..9.32 rows=1 width=36)
-> Seq Scan on tbl_erp_cost_convert_tickets t_4 (cost=0.00..1.01 rows=1 width=36)
Filter: (status = 2)
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_1 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = t_4.sales_contract_id)
Filter: (from_project IS NOT NULL)
-> Materialize (cost=3.09..3.12 rows=1 width=36)
-> Subquery Scan on esgs (cost=3.09..3.11 rows=1 width=36)
-> HashAggregate (cost=3.09..3.10 rows=1 width=36)
Group Key: t_5.project_id
-> Hash Right Join (cost=1.16..3.04 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..1.68 rows=68 width=13)
-> Hash (cost=1.15..1.15 rows=1 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_5 (cost=0.00..1.15 rows=1 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))