QUERY PLAN
Limit (cost=1222.60..1222.62 rows=9 width=220)
-> Sort (cost=1222.60..1222.62 rows=9 width=220)
Sort Key: r.created_at DESC
-> Append (cost=13.46..1222.45 rows=9 width=220)
-> Nested Loop Left Join (cost=13.46..358.98 rows=1 width=155)
Filter: ((NOT (hashed SubPlan 1)) OR (r.sales_contract_id IS NULL))
-> Nested Loop (cost=0.29..344.41 rows=1 width=107)
-> Seq Scan on tbl_erp_cash_cost_ticket_handlers am (cost=0.00..156.86 rows=24 width=13)
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=98)
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_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))
-> Nested Loop (cost=12.91..664.58 rows=1 width=182)
Join Filter: (pd.id = am_1.erp_profit_distribute_id)
-> Nested Loop (cost=12.91..663.43 rows=1 width=107)
Join Filter: (sc_1.from_project = pd.project_id)
-> Nested Loop (cost=12.91..648.26 rows=1 width=111)
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=103)
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))
-> 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=13)
Filter: (user_id = 1001)
-> Nested Loop (cost=12.89..19.47 rows=1 width=193)
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=40)
Filter: (user_id = 1001)
-> Seq Scan on tbl_erp_cost_tickets r_2 (cost=12.89..17.74 rows=9 width=116)
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_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))
-> Nested Loop (cost=12.91..31.78 rows=1 width=193)
Join Filter: (pd_1.id = am_3.erp_profit_distribute_id)
-> Nested Loop (cost=12.91..30.63 rows=1 width=125)
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=121)
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=13)
Filter: (user_id = 1001)
-> Nested Loop Left Join (cost=13.17..24.01 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=212)
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=176)
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=40)
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_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))
-> Nested Loop (cost=13.19..36.47 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=212)
Join Filter: (sc_3.from_project = pd_2.project_id)
-> Nested Loop (cost=0.28..9.48 rows=1 width=212)
-> Seq Scan on tbl_erp_cost_convert_tickets r_5 (cost=0.00..1.18 rows=1 width=208)
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=13)
Filter: (user_id = 1001)
-> GroupAggregate (cost=35.80..35.94 rows=2 width=220)
Group Key: r_6.id
-> Sort (cost=35.80..35.82 rows=9 width=112)
Sort Key: r_6.id
-> Hash Right Join (cost=21.69..35.66 rows=9 width=112)
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=13)
-> Hash (cost=21.66..21.66 rows=2 width=103)
-> Seq Scan on tbl_erp_sales_gifts r_6 (cost=12.89..21.66 rows=2 width=103)
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_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* 8" (cost=51.07..51.13 rows=1 width=220)
-> GroupAggregate (cost=51.07..51.12 rows=1 width=225)
Group Key: r_7.id, am_6.user_id, am_6.pct
-> Sort (cost=51.07..51.08 rows=1 width=117)
Sort Key: r_7.id, am_6.pct
-> Hash Right Join (cost=36.48..51.06 rows=1 width=117)
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=13)
-> Hash (cost=36.47..36.47 rows=1 width=108)
-> Hash Join (cost=27.84..36.47 rows=1 width=108)
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=99)
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_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))
-> 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)