QUERY PLAN
Limit (cost=2893.57..2893.67 rows=40 width=560)
-> Sort (cost=2893.57..2893.72 rows=60 width=560)
Sort Key: p.bargain_date DESC, p.updated_at DESC
-> Subquery Scan on p (cost=2874.10..2891.80 rows=60 width=560)
-> GroupAggregate (cost=2874.10..2890.60 rows=60 width=532)
Group Key: p_1.id, pd.user_id, pd.pct
-> Sort (cost=2874.10..2874.25 rows=60 width=456)
Sort Key: p_1.id, pd.user_id, pd.pct
-> Nested Loop Left Join (cost=1822.58..2872.32 rows=60 width=456)
Join Filter: (p_1.id = ctlc.project_id)
-> Hash Left Join (cost=923.98..998.19 rows=60 width=448)
Hash Cond: (p_1.id = esgs.project_id)
-> Hash Left Join (cost=900.95..975.01 rows=60 width=416)
Hash Cond: (p_1.id = eccrta.project_id)
-> Hash Left Join (cost=825.86..899.76 rows=60 width=384)
Hash Cond: (p_1.id = ecctcpc.project_id)
-> Hash Left Join (cost=791.42..865.16 rows=60 width=352)
Hash Cond: (p_1.id = ecctasc.project_id)
-> Hash Left Join (cost=276.65..350.24 rows=60 width=320)
Hash Cond: (p_1.id = ecta.project_id)
-> Hash Left Join (cost=272.72..346.15 rows=60 width=288)
Hash Cond: (p_1.id = o2.project_id)
-> Hash Left Join (cost=238.57..311.84 rows=60 width=224)
Hash Cond: (p_1.id = o.project_id)
-> Hash Left Join (cost=204.42..277.53 rows=60 width=160)
Hash Cond: (p_1.id = pd.project_id)
-> Hash Right Join (cost=202.05..274.93 rows=60 width=124)
Hash Cond: (sc.from_project = p_1.id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..68.32 rows=1732 width=4)
-> Hash (cost=201.30..201.30 rows=60 width=124)
-> Seq Scan on tbl_projects p_1 (cost=0.00..201.30 rows=60 width=124)
Filter: ((bargain_date >= '2026-01-01'::date) AND (bargain_date <= '2026-12-31'::date) AND (project_status = 3))
-> Hash (cost=2.32..2.32 rows=4 width=40)
-> Subquery Scan on pd (cost=2.23..2.32 rows=4 width=40)
-> HashAggregate (cost=2.23..2.28 rows=4 width=40)
Group Key: t.project_id, r.user_id
-> Hash Right Join (cost=1.07..2.20 rows=4 width=13)
Hash Cond: (r.erp_profit_distribute_id = t.id)
-> Seq Scan on tbl_erp_profit_distribute_pcts r (cost=0.00..1.08 rows=8 width=13)
-> Hash (cost=1.05..1.05 rows=2 width=8)
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..1.05 rows=2 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=32.05..32.05 rows=168 width=68)
-> Subquery Scan on o (cost=27.85..32.05 rows=168 width=68)
-> HashAggregate (cost=27.85..30.37 rows=168 width=68)
Group Key: t_1.project_id
-> Hash Right Join (cost=9.68..23.83 rows=536 width=14)
Hash Cond: (eipss.erp_sales_outbound_id = t_1.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.65 rows=565 width=14)
-> Hash (cost=7.40..7.40 rows=182 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_1 (cost=0.00..7.40 rows=182 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=32.05..32.05 rows=168 width=68)
-> Subquery Scan on o2 (cost=27.85..32.05 rows=168 width=68)
-> HashAggregate (cost=27.85..30.37 rows=168 width=68)
Group Key: t_2.project_id
-> Hash Right Join (cost=9.68..23.83 rows=536 width=14)
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.65 rows=565 width=14)
-> Hash (cost=7.40..7.40 rows=182 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t_2 (cost=0.00..7.40 rows=182 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=3.57..3.57 rows=29 width=36)
-> Subquery Scan on ecta (cost=2.92..3.57 rows=29 width=36)
-> HashAggregate (cost=2.92..3.28 rows=29 width=36)
Group Key: t_3.project_id
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..2.74 rows=36 width=9)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=514.76..514.76 rows=1 width=36)
-> Subquery Scan on ecctasc (cost=514.72..514.76 rows=1 width=36)
-> GroupAggregate (cost=514.72..514.75 rows=1 width=36)
Group Key: sc_1.from_project
-> Sort (cost=514.72..514.73 rows=1 width=9)
Sort Key: sc_1.from_project
-> Nested Loop (cost=1.37..514.71 rows=1 width=9)
-> Hash Left Join (cost=1.09..514.39 rows=1 width=9)
Hash Cond: (t_4.id = cpc.erp_cash_cost_ticket_id)
Filter: (cpc.id IS NULL)
-> Seq Scan on tbl_erp_cash_cost_tickets t_4 (cost=0.00..470.94 rows=8465 width=13)
Filter: (status = 2)
-> Hash (cost=1.04..1.04 rows=4 width=8)
-> Seq Scan on tbl_erp_cash_cost_ticket_client_project_costs cpc (cost=0.00..1.04 rows=4 width=8)
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_1 (cost=0.28..0.32 rows=1 width=8)
Index Cond: (id = t_4.sales_contract_id)
Filter: (from_project IS NOT NULL)
-> Hash (cost=34.40..34.40 rows=3 width=36)
-> Subquery Scan on ecctcpc (cost=34.30..34.40 rows=3 width=36)
-> GroupAggregate (cost=34.30..34.37 rows=3 width=36)
Group Key: cpc_1.project_id
-> Sort (cost=34.30..34.31 rows=4 width=9)
Sort Key: cpc_1.project_id
-> Nested Loop (cost=0.29..34.26 rows=4 width=9)
-> Seq Scan on tbl_erp_cash_cost_ticket_client_project_costs cpc_1 (cost=0.00..1.04 rows=4 width=13)
Filter: (project_id IS NOT NULL)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_5 (cost=0.29..8.30 rows=1 width=4)
Index Cond: (id = cpc_1.erp_cash_cost_ticket_id)
Filter: (status = 2)
-> Hash (cost=74.97..74.97 rows=10 width=36)
-> Subquery Scan on eccrta (cost=74.67..74.97 rows=10 width=36)
-> GroupAggregate (cost=74.67..74.87 rows=10 width=36)
Group Key: sc_2.from_project
-> Sort (cost=74.67..74.70 rows=10 width=10)
Sort Key: sc_2.from_project
-> Nested Loop (cost=0.28..74.50 rows=10 width=10)
-> Seq Scan on tbl_erp_cost_convert_tickets t_6 (cost=0.00..1.14 rows=10 width=10)
Filter: (status = 2)
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc_2 (cost=0.28..6.69 rows=1 width=8)
Index Cond: (id = t_6.sales_contract_id)
Filter: (from_project IS NOT NULL)
-> Hash (cost=22.71..22.71 rows=25 width=36)
-> Subquery Scan on esgs (cost=22.15..22.71 rows=25 width=36)
-> HashAggregate (cost=22.15..22.46 rows=25 width=36)
Group Key: t_7.project_id
-> Hash Right Join (cost=6.78..20.92 rows=164 width=14)
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.65 rows=565 width=14)
-> Hash (cost=6.15..6.15 rows=50 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_7 (cost=0.00..6.15 rows=50 width=8)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Materialize (cost=898.60..1846.31 rows=31 width=12)
-> Subquery Scan on ctlc (cost=898.60..1846.15 rows=31 width=12)
-> GroupAggregate (cost=898.60..1845.84 rows=31 width=12)
Group Key: "*SELECT* 1".project_id
-> Sort (cost=898.60..898.68 rows=31 width=65)
Sort Key: "*SELECT* 1".project_id
-> Nested Loop Left Join (cost=61.97..897.83 rows=31 width=65)
Join Filter: (ps.id = psrr.punch_strategy_id)
-> Nested Loop Left Join (cost=61.97..895.62 rows=31 width=52)
Join Filter: (psrr.user_id = "*SELECT* 1".engineer_id)
-> Hash Left Join (cost=61.97..887.94 rows=31 width=52)
Hash Cond: (("*SELECT* 1".engineer_id = payroll.user_id) AND ("*SELECT* 1".settle_month = payroll.settle_month))
-> Append (cost=0.29..826.09 rows=31 width=20)
-> Subquery Scan on "*SELECT* 1" (cost=0.29..163.18 rows=5 width=20)
-> Nested Loop (cost=0.29..163.13 rows=5 width=280)
-> Seq Scan on tbl_pre_sales p_2 (cost=0.00..100.65 rows=8 width=28)
Filter: ((start_at IS NOT NULL) AND ((NOT is_draft) OR (is_draft IS NULL)) AND (end_at IS NOT NULL) AND (engineer_id IS NOT NULL) AND (start_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (start_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (end_at > start_at))
-> Index Scan using tbl_cases_pkey on tbl_cases c (cost=0.29..7.80 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..662.90 rows=26 width=20)
-> Nested Loop (cost=0.29..662.64 rows=26 width=280)
-> Seq Scan on tbl_implement_records i (cost=0.00..393.01 rows=44 width=24)
Filter: ((arrived_at IS NOT NULL) AND ((NOT is_draft) OR (is_draft IS NULL)) AND (left_at IS NOT NULL) AND (arrived_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (arrived_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (left_at > arrived_at))
-> Index Scan using tbl_cases_pkey on tbl_cases c_1 (cost=0.29..6.12 rows=1 width=16)
Index Cond: (id = i.case_id)
Filter: ((last_engineer IS NOT NULL) AND (project_id IS NOT NULL))
-> Hash (cost=58.69..58.69 rows=200 width=40)
-> Subquery Scan on payroll (cost=54.19..58.69 rows=200 width=40)
-> HashAggregate (cost=54.19..56.69 rows=200 width=40)
Group Key: am.user_id, t_8.settle_month
-> Append (cost=12.02..48.96 rows=697 width=40)
-> HashAggregate (cost=12.02..14.62 rows=208 width=40)
Group Key: am.user_id, t_8.settle_month
-> Hash Join (cost=1.34..9.95 rows=276 width=11)
Hash Cond: (am.erp_wage_ticket_id = t_8.id)
-> Seq Scan on tbl_erp_wage_ticket_amounts am (cost=0.00..7.45 rows=345 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.19..1.19 rows=12 width=8)
-> Seq Scan on tbl_erp_wage_tickets t_8 (cost=0.00..1.19 rows=12 width=8)
Filter: (status = 2)
-> HashAggregate (cost=9.56..12.33 rows=222 width=40)
Group Key: am_1.user_id, t_9.settle_month
-> Hash Join (cost=1.27..7.89 rows=222 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..5.67 rows=267 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.15..1.15 rows=10 width=8)
-> Seq Scan on tbl_erp_social_insurances t_9 (cost=0.00..1.15 rows=10 width=8)
Filter: (status = 2)
-> HashAggregate (cost=7.54..9.82 rows=182 width=40)
Group Key: am_2.user_id, t_10.settle_month
-> Hash Join (cost=1.21..6.13 rows=188 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..4.11 rows=211 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.11..1.11 rows=8 width=8)
-> Seq Scan on tbl_erp_provident_funds t_10 (cost=0.00..1.11 rows=8 width=8)
Filter: (status = 2)
-> HashAggregate (cost=4.16..5.22 rows=85 width=40)
Group Key: am_3.user_id, t_11.settle_month
-> Hash Join (cost=1.24..3.52 rows=85 width=11)
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.94 rows=94 width=11)
Filter: (user_id IS NOT NULL)
-> Hash (cost=1.12..1.12 rows=9 width=8)
-> Seq Scan on tbl_erp_personal_taxs t_11 (cost=0.00..1.12 rows=9 width=8)
Filter: (status = 2)
-> Materialize (cost=0.00..1.21 rows=14 width=8)
-> Seq Scan on tbl_punch_strategy_resource_rels psrr (cost=0.00..1.14 rows=14 width=8)
-> Materialize (cost=0.00..1.04 rows=3 width=17)
-> Seq Scan on tbl_punch_strategies ps (cost=0.00..1.03 rows=3 width=17)
SubPlan 1
-> Aggregate (cost=30.50..30.51 rows=1 width=8)
-> Hash Left Join (cost=5.78..29.25 rows=500 width=0)
Hash Cond: ((work_dates.work_date)::date = lh.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=1.50..22.00 rows=1000 width=13)
Hash Cond: ((work_dates.work_date)::date = pss.edate)
-> Function Scan on generate_series work_dates (cost=0.01..10.01 rows=1000 width=8)
-> Hash (cost=1.36..1.36 rows=10 width=9)
-> Seq Scan on tbl_punch_strategy_schedules pss (cost=0.00..1.36 rows=10 width=9)
Filter: (punch_strategy_id = ps.id)
-> 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)