QUERY PLAN
Limit (cost=374.33..374.38 rows=20 width=305)
-> Sort (cost=374.33..374.64 rows=127 width=305)
Sort Key: c.amount DESC NULLS LAST
-> Group (cost=369.04..370.95 rows=127 width=305)
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=177)
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=177)
-> 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=177)
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=145)
-> Hash Right Join (cost=180.57..232.27 rows=127 width=145)
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=139)
-> Hash Right Join (cost=72.12..108.45 rows=127 width=139)
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=139)
-> Seq Scan on tbl_purchase_contracts c (cost=0.00..70.54 rows=127 width=139)
Filter: (supplier_id = 46)