QUERY PLAN
Limit (cost=2232.30..2232.30 rows=1 width=560)
-> Sort (cost=2232.30..2232.30 rows=1 width=560)
Sort Key: p.bargain_date DESC, p.updated_at DESC
-> Subquery Scan on p (cost=2231.99..2232.29 rows=1 width=560)
-> GroupAggregate (cost=2231.99..2232.27 rows=1 width=532)
Group Key: p_1.id, r.user_id, (sum(r.pct))
-> Sort (cost=2231.99..2232.00 rows=1 width=456)
Sort Key: p_1.id, r.user_id, (sum(r.pct))
-> Nested Loop Left Join (cost=2038.28..2231.98 rows=1 width=456)
Join Filter: (p_1.id = "*SELECT* 1".project_id)
-> Nested Loop Left Join (cost=1477.35..1621.02 rows=1 width=448)
Join Filter: (p_1.id = t_7.project_id)
-> Nested Loop Left Join (cost=1454.19..1597.27 rows=1 width=416)
Join Filter: (p_1.id = sc_2.from_project)
-> Nested Loop Left Join (cost=1444.75..1587.78 rows=1 width=384)
Join Filter: (p_1.id = cpc_1.project_id)
-> Nested Loop Left Join (cost=865.34..1001.37 rows=1 width=352)
Join Filter: (p_1.id = sc_1.from_project)
-> Nested Loop Left Join (cost=275.44..409.69 rows=1 width=320)
Join Filter: (p_1.id = t_3.project_id)
-> Nested Loop Left Join (cost=271.16..403.98 rows=1 width=288)
Join Filter: (p_1.id = t_2.project_id)
-> Nested Loop Left Join (cost=242.61..370.44 rows=1 width=224)
Join Filter: (p_1.id = t_1.project_id)
-> Nested Loop Left Join (cost=214.06..336.90 rows=1 width=160)
Join Filter: (t.project_id = p_1.id)
-> Hash Right Join (cost=200.00..322.79 rows=1 width=124)
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=199.99..199.99 rows=1 width=124)
-> Seq Scan on tbl_projects p_1 (cost=0.00..199.99 rows=1 width=124)
Filter: ((bargain_date >= '2026-12-01'::date) AND (bargain_date <= '2026-12-31'::date) AND (project_status = 3))
-> 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)
-> 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))
-> HashAggregate (cost=28.55..30.55 rows=133 width=68)
Group Key: t_2.project_id
-> Hash Right Join (cost=10.69..24.67 rows=518 width=13)
Hash Cond: (eipss_1.erp_sales_outbound_id = t_2.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_1 (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_2 (cost=0.00..8.90 rows=143 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> HashAggregate (cost=4.28..4.79 rows=41 width=36)
Group Key: t_3.project_id
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..4.03 rows=50 width=9)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> GroupAggregate (cost=589.90..590.74 rows=42 width=36)
Group Key: sc_1.from_project
-> Sort (cost=589.90..590.00 rows=42 width=9)
Sort Key: sc_1.from_project
-> Nested Loop (cost=550.88..588.76 rows=42 width=9)
-> Hash Right Join (cost=550.60..574.11 rows=42 width=9)
Hash Cond: (cpc.erp_cash_cost_ticket_id = t_4.id)
Filter: (cpc.id IS NULL)
-> Seq Scan on tbl_erp_cash_cost_ticket_client_project_costs cpc (cost=0.00..20.70 rows=1070 width=8)
-> Hash (cost=444.86..444.86 rows=8459 width=13)
-> Seq Scan on tbl_erp_cash_cost_tickets t_4 (cost=0.00..444.86 rows=8459 width=13)
Filter: (status = 2)
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_1 (cost=0.28..0.35 rows=1 width=8)
Index Cond: (id = t_4.sales_contract_id)
Filter: (from_project IS NOT NULL)
-> HashAggregate (cost=579.42..581.92 rows=200 width=36)
Group Key: cpc_1.project_id
-> Hash Join (cost=550.60..574.10 rows=1064 width=36)
Hash Cond: (cpc_1.erp_cash_cost_ticket_id = t_5.id)
-> Seq Scan on tbl_erp_cash_cost_ticket_client_project_costs cpc_1 (cost=0.00..20.70 rows=1065 width=40)
Filter: (project_id IS NOT NULL)
-> Hash (cost=444.86..444.86 rows=8459 width=4)
-> Seq Scan on tbl_erp_cash_cost_tickets t_5 (cost=0.00..444.86 rows=8459 width=4)
Filter: (status = 2)
-> GroupAggregate (cost=9.44..9.46 rows=1 width=36)
Group Key: sc_2.from_project
-> Sort (cost=9.44..9.44 rows=1 width=36)
Sort Key: sc_2.from_project
-> Nested Loop (cost=0.28..9.43 rows=1 width=36)
-> Seq Scan on tbl_erp_cost_convert_tickets t_6 (cost=0.00..1.12 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_6.sales_contract_id)
Filter: (from_project IS NOT NULL)
-> HashAggregate (cost=23.16..23.38 rows=17 width=36)
Group Key: t_7.project_id
-> Hash Right Join (cost=7.99..21.95 rows=161 width=13)
Hash Cond: (eipss_2.erp_sales_gift_id = t_7.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_2 (cost=0.00..12.51 rows=551 width=13)
-> Hash (cost=7.54..7.54 rows=36 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_7 (cost=0.00..7.54 rows=36 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> GroupAggregate (cost=560.92..610.91 rows=2 width=12)
Group Key: "*SELECT* 1".project_id
-> Sort (cost=560.92..560.93 rows=2 width=65)
Sort Key: "*SELECT* 1".project_id
-> Hash Right Join (cost=559.62..560.91 rows=2 width=65)
Hash Cond: (psrr.user_id = "*SELECT* 1".engineer_id)
-> Hash Left Join (cost=1.07..2.28 rows=16 width=21)
Hash Cond: (psrr.punch_strategy_id = ps.id)
-> Seq Scan on tbl_punch_strategy_resource_rels psrr (cost=0.00..1.16 rows=16 width=8)
-> Hash (cost=1.03..1.03 rows=3 width=17)
-> Seq Scan on tbl_punch_strategies ps (cost=0.00..1.03 rows=3 width=17)
-> Hash (cost=558.53..558.53 rows=2 width=52)
-> Hash Right Join (cost=552.52..558.53 rows=2 width=52)
Hash Cond: ((am.user_id = "*SELECT* 1".engineer_id) AND (t_8.settle_month = "*SELECT* 1".settle_month))
-> HashAggregate (cost=42.32..44.82 rows=200 width=40)
Group Key: am.user_id, t_8.settle_month
-> Append (cost=12.96..40.31 rows=268 width=40)
-> HashAggregate (cost=12.96..15.56 rows=208 width=40)
Group Key: am.user_id, t_8.settle_month
-> Hash Join (cost=1.31..10.92 rows=271 width=11)
Hash Cond: (am.erp_wage_ticket_id = t_8.id)
-> Seq Scan on tbl_erp_wage_ticket_amounts am (cost=0.00..8.45 rows=345 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.18..1.18 rows=11 width=8)
-> Seq Scan on tbl_erp_wage_tickets t_8 (cost=0.00..1.18 rows=11 width=8)
Filter: (status = 2)
-> HashAggregate (cost=9.96..10.28 rows=26 width=40)
Group Key: am_1.user_id, t_9.settle_month
-> Hash Join (cost=1.14..9.75 rows=27 width=11)
Hash Cond: (am_1.erp_social_insurance_id = t_9.id)
-> Seq Scan on tbl_erp_social_insurance_amounts am_1 (cost=0.00..7.67 rows=267 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.12..1.12 rows=1 width=8)
-> Seq Scan on tbl_erp_social_insurances t_9 (cost=0.00..1.12 rows=1 width=8)
Filter: (status = 2)
-> HashAggregate (cost=8.24..8.57 rows=26 width=40)
Group Key: am_2.user_id, t_10.settle_month
-> Hash Join (cost=1.10..8.02 rows=30 width=11)
Hash Cond: (am_2.erp_provident_fund_id = t_10.id)
-> Seq Scan on tbl_erp_provident_fund_amounts am_2 (cost=0.00..6.11 rows=211 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.09..1.09 rows=1 width=8)
-> Seq Scan on tbl_erp_provident_funds t_10 (cost=0.00..1.09 rows=1 width=8)
Filter: (status = 2)
-> HashAggregate (cost=3.12..3.22 rows=8 width=40)
Group Key: am_3.user_id, t_11.settle_month
-> Hash Join (cost=1.11..3.06 rows=8 width=14)
Hash Cond: (am_3.erp_personal_tax_id = t_11.id)
-> Seq Scan on tbl_erp_personal_tax_amounts am_3 (cost=0.00..1.68 rows=68 width=14)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.10..1.10 rows=1 width=8)
-> Seq Scan on tbl_erp_personal_taxs t_11 (cost=0.00..1.10 rows=1 width=8)
Filter: (status = 2)
-> Hash (cost=510.17..510.17 rows=2 width=20)
-> Append (cost=0.29..510.17 rows=2 width=20)
-> Subquery Scan on "*SELECT* 1" (cost=0.29..108.85 rows=1 width=20)
-> Nested Loop (cost=0.29..108.84 rows=1 width=280)
-> Seq Scan on tbl_pre_sales p_2 (cost=0.00..100.53 rows=1 width=28)
Filter: ((start_at IS NOT NULL) AND (end_at IS NOT NULL) AND (engineer_id IS NOT NULL) AND ((NOT is_draft) OR (is_draft IS NULL)) AND (end_at > start_at) AND (start_at >= '2026-12-01 00:00:00+08'::timestamp with time zone) AND (start_at <= '2026-12-31 23:59:59+08'::timestamp with time zone))
-> Index Scan using tbl_cases_pkey on tbl_cases c (cost=0.29..8.30 rows=1 width=12)
Index Cond: (id = p_2.case_id)
Filter: (project_id IS NOT NULL)
-> Subquery Scan on "*SELECT* 2" (cost=0.29..401.32 rows=1 width=20)
-> Nested Loop (cost=0.29..401.31 rows=1 width=280)
-> Seq Scan on tbl_implement_records i (cost=0.00..392.99 rows=1 width=24)
Filter: ((arrived_at IS NOT NULL) AND (left_at IS NOT NULL) AND ((NOT is_draft) OR (is_draft IS NULL)) AND (left_at > arrived_at) AND (arrived_at >= '2026-12-01 00:00:00+08'::timestamp with time zone) AND (arrived_at <= '2026-12-31 23:59:59+08'::timestamp with time zone))
-> Index Scan using tbl_cases_pkey on tbl_cases c_1 (cost=0.29..8.30 rows=1 width=16)
Index Cond: (id = i.case_id)
Filter: ((last_engineer IS NOT NULL) AND (project_id IS NOT NULL))
SubPlan 1
-> Aggregate (cost=24.94..24.95 rows=1 width=8)
-> Hash Left Join (cost=5.67..23.69 rows=500 width=0)
Hash Cond: ((work_dates.work_date)::date = pss.edate)
Filter: CASE WHEN (ps.id IS NULL) THEN (((date_part('isodow'::text, work_dates.work_date))::integer >= 1) AND ((date_part('isodow'::text, work_dates.work_date))::integer <= 5)) WHEN (pss.id IS NOT NULL) THEN pss.need_work WHEN (ps.legal_holiday AND (lh.id IS NOT NULL)) THEN lh.is_exchange ELSE (((ps.week)::integer & (1 << ((date_part('isodow'::text, work_dates.work_date))::integer - 1))) <> 0) END
-> Hash Left Join (cost=4.29..17.10 rows=1000 width=13)
Hash Cond: ((work_dates.work_date)::date = lh.edate)
-> Function Scan on generate_series work_dates (cost=0.01..10.01 rows=1000 width=8)
-> Hash (cost=2.46..2.46 rows=146 width=9)
-> Seq Scan on tbl_legal_holidays lh (cost=0.00..2.46 rows=146 width=9)
-> Hash (cost=1.36..1.36 rows=1 width=9)
-> Seq Scan on tbl_punch_strategy_schedules pss (cost=0.00..1.36 rows=1 width=9)
Filter: (punch_strategy_id = ps.id)