QUERY PLAN
Limit (cost=4770.10..4770.20 rows=40 width=936)
-> Sort (cost=4770.10..4775.13 rows=2013 width=936)
Sort Key: c.updated_at DESC
-> Subquery Scan on c (cost=2843.87..4706.47 rows=2013 width=936)
-> GroupAggregate (cost=2843.87..4646.08 rows=2013 width=840)
Group Key: c_1.id
-> Merge Left Join (cost=2843.87..4167.99 rows=2013 width=936)
Merge Cond: (c_1.id = t_15.client_id)
-> Merge Left Join (cost=2715.87..4033.03 rows=2013 width=872)
Merge Cond: (c_1.id = "*SELECT* 1".client_id)
-> Merge Left Join (cost=1845.18..1906.51 rows=2013 width=864)
Merge Cond: (c_1.id = esgs.client_id)
-> Merge Left Join (cost=1816.32..1871.93 rows=2013 width=832)
Merge Cond: (c_1.id = t_9.client_id)
-> Merge Left Join (cost=1701.16..1751.70 rows=2013 width=800)
Merge Cond: (c_1.id = t_8.client_id)
-> Merge Left Join (cost=1699.98..1745.44 rows=2013 width=768)
Merge Cond: (c_1.id = ectt.client_id)
-> Merge Left Join (cost=1590.53..1630.15 rows=2013 width=736)
Merge Cond: (c_1.id = cpc_1.client_id)
-> Merge Left Join (cost=1074.49..1107.29 rows=2013 width=704)
Merge Cond: (c_1.id = t_5.client_id)
-> Merge Left Join (cost=1071.78..1099.51 rows=2013 width=672)
Merge Cond: (c_1.id = c_2.client_id)
-> Merge Left Join (cost=971.30..993.61 rows=2013 width=640)
Merge Cond: (c_1.id = t_3.client_id)
-> Merge Left Join (cost=966.86..984.09 rows=2013 width=608)
Merge Cond: (c_1.id = t_2.client_id)
-> Merge Left Join (cost=765.35..775.46 rows=2013 width=600)
Merge Cond: (c_1.id = t_1.client_id)
-> Sort (cost=252.15..257.19 rows=2013 width=568)
Sort Key: c_1.id
-> Hash Left Join (cost=26.26..141.69 rows=2013 width=568)
Hash Cond: (c_1.id = o.client_id)
-> Seq Scan on tbl_clients c_1 (cost=0.00..110.13 rows=2013 width=504)
-> Hash (cost=25.86..25.86 rows=32 width=68)
-> Subquery Scan on o (cost=25.06..25.86 rows=32 width=68)
-> HashAggregate (cost=25.06..25.54 rows=32 width=68)
Group Key: t.client_id
-> Hash Right Join (cost=10.11..24.09 rows=130 width=13)
Hash Cond: (eipss.erp_sales_outbound_id = t.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.51 rows=551 width=13)
-> Hash (cost=9.66..9.66 rows=36 width=8)
-> Seq Scan on tbl_erp_sales_outbounds t (cost=0.00..9.66 rows=36 width=8)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> GroupAggregate (cost=513.20..513.22 rows=1 width=36)
Group Key: t_1.client_id
-> Sort (cost=513.20..513.21 rows=1 width=9)
Sort Key: t_1.client_id
-> Hash Right Join (cost=489.68..513.19 rows=1 width=9)
Hash Cond: (cpc.erp_cash_cost_ticket_id = t_1.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=487.21..487.21 rows=198 width=13)
-> Seq Scan on tbl_erp_cash_cost_tickets t_1 (cost=0.00..487.21 rows=198 width=13)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> GroupAggregate (cost=201.50..202.42 rows=52 width=12)
Group Key: t_2.client_id
-> Sort (cost=201.50..201.64 rows=53 width=8)
Sort Key: t_2.client_id
-> Seq Scan on tbl_projects t_2 (cost=0.00..199.99 rows=53 width=8)
Filter: ((client_id IS NOT NULL) AND (bargain_date >= '2026-01-01'::date) AND (bargain_date <= '2026-12-31'::date) AND (project_status = 3))
-> GroupAggregate (cost=4.45..4.47 rows=1 width=36)
Group Key: t_3.client_id
-> Sort (cost=4.45..4.45 rows=1 width=9)
Sort Key: t_3.client_id
-> Seq Scan on tbl_erp_cost_tickets t_3 (cost=0.00..4.44 rows=1 width=9)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> GroupAggregate (cost=100.48..100.66 rows=9 width=36)
Group Key: c_2.client_id
-> Sort (cost=100.48..100.50 rows=9 width=9)
Sort Key: c_2.client_id
-> Nested Loop (cost=0.28..100.34 rows=9 width=9)
-> Seq Scan on tbl_erp_cost_tickets t_4 (cost=0.00..4.44 rows=13 width=9)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_contacts_pkey on tbl_contacts c_2 (cost=0.28..7.38 rows=1 width=8)
Index Cond: (id = t_4.contact_id)
Filter: (client_id IS NOT NULL)
-> GroupAggregate (cost=2.71..2.73 rows=1 width=36)
Group Key: t_5.client_id
-> Sort (cost=2.71..2.71 rows=1 width=10)
Sort Key: t_5.client_id
-> Seq Scan on tbl_erp_other_income_tickets t_5 (cost=0.00..2.70 rows=1 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> GroupAggregate (cost=516.04..516.88 rows=42 width=36)
Group Key: cpc_1.client_id
-> Sort (cost=516.04..516.14 rows=42 width=36)
Sort Key: cpc_1.client_id
-> Hash Join (cost=491.41..514.90 rows=42 width=36)
Hash Cond: (cpc_1.erp_cash_cost_ticket_id = t_6.id)
-> Seq Scan on tbl_erp_cash_cost_ticket_client_project_costs cpc_1 (cost=0.00..20.70 rows=1065 width=40)
Filter: (client_id IS NOT NULL)
-> Hash (cost=487.21..487.21 rows=336 width=4)
-> Seq Scan on tbl_erp_cash_cost_tickets t_6 (cost=0.00..487.21 rows=336 width=4)
Filter: ((created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Sort (cost=109.45..109.58 rows=54 width=36)
Sort Key: ectt.client_id
-> Subquery Scan on ectt (cost=106.68..107.90 rows=54 width=36)
-> HashAggregate (cost=106.68..107.36 rows=54 width=36)
Group Key: t_7.client_id
-> Seq Scan on tbl_erp_collect_tickets t_7 (cost=0.00..106.40 rows=57 width=10)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> GroupAggregate (cost=1.19..1.21 rows=1 width=36)
Group Key: t_8.client_id
-> Sort (cost=1.19..1.19 rows=1 width=36)
Sort Key: t_8.client_id
-> Seq Scan on tbl_erp_cost_convert_tickets t_8 (cost=0.00..1.18 rows=1 width=36)
Filter: ((client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> GroupAggregate (cost=115.15..115.18 rows=1 width=36)
Group Key: t_9.client_id
-> Sort (cost=115.15..115.16 rows=1 width=10)
Sort Key: t_9.client_id
-> Seq Scan on tbl_erp_payment_tickets t_9 (cost=0.00..115.14 rows=1 width=10)
Filter: (is_payment_settle AND (client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone) AND (status = 2))
-> Sort (cost=28.87..28.98 rows=45 width=36)
Sort Key: esgs.client_id
-> Subquery Scan on esgs (cost=26.62..27.63 rows=45 width=36)
-> HashAggregate (cost=26.62..27.18 rows=45 width=36)
Group Key: t_10.client_id
-> Hash Right Join (cost=8.93..22.89 rows=497 width=13)
Hash Cond: (eipss_1.erp_sales_gift_id = t_10.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_1 (cost=0.00..12.51 rows=551 width=13)
-> Hash (cost=7.54..7.54 rows=111 width=8)
-> Seq Scan on tbl_erp_sales_gifts t_10 (cost=0.00..7.54 rows=111 width=8)
Filter: ((client_id IS NOT NULL) AND (status = 2))
-> GroupAggregate (cost=870.68..2120.37 rows=50 width=12)
Group Key: "*SELECT* 1".client_id
-> Sort (cost=870.68..870.81 rows=50 width=65)
Sort Key: "*SELECT* 1".client_id
-> Nested Loop Left Join (cost=51.46..869.27 rows=50 width=65)
Join Filter: (ps.id = psrr.punch_strategy_id)
-> Hash Left Join (cost=51.46..866.05 rows=50 width=52)
Hash Cond: ("*SELECT* 1".engineer_id = psrr.user_id)
-> Hash Left Join (cost=50.10..864.34 rows=50 width=52)
Hash Cond: (("*SELECT* 1".engineer_id = payroll.user_id) AND ("*SELECT* 1".settle_month = payroll.settle_month))
-> Append (cost=0.29..814.26 rows=50 width=20)
-> Subquery Scan on "*SELECT* 1" (cost=0.29..150.53 rows=6 width=20)
-> Nested Loop Left Join (cost=0.29..150.47 rows=6 width=280)
Filter: (COALESCE(p.client_id, c_3.client_id) IS NOT NULL)
-> Seq Scan on tbl_pre_sales p (cost=0.00..100.53 rows=6 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-01-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_3 (cost=0.29..8.30 rows=1 width=8)
Index Cond: (id = p.case_id)
-> Subquery Scan on "*SELECT* 2" (cost=0.29..663.73 rows=44 width=20)
-> Nested Loop (cost=0.29..663.29 rows=44 width=280)
-> Seq Scan on tbl_implement_records i (cost=0.00..392.99 rows=44 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-01-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_4 (cost=0.29..6.12 rows=1 width=12)
Index Cond: (id = i.case_id)
Filter: ((last_engineer IS NOT NULL) AND (COALESCE(i.client_id, client_id) IS NOT NULL))
-> Hash (cost=46.82..46.82 rows=200 width=40)
-> Subquery Scan on payroll (cost=42.32..46.82 rows=200 width=40)
-> HashAggregate (cost=42.32..44.82 rows=200 width=40)
Group Key: am.user_id, t_11.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_11.settle_month
-> Hash Join (cost=1.31..10.92 rows=271 width=11)
Hash Cond: (am.erp_wage_ticket_id = t_11.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_11 (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_12.settle_month
-> Hash Join (cost=1.14..9.75 rows=27 width=11)
Hash Cond: (am_1.erp_social_insurance_id = t_12.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_12 (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_13.settle_month
-> Hash Join (cost=1.10..8.02 rows=30 width=11)
Hash Cond: (am_2.erp_provident_fund_id = t_13.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_13 (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_14.settle_month
-> Hash Join (cost=1.11..3.06 rows=8 width=14)
Hash Cond: (am_3.erp_personal_tax_id = t_14.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_14 (cost=0.00..1.10 rows=1 width=8)
Filter: (status = 2)
-> Hash (cost=1.16..1.16 rows=16 width=8)
-> Seq Scan on tbl_punch_strategy_resource_rels psrr (cost=0.00..1.16 rows=16 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=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)
-> GroupAggregate (cost=128.01..129.03 rows=40 width=68)
Group Key: t_15.client_id
-> Sort (cost=128.01..128.11 rows=42 width=13)
Sort Key: t_15.client_id
-> Seq Scan on tbl_sales_contracts t_15 (cost=0.00..126.88 rows=42 width=13)
Filter: (build_by_project AND (client_id IS NOT NULL) AND (created_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (created_at <= '2026-12-31 23:59:59+08'::timestamp with time zone))