QUERY PLAN
Aggregate (cost=1312.10..1312.11 rows=1 width=8)
-> Sort (cost=1311.91..1311.94 rows=13 width=301)
Sort Key: "*SELECT* 1".created_at DESC
-> Result (cost=13.46..1311.67 rows=13 width=301)
-> Append (cost=13.46..1311.54 rows=13 width=8)
-> Subquery Scan on "*SELECT* 1" (cost=13.46..358.99 rows=1 width=8)
-> Nested Loop Left Join (cost=13.46..358.98 rows=1 width=301)
Filter: ((NOT (hashed SubPlan 1)) OR (r.sales_contract_id IS NULL))
-> Nested Loop (cost=0.29..344.41 rows=1 width=12)
-> Seq Scan on tbl_erp_cash_cost_ticket_handlers am (cost=0.00..156.86 rows=24 width=4)
Filter: (user_id = 1001)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets r (cost=0.29..7.81 rows=1 width=16)
Index Cond: (id = am.erp_cash_cost_ticket_id)
Filter: (is_payment_settle 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 (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts sc (cost=0.28..1.66 rows=1 width=8)
Index Cond: (id = r.sales_contract_id)
SubPlan 1
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_4.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_4.project_id
-> Seq Scan on tbl_erp_profit_distributes t_4 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Subquery Scan on "*SELECT* 2" (cost=12.91..664.58 rows=1 width=8)
-> Nested Loop (cost=12.91..664.57 rows=1 width=301)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=12.91..663.43 rows=1 width=12)
Join Filter: (sc_1.from_project = pd.project_id)
-> Nested Loop (cost=12.91..648.26 rows=1 width=16)
Join Filter: (sc_1.id = r_1.sales_contract_id)
-> Hash Join (cost=12.91..135.70 rows=1 width=12)
Hash Cond: (sc_1.from_project = t.project_id)
-> Seq Scan on tbl_sales_contracts sc_1 (cost=0.00..118.25 rows=1725 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t.project_id
-> Seq Scan on tbl_erp_profit_distributes t (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_cash_cost_tickets r_1 (cost=0.00..508.38 rows=334 width=12)
Filter: (is_payment_settle AND (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) 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))
-> Seq Scan on tbl_erp_profit_distributes pd (cost=0.00..12.30 rows=230 width=8)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_1 (cost=0.00..1.10 rows=3 width=4)
Filter: (user_id = 1001)
-> Subquery Scan on "*SELECT* 3" (cost=12.89..19.48 rows=1 width=8)
-> Nested Loop (cost=12.89..19.47 rows=1 width=301)
Join Filter: (r_2.id = am_2.erp_cost_ticket_id)
-> Seq Scan on tbl_erp_cost_ticket_handlers am_2 (cost=0.00..1.61 rows=1 width=4)
Filter: (user_id = 1001)
-> Seq Scan on tbl_erp_cost_tickets r_2 (cost=12.89..17.74 rows=9 width=12)
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 (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND ((NOT (hashed SubPlan 2)) OR (project_id IS NULL)) AND (status = 2))
SubPlan 2
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_5.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_5.project_id
-> Seq Scan on tbl_erp_profit_distributes t_5 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Subquery Scan on "*SELECT* 4" (cost=12.91..31.78 rows=1 width=8)
-> Nested Loop (cost=12.91..31.77 rows=1 width=301)
Join Filter: (pd_1.id = am_3.erp_profit_distribute_id)
-> Nested Loop (cost=12.91..30.63 rows=1 width=12)
Join Filter: (pd_1.project_id = r_3.project_id)
-> Hash Join (cost=12.91..25.83 rows=1 width=12)
Hash Cond: (pd_1.project_id = t_1.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_1 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_1.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_1.project_id
-> Seq Scan on tbl_erp_profit_distributes t_1 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_cost_tickets r_3 (cost=0.00..4.64 rows=13 width=12)
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 (created_at >= '2025-08-08 14:34:58+08'::timestamp with time zone) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_3 (cost=0.00..1.10 rows=3 width=4)
Filter: (user_id = 1001)
-> Subquery Scan on "*SELECT* 5" (cost=13.17..24.01 rows=1 width=8)
-> Nested Loop Left Join (cost=13.17..24.00 rows=1 width=301)
Filter: ((NOT (hashed SubPlan 3)) OR (r_4.sales_contract_id IS NULL))
-> Nested Loop (cost=0.00..2.80 rows=1 width=12)
Join Filter: (r_4.id = am_4.erp_cost_convert_ticket_id)
-> Seq Scan on tbl_erp_cost_convert_tickets r_4 (cost=0.00..1.18 rows=1 width=16)
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))
-> Seq Scan on tbl_erp_cost_convert_ticket_handlers am_4 (cost=0.00..1.61 rows=1 width=4)
Filter: (user_id = 1001)
-> 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 = r_4.sales_contract_id)
SubPlan 3
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_6.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_6.project_id
-> Seq Scan on tbl_erp_profit_distributes t_6 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Subquery Scan on "*SELECT* 6" (cost=13.19..36.47 rows=1 width=8)
-> Nested Loop (cost=13.19..36.46 rows=1 width=301)
Join Filter: (pd_2.id = am_5.erp_profit_distribute_id)
-> Nested Loop (cost=13.19..35.32 rows=1 width=12)
Join Filter: (sc_3.from_project = pd_2.project_id)
-> Nested Loop (cost=0.28..9.48 rows=1 width=12)
-> Seq Scan on tbl_erp_cost_convert_tickets r_5 (cost=0.00..1.18 rows=1 width=12)
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_sales_contracts_pkey on tbl_sales_contracts sc_3 (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = r_5.sales_contract_id)
-> Hash Join (cost=12.91..25.83 rows=1 width=12)
Hash Cond: (pd_2.project_id = t_2.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_2 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_2.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_2.project_id
-> Seq Scan on tbl_erp_profit_distributes t_2 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_5 (cost=0.00..1.10 rows=3 width=4)
Filter: (user_id = 1001)
-> Subquery Scan on "*SELECT* 7" (cost=35.80..35.88 rows=2 width=8)
-> GroupAggregate (cost=35.80..35.86 rows=2 width=301)
Group Key: r_6.id
-> Sort (cost=35.80..35.82 rows=9 width=12)
Sort Key: r_6.id
-> Hash Right Join (cost=21.69..35.66 rows=9 width=12)
Hash Cond: (eipss.erp_sales_gift_id = r_6.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss (cost=0.00..12.51 rows=551 width=4)
-> Hash (cost=21.66..21.66 rows=2 width=12)
-> Seq Scan on tbl_erp_sales_gifts r_6 (cost=12.89..21.66 rows=2 width=12)
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 ((NOT (hashed SubPlan 4)) OR (project_id IS NULL)) AND (status = 2) AND (handler = 1001))
SubPlan 4
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_7.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_7.project_id
-> Seq Scan on tbl_erp_profit_distributes t_7 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Subquery Scan on "*SELECT* 8" (cost=51.07..51.11 rows=1 width=8)
-> GroupAggregate (cost=51.07..51.10 rows=1 width=306)
Group Key: r_7.id, am_6.user_id, am_6.pct
-> Sort (cost=51.07..51.08 rows=1 width=21)
Sort Key: r_7.id, am_6.pct
-> Hash Right Join (cost=36.48..51.06 rows=1 width=21)
Hash Cond: (eipss_1.erp_sales_gift_id = r_7.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_1 (cost=0.00..12.51 rows=551 width=4)
-> Hash (cost=36.47..36.47 rows=1 width=21)
-> Hash Join (cost=27.84..36.47 rows=1 width=21)
Hash Cond: (r_7.project_id = pd_3.project_id)
-> Seq Scan on tbl_erp_sales_gifts r_7 (cost=12.89..21.36 rows=42 width=16)
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 ((NOT (hashed SubPlan 5)) OR (project_id IS NULL)) AND (status = 2))
SubPlan 5
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_8.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_8.project_id
-> Seq Scan on tbl_erp_profit_distributes t_8 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Hash (cost=14.91..14.91 rows=3 width=13)
-> Hash Join (cost=1.14..14.91 rows=3 width=13)
Hash Cond: (pd_3.id = am_6.erp_profit_distribute_id)
-> Seq Scan on tbl_erp_profit_distributes pd_3 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=1.10..1.10 rows=3 width=13)
-> Seq Scan on tbl_erp_profit_distribute_pcts am_6 (cost=0.00..1.10 rows=3 width=13)
Filter: (user_id = 1001)
-> Subquery Scan on "*SELECT* 9" (cost=37.52..37.64 rows=3 width=8)
-> GroupAggregate (cost=37.52..37.61 rows=3 width=301)
Group Key: r_8.id
-> Sort (cost=37.52..37.55 rows=11 width=12)
Sort Key: r_8.id
-> Hash Right Join (cost=23.35..37.33 rows=11 width=12)
Hash Cond: (eipss_2.erp_sales_outbound_id = r_8.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_2 (cost=0.00..12.51 rows=551 width=4)
-> Hash (cost=23.31..23.31 rows=3 width=12)
-> Seq Scan on tbl_erp_sales_outbounds r_8 (cost=12.89..23.31 rows=3 width=12)
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 ((NOT (hashed SubPlan 6)) OR (project_id IS NULL)) AND (status = 2) AND (handler = 1001))
SubPlan 6
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_9.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_9.project_id
-> Seq Scan on tbl_erp_profit_distributes t_9 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Subquery Scan on "*SELECT* 10" (cost=51.58..51.61 rows=1 width=8)
-> GroupAggregate (cost=51.58..51.60 rows=1 width=310)
Group Key: r_9.id, am_7.user_id, am_7.pct
-> Sort (cost=51.58..51.58 rows=1 width=21)
Sort Key: r_9.id, am_7.pct
-> Nested Loop (cost=35.95..51.57 rows=1 width=21)
Join Filter: (pd_4.id = am_7.erp_profit_distribute_id)
-> Hash Join (cost=35.95..50.43 rows=1 width=16)
Hash Cond: (r_9.project_id = pd_4.project_id)
-> Hash Right Join (cost=10.11..24.09 rows=130 width=16)
Hash Cond: (eipss_3.erp_sales_outbound_id = r_9.id)
-> Seq Scan on tbl_erp_inventory_product_spents eipss_3 (cost=0.00..12.51 rows=551 width=4)
-> Hash (cost=9.66..9.66 rows=36 width=16)
-> Seq Scan on tbl_erp_sales_outbounds r_9 (cost=0.00..9.66 rows=36 width=16)
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))
-> Hash (cost=25.83..25.83 rows=1 width=12)
-> Hash Join (cost=12.91..25.83 rows=1 width=12)
Hash Cond: (pd_4.project_id = t_3.project_id)
-> Seq Scan on tbl_erp_profit_distributes pd_4 (cost=0.00..12.30 rows=230 width=8)
-> Hash (cost=12.90..12.90 rows=1 width=4)
-> Group (cost=12.88..12.89 rows=1 width=4)
Group Key: t_3.project_id
-> Sort (cost=12.88..12.89 rows=1 width=4)
Sort Key: t_3.project_id
-> Seq Scan on tbl_erp_profit_distributes t_3 (cost=0.00..12.88 rows=1 width=4)
Filter: ((project_id IS NOT NULL) AND (status = 2))
-> Seq Scan on tbl_erp_profit_distribute_pcts am_7 (cost=0.00..1.10 rows=3 width=13)
Filter: (user_id = 1001)