QUERY PLAN
Aggregate (cost=369.66..369.67 rows=1 width=96)
-> Hash Left Join (cost=277.34..353.73 rows=2123 width=44)
Hash Cond: (pc.id = pci.contract_id)
-> Hash Left Join (cost=139.54..210.35 rows=2123 width=16)
Hash Cond: (pc.id = pcp.contract_id)
-> Seq Scan on tbl_purchase_contracts pc (cost=0.00..65.23 rows=2123 width=14)
-> Hash (cost=113.00..113.00 rows=2123 width=10)
-> Subquery Scan on pcp (cost=70.54..113.00 rows=2123 width=10)
-> HashAggregate (cost=70.54..91.77 rows=2123 width=10)
Group Key: tbl_purchase_contracts.id
-> Seq Scan on tbl_purchase_contracts (cost=0.00..65.23 rows=2123 width=10)
-> Hash (cost=112.32..112.32 rows=2038 width=36)
-> Subquery Scan on pci (cost=66.47..112.32 rows=2038 width=36)
-> HashAggregate (cost=66.47..91.94 rows=2038 width=36)
Group Key: tbl_purchase_contract_invoices.contract_id
-> Seq Scan on tbl_purchase_contract_invoices (cost=0.00..54.65 rows=2364 width=10)
Filter: ((red_invoice_id IS NOT NULL) OR (NOT is_cancel))