QUERY PLAN
Limit (cost=2997.61..2997.63 rows=6 width=165)
-> Sort (cost=2997.61..2997.63 rows=6 width=165)
Sort Key: b.bill_date DESC, b.object_type, b.object_id DESC, b.event_type, b.event_id DESC
-> Merge Left Join (cost=38.24..2997.53 rows=6 width=165)
Merge Cond: ((b.object_id = g.object_id) AND (b.event_id = g.event_id))
Join Filter: ((g.object_type = b.object_type) AND (g.event_type = b.event_type))
Filter: ((g.success_count IS NULL) OR (g.success_count = 0))
-> Subquery Scan on b (cost=32.89..2991.22 rows=142 width=117)
Filter: ((b.object_type <> ALL ('{20,20018}'::integer[])) OR b.have_invoice OR (SubPlan 1))
-> GroupAggregate (cost=32.89..39.41 rows=143 width=117)
Group Key: t.id, e.id
-> Sort (cost=32.89..33.46 rows=230 width=55)
Sort Key: t.id, e.id
-> Hash Left Join (cost=12.20..23.86 rows=230 width=55)
Hash Cond: (a.user_id = u.id)
-> Hash Right Join (cost=2.96..14.00 rows=230 width=51)
Hash Cond: (a.erp_wage_ticket_id = t.id)
-> Seq Scan on tbl_erp_wage_ticket_amounts a (cost=0.00..7.45 rows=345 width=8)
-> Hash (cost=2.83..2.83 rows=10 width=47)
-> Hash Join (cost=1.35..2.83 rows=10 width=47)
Hash Cond: (e.erp_wage_ticket_id = t.id)
-> Seq Scan on tbl_erp_wage_ticket_bank_outs e (cost=0.00..1.44 rows=11 width=28)
Filter: ((settle_at IS NOT NULL) AND (amount > '0'::numeric) AND (settle_at >= '2026-01-01 00:00:00+08'::timestamp with time zone) AND (settle_at <= '2026-07-31 23:59:59+08'::timestamp with time zone) AND (amount > '0'::numeric))
-> Hash (cost=1.19..1.19 rows=13 width=23)
-> Seq Scan on tbl_erp_wage_tickets t (cost=0.00..1.19 rows=13 width=23)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=7.44..7.44 rows=144 width=8)
-> Seq Scan on tbl_users u (cost=0.00..7.44 rows=144 width=8)
SubPlan 1
-> Nested Loop (cost=0.29..20.63 rows=1 width=0)
Join Filter: (r.taxonomy_id = tt.id)
-> Index Scan using tbl_term_taxonomy_relationships_object_id_term_id_idx on tbl_term_taxonomy_relationships r (cost=0.29..11.58 rows=1 width=4)
Index Cond: ((object_id = b.object_id) AND (term_id = 18))
Filter: (((b.object_type = 20) AND (object_type = 7)) OR ((b.object_type = 20018) AND (object_type = 11)))
-> Seq Scan on tbl_term_taxonomies tt (cost=0.00..8.99 rows=5 width=4)
Filter: ((serial_no)::text = ANY ('{221106,640301,640302,640303,600704}'::text[]))
-> Sort (cost=5.36..5.42 rows=26 width=36)
Sort Key: g.object_id, g.event_id
-> Subquery Scan on g (cost=4.16..4.75 rows=26 width=36)
-> HashAggregate (cost=4.16..4.49 rows=26 width=36)
Group Key: v.object_type, v.object_id, COALESCE((v.event_type)::integer, 0), COALESCE(v.event_id, 0)
-> Seq Scan on tbl_voucher_gen_items v (cost=0.00..2.81 rows=54 width=22)
Filter: (ledger_id = 1)