QUERY PLAN
Aggregate (cost=175267.67..175267.68 rows=1 width=32)
-> Nested Loop Left Join (cost=3.23..175267.60 rows=29 width=5)
Join Filter: (g.object_id = e.id)
Filter: ((g.success_count IS NULL) OR (g.success_count = 0))
-> Seq Scan on tbl_erp_cash_cost_tickets e (cost=0.00..175263.82 rows=29 width=9)
Filter: ((wrote_date >= '2026-03-05'::date) AND (wrote_date <= '2026-06-30'::date) AND (amount > '0'::numeric) AND (status = 2) AND ((20018 <> ALL ('{20,20018}'::integer[])) OR have_invoice OR (alternatives: SubPlan 1 or hashed SubPlan 2)))
SubPlan 1
-> Nested Loop (cost=0.29..20.61 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.56 rows=1 width=4)
Index Cond: ((object_id = e.id) AND (term_id = 18))
Filter: (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[]))
SubPlan 2
-> Hash Join (cost=9.05..1485.05 rows=37 width=4)
Hash Cond: (r_1.taxonomy_id = tt_1.id)
-> Seq Scan on tbl_term_taxonomy_relationships r_1 (cost=0.00..1470.03 rows=2244 width=8)
Filter: ((term_id = 18) AND (object_type = 11))
-> Hash (cost=8.99..8.99 rows=5 width=4)
-> Seq Scan on tbl_term_taxonomies tt_1 (cost=0.00..8.99 rows=5 width=4)
Filter: ((serial_no)::text = ANY ('{221106,640301,640302,640303,600704}'::text[]))
-> Materialize (cost=3.23..3.27 rows=1 width=12)
-> Subquery Scan on g (cost=3.23..3.27 rows=1 width=12)
-> GroupAggregate (cost=3.23..3.26 rows=1 width=36)
Group Key: v.object_type, v.object_id, (COALESCE((v.event_type)::integer, 0)), (COALESCE(v.event_id, 0))
-> Sort (cost=3.23..3.23 rows=1 width=18)
Sort Key: v.object_id
-> Seq Scan on tbl_voucher_gen_items v (cost=0.00..3.22 rows=1 width=18)
Filter: ((ledger_id = 1) AND (object_type = 20018) AND (COALESCE(event_id, 0) = 0) AND (COALESCE((event_type)::integer, 0) = 0))