QUERY PLAN
Limit (cost=1766.66..1766.71 rows=20 width=604)
-> Sort (cost=1766.66..1768.31 rows=657 width=604)
Sort Key: (sum(pc.total_amount)) DESC NULLS LAST
-> HashAggregate (cost=1724.54..1749.18 rows=657 width=604)
Group Key: s.id
-> Hash Left Join (cost=1673.34..1708.12 rows=657 width=492)
Hash Cond: (s.id = pcr2.supplier_id)
-> Hash Left Join (cost=1387.96..1421.00 rows=657 width=484)
Hash Cond: (s.id = pcr.supplier_id)
-> Hash Left Join (cost=1102.57..1133.88 rows=657 width=476)
Hash Cond: (s.id = pc.supplier_id)
-> Seq Scan on tbl_suppliers s (cost=0.00..29.57 rows=657 width=380)
-> Hash (cost=1096.76..1096.76 rows=465 width=100)
-> Subquery Scan on pc (cost=1083.97..1096.76 rows=465 width=100)
-> HashAggregate (cost=1083.97..1092.11 rows=465 width=100)
Group Key: pc_1.supplier_id
-> Hash Left Join (cost=978.48..1061.21 rows=2276 width=74)
Hash Cond: (pc_1.id = pci.contract_id)
-> Hash Left Join (cost=829.02..905.76 rows=2276 width=46)
Hash Cond: (pc_1.id = pcp.contract_id)
-> Seq Scan on tbl_purchase_contracts pc_1 (cost=0.00..70.76 rows=2276 width=14)
-> Hash (cost=826.52..826.52 rows=200 width=36)
-> Subquery Scan on pcp (cost=822.02..826.52 rows=200 width=36)
-> HashAggregate (cost=822.02..824.52 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=97.34..753.41 rows=4574 width=124)
-> Subquery Scan on "*SELECT* 1" (cost=97.34..189.69 rows=2324 width=90)
-> Hash Join (cost=97.34..160.64 rows=2324 width=86)
Hash Cond: (r.erp_payment_ticket_id = t.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..39.65 rows=2365 width=18)
-> Hash (cost=68.42..68.42 rows=2313 width=44)
-> Seq Scan on tbl_erp_payment_tickets t (cost=0.00..68.42 rows=2313 width=44)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash Join (cost=1.25..3.13 rows=10 width=100)
Hash Cond: (t_1.id = r_1.erp_other_income_ticket_id)
-> Seq Scan on tbl_erp_other_income_tickets t_1 (cost=0.00..1.50 rows=37 width=50)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=1.11..1.11 rows=11 width=18)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.11 rows=11 width=18)
-> Nested Loop (cost=0.29..378.23 rows=53 width=95)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.53 rows=53 width=17)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_2 (cost=0.29..7.10 rows=1 width=46)
Index Cond: (id = r_2.erp_cash_cost_ticket_id)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash Join (cost=90.81..159.86 rows=2187 width=109)
Hash Cond: (r_3.erp_collect_ticket_id = t_3.id)
-> Seq Scan on tbl_erp_collect_ticket_settles r_3 (cost=0.00..35.94 rows=2194 width=18)
-> Hash (cost=63.45..63.45 rows=2189 width=33)
-> Seq Scan on tbl_erp_collect_tickets t_3 (cost=0.00..63.45 rows=2189 width=33)
Filter: (status = ANY ('{2,5}'::integer[]))
-> 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))
-> Hash (cost=279.57..279.57 rows=465 width=12)
-> Subquery Scan on pcr (cost=254.44..279.57 rows=465 width=12)
-> GroupAggregate (cost=254.44..274.92 rows=465 width=12)
Group Key: pc_2.supplier_id
-> Sort (cost=254.44..259.72 rows=2111 width=8)
Sort Key: pc_2.supplier_id
-> Hash Left Join (cost=99.21..137.87 rows=2111 width=8)
Hash Cond: (pcr_1.contract_id = pc_2.id)
-> Seq Scan on tbl_purchase_client_rels pcr_1 (cost=0.00..33.11 rows=2111 width=8)
-> Hash (cost=70.76..70.76 rows=2276 width=8)
-> Seq Scan on tbl_purchase_contracts pc_2 (cost=0.00..70.76 rows=2276 width=8)
-> Hash (cost=279.57..279.57 rows=465 width=12)
-> Subquery Scan on pcr2 (cost=254.44..279.57 rows=465 width=12)
-> GroupAggregate (cost=254.44..274.92 rows=465 width=12)
Group Key: pc_3.supplier_id
-> Sort (cost=254.44..259.72 rows=2111 width=8)
Sort Key: pc_3.supplier_id
-> Hash Left Join (cost=99.21..137.87 rows=2111 width=8)
Hash Cond: (pcr_2.contract_id = pc_3.id)
-> Seq Scan on tbl_purchase_client_rels pcr_2 (cost=0.00..33.11 rows=2111 width=8)
-> Hash (cost=70.76..70.76 rows=2276 width=8)
-> Seq Scan on tbl_purchase_contracts pc_3 (cost=0.00..70.76 rows=2276 width=8)