QUERY PLAN
Aggregate (cost=525.70..525.71 rows=1 width=128)
-> Sort (cost=474.70..480.37 rows=2266 width=663)
Sort Key: (CASE WHEN (t0.id IS NOT NULL) THEN t0.remind_date ELSE sc.signed_at END) DESC
-> Hash Right Join (cost=312.62..348.42 rows=2266 width=663)
Hash Cond: (t0.contract_id = sc.id)
-> HashAggregate (cost=166.58..174.89 rows=831 width=41)
Group Key: t0.id
-> Hash Join (cost=145.93..164.50 rows=831 width=24)
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=24)
-> 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=28)
-> Seq Scan on tbl_purchase_contracts sc (cost=0.00..117.72 rows=2266 width=28)
Filter: ((NOT is_draft) AND (NOT is_refuse))