QUERY PLAN
Limit (cost=2814.67..2814.72 rows=20 width=375)
-> Sort (cost=2814.67..2820.28 rows=2244 width=375)
Sort Key: (CASE WHEN (t0.id IS NOT NULL) THEN t0.remind_date ELSE sc.signed_at END) DESC
-> Hash Left Join (cost=2645.52..2754.96 rows=2244 width=375)
Hash Cond: (sc.id = r.purchase_contract_id)
-> Hash Left Join (cost=995.78..1026.38 rows=2244 width=143)
Hash Cond: (sc.id = ovsub.id)
-> Hash Left Join (cost=617.77..642.48 rows=2244 width=111)
Hash Cond: (sc.id = sci.contract_id)
-> Hash Right Join (cost=312.62..331.42 rows=2244 width=79)
Hash Cond: (t0.contract_id = sc.id)
Filter: (((t0.id IS NOT NULL) AND (t0.paid_amount <> '0'::numeric)) OR ((t0.id IS NULL) AND (sc.erp_settle_amount > '0'::numeric)))
-> HashAggregate (cost=166.58..174.89 rows=831 width=41)
Group Key: t0.id
-> Hash Join (cost=145.93..164.50 rows=831 width=32)
Hash Cond: (t0.contract_id = t1.id)
-> Seq Scan on tbl_purchase_contract_payment_collection_remind_dates t0 (cost=0.00..16.37 rows=837 width=32)
-> Hash (cost=117.72..117.72 rows=2257 width=4)
-> Seq Scan on tbl_purchase_contracts t1 (cost=0.00..117.72 rows=2257 width=4)
Filter: (NOT is_chargeback)
-> Hash (cost=117.72..117.72 rows=2266 width=51)
-> Seq Scan on tbl_purchase_contracts sc (cost=0.00..117.72 rows=2266 width=51)
Filter: ((NOT is_draft) AND (NOT is_refuse))
-> Hash (cost=276.94..276.94 rows=2257 width=36)
-> Subquery Scan on sci (cost=226.15..276.94 rows=2257 width=36)
-> HashAggregate (cost=226.15..254.37 rows=2257 width=36)
Group Key: t0_1.id
-> Hash Right Join (cost=145.93..213.39 rows=2553 width=17)
Hash Cond: (t1_1.contract_id = t0_1.id)
-> Seq Scan on tbl_purchase_contract_invoices t1_1 (cost=0.00..60.70 rows=2570 width=10)
-> Hash (cost=117.72..117.72 rows=2257 width=11)
-> Seq Scan on tbl_purchase_contracts t0_1 (cost=0.00..117.72 rows=2257 width=11)
Filter: (NOT is_chargeback)
-> Hash (cost=375.44..375.44 rows=205 width=36)
-> Subquery Scan on ovsub (cost=370.83..375.44 rows=205 width=36)
-> HashAggregate (cost=370.83..373.39 rows=205 width=36)
Group Key: t1_2.id
-> Hash Join (cost=177.18..369.81 rows=205 width=10)
Hash Cond: (p.id = t1_2.from_project)
-> Seq Scan on tbl_projects p (cost=0.00..184.28 rows=1691 width=4)
Filter: (project_status = 3)
-> Hash (cost=172.43..172.43 rows=380 width=14)
-> Hash Join (cost=37.79..172.43 rows=380 width=14)
Hash Cond: (t1_2.id = t0_2.contract_id)
-> Seq Scan on tbl_sales_contracts t1_2 (cost=0.00..118.25 rows=1679 width=8)
Filter: (need_analysis AND (from_project IS NOT NULL))
-> Hash (cost=32.91..32.91 rows=390 width=10)
-> Seq Scan on tbl_sales_contract_payment_collection_remind_dates t0_2 (cost=0.00..32.91 rows=390 width=10)
Filter: (((status = 1) AND (overdue_days IS NOT NULL)) OR ((status = 0) AND (remind_date <= '2026-06-04'::date)))
-> Hash (cost=1649.61..1649.61 rows=11 width=36)
-> GroupAggregate (cost=1649.28..1649.50 rows=11 width=36)
Group Key: r.purchase_contract_id
-> Sort (cost=1649.28..1649.31 rows=11 width=10)
Sort Key: r.purchase_contract_id
-> Nested Loop (cost=0.28..1649.09 rows=11 width=10)
-> Seq Scan on tbl_term_taxonomy_relationships dt1 (cost=0.00..1470.28 rows=36 width=4)
Filter: ((term_id = 17) AND (taxonomy_id = 2100000000))
-> Index Scan using tbl_approves_pkey on tbl_approves r (cost=0.28..4.97 rows=1 width=14)
Index Cond: (id = dt1.object_id)
Filter: (status = 2)