QUERY PLAN
Aggregate (cost=399.69..399.70 rows=1 width=96)
-> Hash Left Join (cost=299.88..382.62 rows=2276 width=44)
Hash Cond: (pc.id = pci.contract_id)
-> Hash Left Join (cost=150.42..227.17 rows=2276 width=16)
Hash Cond: (pc.id = pcp.contract_id)
-> Seq Scan on tbl_purchase_contracts pc (cost=0.00..70.76 rows=2276 width=14)
-> Hash (cost=121.97..121.97 rows=2276 width=10)
-> Subquery Scan on pcp (cost=76.45..121.97 rows=2276 width=10)
-> HashAggregate (cost=76.45..99.21 rows=2276 width=10)
Group Key: tbl_purchase_contracts.id
-> Seq Scan on tbl_purchase_contracts (cost=0.00..70.76 rows=2276 width=10)
-> Hash (cost=122.38..122.38 rows=2167 width=36)
-> Subquery Scan on pci (cost=73.62..122.38 rows=2167 width=36)
-> HashAggregate (cost=73.62..100.71 rows=2167 width=36)
Group Key: tbl_purchase_contract_invoices.contract_id
-> Seq Scan on tbl_purchase_contract_invoices (cost=0.00..60.75 rows=2574 width=10)
Filter: ((red_invoice_id IS NOT NULL) OR (NOT is_cancel))