QUERY PLAN
Aggregate (cost=493.24..493.25 rows=1 width=96)
-> Hash Left Join (cost=346.53..476.20 rows=2272 width=44)
Hash Cond: (pc.id = pci.contract_id)
-> Hash Left Join (cost=197.24..320.94 rows=2272 width=16)
Hash Cond: (pc.id = pcp.contract_id)
-> Seq Scan on tbl_purchase_contracts pc (cost=0.00..117.72 rows=2272 width=14)
-> Hash (cost=168.84..168.84 rows=2272 width=10)
-> Subquery Scan on pcp (cost=123.40..168.84 rows=2272 width=10)
-> HashAggregate (cost=123.40..146.12 rows=2272 width=10)
Group Key: tbl_purchase_contracts.id
-> Seq Scan on tbl_purchase_contracts (cost=0.00..117.72 rows=2272 width=10)
-> Hash (cost=122.23..122.23 rows=2164 width=36)
-> Subquery Scan on pci (cost=73.55..122.23 rows=2164 width=36)
-> HashAggregate (cost=73.55..100.59 rows=2164 width=36)
Group Key: tbl_purchase_contract_invoices.contract_id
-> Seq Scan on tbl_purchase_contract_invoices (cost=0.00..60.70 rows=2569 width=10)
Filter: ((red_invoice_id IS NOT NULL) OR (NOT is_cancel))