QUERY PLAN
Aggregate (cost=376.65..376.66 rows=1 width=8)
-> Sort (cost=374.75..375.07 rows=127 width=665)
Sort Key: c.amount DESC NULLS LAST
-> Group (cost=369.04..370.31 rows=127 width=665)
Group Key: c.id, tbl_purchase_contracts.erp_settle_amount, (sum(tbl_purchase_contract_invoices.amount))
-> Sort (cost=369.04..369.36 rows=127 width=48)
Sort Key: c.id, tbl_purchase_contracts.erp_settle_amount, (sum(tbl_purchase_contract_invoices.amount))
-> Nested Loop (cost=300.60..364.60 rows=127 width=48)
-> Index Only Scan using tbl_suppliers_pkey on tbl_suppliers supplier (cost=0.28..8.29 rows=1 width=4)
Index Cond: (id = 46)
-> Hash Right Join (cost=300.32..355.04 rows=127 width=52)
Hash Cond: (tbl_purchase_contract_invoices.contract_id = c.id)
-> 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))
-> Hash (cost=232.27..232.27 rows=127 width=20)
-> Hash Right Join (cost=180.57..232.27 rows=127 width=20)
Hash Cond: (tbl_purchase_contracts.id = c.id)
-> 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=108.45..108.45 rows=127 width=14)
-> Hash Right Join (cost=72.12..108.45 rows=127 width=14)
Hash Cond: (pcr.contract_id = c.id)
-> Seq Scan on tbl_purchase_client_rels pcr (cost=0.00..31.05 rows=2005 width=4)
-> Hash (cost=70.54..70.54 rows=127 width=14)
-> Seq Scan on tbl_purchase_contracts c (cost=0.00..70.54 rows=127 width=14)
Filter: (supplier_id = 46)