QUERY PLAN
Limit (cost=76.65..76.66 rows=1 width=165)
-> Sort (cost=76.65..76.66 rows=1 width=165)
Sort Key: i.from_date DESC, i.id DESC
-> Nested Loop Left Join (cost=3.50..76.64 rows=1 width=165)
Join Filter: (v.object_id = i.id)
Filter: (((count(*) FILTER (WHERE (v.status = 1))) IS NULL) OR ((count(*) FILTER (WHERE (v.status = 1))) = 0))
-> Nested Loop Left Join (cost=0.28..73.34 rows=1 width=43)
-> Seq Scan on tbl_sales_contract_invoices i (cost=0.00..65.04 rows=1 width=43)
Filter: ((NOT is_cancel) AND (NOT is_draft) AND ((invoice_type <> '-1'::integer) OR (invoice_type IS NULL)) AND (from_date >= '2026-06-01'::date) AND (from_date <= '2026-06-30'::date) AND (amount > '0'::numeric))
-> Index Scan using tbl_sales_contracts_pkey on tbl_sales_contracts c (cost=0.28..8.29 rows=1 width=8)
Index Cond: (id = i.contract_id)
-> GroupAggregate (cost=3.23..3.27 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=22)
Sort Key: v.object_id
-> Seq Scan on tbl_voucher_gen_items v (cost=0.00..3.22 rows=1 width=22)
Filter: ((ledger_id = 1) AND (object_type = 107) AND (COALESCE(event_id, 0) = 0) AND (COALESCE((event_type)::integer, 0) = 0))