QUERY PLAN
Limit (cost=1925.28..1925.33 rows=20 width=604)
-> Sort (cost=1925.28..1926.93 rows=657 width=604)
Sort Key: (sum(pc.total_amount)) DESC NULLS LAST
-> HashAggregate (cost=1883.16..1907.80 rows=657 width=604)
Group Key: s.id
-> Hash Left Join (cost=1831.96..1866.74 rows=657 width=492)
Hash Cond: (s.id = pcr2.supplier_id)
-> Hash Left Join (cost=1500.00..1533.04 rows=657 width=484)
Hash Cond: (s.id = pcr.supplier_id)
-> Hash Left Join (cost=1168.03..1199.34 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=1162.22..1162.22 rows=465 width=100)
-> Subquery Scan on pc (cost=1149.43..1162.22 rows=465 width=100)
-> HashAggregate (cost=1149.43..1157.57 rows=465 width=100)
Group Key: pc_1.supplier_id
-> Hash Left Join (cost=997.04..1126.71 rows=2272 width=74)
Hash Cond: (pc_1.id = pci.contract_id)
-> Hash Left Join (cost=847.76..971.45 rows=2272 width=46)
Hash Cond: (pc_1.id = pcp.contract_id)
-> Seq Scan on tbl_purchase_contracts pc_1 (cost=0.00..117.72 rows=2272 width=14)
-> Hash (cost=845.26..845.26 rows=200 width=36)
-> Subquery Scan on pcp (cost=840.76..845.26 rows=200 width=36)
-> HashAggregate (cost=840.76..843.26 rows=200 width=36)
Group Key: "*SELECT* 1".contract_id
-> Append (cost=132.28..770.45 rows=4687 width=124)
-> Subquery Scan on "*SELECT* 1" (cost=132.28..229.02 rows=2458 width=90)
-> Hash Join (cost=132.28..198.30 rows=2458 width=86)
Hash Cond: (r.erp_payment_ticket_id = t.id)
-> Seq Scan on tbl_erp_payment_ticket_settles r (cost=0.00..41.01 rows=2501 width=18)
-> Hash (cost=103.39..103.39 rows=2311 width=44)
-> Seq Scan on tbl_erp_payment_tickets t (cost=0.00..103.39 rows=2311 width=44)
Filter: (is_payment_settle AND (status = ANY ('{2,5}'::integer[])))
-> Hash Join (cost=1.18..3.98 rows=8 width=126)
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..2.50 rows=38 width=50)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=1.08..1.08 rows=8 width=44)
-> Seq Scan on tbl_erp_other_income_ticket_purchase_contract_settles r_1 (cost=0.00..1.08 rows=8 width=44)
-> Nested Loop (cost=0.29..323.63 rows=45 width=122)
-> Seq Scan on tbl_erp_cash_cost_ticket_purchase_contract_settles r_2 (cost=0.00..1.45 rows=45 width=44)
-> Index Scan using tbl_erp_cash_cost_tickets_pkey on tbl_erp_cash_cost_tickets t_2 (cost=0.29..7.15 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=122.76..191.53 rows=2176 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.83 rows=2183 width=18)
-> Hash (cost=95.42..95.42 rows=2187 width=33)
-> Seq Scan on tbl_erp_collect_tickets t_3 (cost=0.00..95.42 rows=2187 width=33)
Filter: (status = ANY ('{2,5}'::integer[]))
-> 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))
-> Hash (cost=326.15..326.15 rows=465 width=12)
-> Subquery Scan on pcr (cost=301.05..326.15 rows=465 width=12)
-> GroupAggregate (cost=301.05..321.50 rows=465 width=12)
Group Key: pc_2.supplier_id
-> Sort (cost=301.05..306.32 rows=2107 width=8)
Sort Key: pc_2.supplier_id
-> Hash Left Join (cost=146.12..184.73 rows=2107 width=8)
Hash Cond: (pcr_1.contract_id = pc_2.id)
-> Seq Scan on tbl_purchase_client_rels pcr_1 (cost=0.00..33.07 rows=2107 width=8)
-> Hash (cost=117.72..117.72 rows=2272 width=8)
-> Seq Scan on tbl_purchase_contracts pc_2 (cost=0.00..117.72 rows=2272 width=8)
-> Hash (cost=326.15..326.15 rows=465 width=12)
-> Subquery Scan on pcr2 (cost=301.05..326.15 rows=465 width=12)
-> GroupAggregate (cost=301.05..321.50 rows=465 width=12)
Group Key: pc_3.supplier_id
-> Sort (cost=301.05..306.32 rows=2107 width=8)
Sort Key: pc_3.supplier_id
-> Hash Left Join (cost=146.12..184.73 rows=2107 width=8)
Hash Cond: (pcr_2.contract_id = pc_3.id)
-> Seq Scan on tbl_purchase_client_rels pcr_2 (cost=0.00..33.07 rows=2107 width=8)
-> Hash (cost=117.72..117.72 rows=2272 width=8)
-> Seq Scan on tbl_purchase_contracts pc_3 (cost=0.00..117.72 rows=2272 width=8)