QUERY PLAN
Limit (cost=990.87..990.92 rows=20 width=540)
-> Sort (cost=990.87..996.18 rows=2121 width=540)
Sort Key: p.in_out_at DESC NULLS LAST, p.updated_at DESC
-> GroupAggregate (cost=757.08..934.44 rows=2121 width=540)
Group Key: p.id
-> Merge Left Join (cost=757.08..780.66 rows=2121 width=540)
Merge Cond: (p.id = tmp5.product_id)
-> Merge Left Join (cost=737.07..753.84 rows=2121 width=476)
Merge Cond: (p.id = tmp4.product_id)
-> Merge Left Join (cost=734.37..745.71 rows=2121 width=412)
Merge Cond: (p.id = tmp3.product_id)
-> Sort (cost=729.52..734.82 rows=2121 width=348)
Sort Key: p.id
-> Hash Join (cost=523.38..612.33 rows=2121 width=348)
Hash Cond: (p.id = p_2.id)
-> Hash Left Join (cost=88.39..171.76 rows=2121 width=348)
Hash Cond: (p.id = tmp2.product_id)
-> Hash Left Join (cost=37.31..115.10 rows=2121 width=284)
Hash Cond: (p.id = tmp1.product_id)
-> Seq Scan on tbl_products p (cost=0.00..72.21 rows=2121 width=156)
-> Hash (cost=32.71..32.71 rows=368 width=132)
-> Subquery Scan on tmp1 (cost=21.67..32.71 rows=368 width=132)
-> HashAggregate (cost=21.67..29.03 rows=368 width=164)
Group Key: sub1.product_id
-> Seq Scan on tbl_erp_inventory_products sub1 (cost=0.00..12.97 rows=497 width=16)
-> Hash (cost=46.48..46.48 rows=368 width=68)
-> Subquery Scan on tmp2 (cost=37.28..46.48 rows=368 width=68)
-> HashAggregate (cost=37.28..42.80 rows=368 width=68)
Group Key: e.product_id
-> Hash Left Join (cost=19.18..33.15 rows=551 width=13)
Hash Cond: (p_1.erp_inventory_product_id = e.id)
-> Seq Scan on tbl_erp_inventory_product_spents p_1 (cost=0.00..12.51 rows=551 width=13)
-> Hash (cost=12.97..12.97 rows=497 width=8)
-> Seq Scan on tbl_erp_inventory_products e (cost=0.00..12.97 rows=497 width=8)
-> Hash (cost=408.48..408.48 rows=2121 width=4)
-> HashAggregate (cost=366.06..387.27 rows=2121 width=4)
Group Key: p_2.id
-> Hash Right Join (cost=332.03..360.76 rows=2121 width=4)
Hash Cond: (esosps.erp_inventory_product_id = eip.id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products esosps (cost=0.00..8.11 rows=411 width=8)
-> Hash (cost=305.51..305.51 rows=2121 width=8)
-> Hash Left Join (cost=256.35..305.51 rows=2121 width=8)
Hash Cond: (epips.project_id = sc.from_project)
-> Hash Right Join (cost=116.54..136.88 rows=2121 width=12)
Hash Cond: (eip.product_id = p_2.id)
-> Hash Left Join (cost=17.82..36.85 rows=497 width=12)
Hash Cond: (eip.erp_purchase_inbound_id = epis.id)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..12.97 rows=497 width=32)
-> Hash (cost=15.17..15.17 rows=212 width=8)
-> Hash Right Join (cost=10.77..15.17 rows=212 width=8)
Hash Cond: (epips.erp_purchase_inbound_id = epis.id)
-> Seq Scan on tbl_erp_purchase_inbound_projects epips (cost=0.00..3.89 rows=189 width=8)
-> Hash (cost=8.12..8.12 rows=212 width=4)
-> Seq Scan on tbl_erp_purchase_inbounds epis (cost=0.00..8.12 rows=212 width=4)
-> Hash (cost=72.21..72.21 rows=2121 width=20)
-> Seq Scan on tbl_products p_2 (cost=0.00..72.21 rows=2121 width=20)
-> Hash (cost=118.25..118.25 rows=1725 width=4)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..118.25 rows=1725 width=4)
-> Sort (cost=4.85..4.97 rows=49 width=68)
Sort Key: tmp3.product_id
-> Subquery Scan on tmp3 (cost=2.25..3.48 rows=49 width=68)
-> HashAggregate (cost=2.25..2.99 rows=49 width=104)
Group Key: p_3.product_id
-> Seq Scan on tbl_erp_inventory_start_products p_3 (cost=0.00..1.50 rows=50 width=46)
-> Sort (cost=2.70..2.72 rows=9 width=68)
Sort Key: tmp4.product_id
-> Subquery Scan on tmp4 (cost=2.33..2.55 rows=9 width=68)
-> HashAggregate (cost=2.33..2.46 rows=9 width=72)
Group Key: p_4.product_id
-> Hash Join (cost=1.06..2.24 rows=9 width=68)
Hash Cond: (p_4.erp_inventory_lost_id = e_1.id)
-> Seq Scan on tbl_erp_inventory_lost_products p_4 (cost=0.00..1.13 rows=13 width=72)
-> Hash (cost=1.04..1.04 rows=2 width=4)
-> Seq Scan on tbl_erp_inventory_losts e_1 (cost=0.00..1.04 rows=2 width=4)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=20.02..20.27 rows=100 width=68)
Sort Key: tmp5.product_id
-> Subquery Scan on tmp5 (cost=14.19..16.69 rows=100 width=68)
-> HashAggregate (cost=14.19..15.69 rows=100 width=72)
Group Key: p_5.product_id
-> Hash Join (cost=1.16..13.19 rows=100 width=68)
Hash Cond: (p_5.erp_inventory_overflow_id = e_2.id)
-> Seq Scan on tbl_erp_inventory_overflow_products p_5 (cost=0.00..11.60 rows=160 width=72)
-> Hash (cost=1.10..1.10 rows=5 width=4)
-> Seq Scan on tbl_erp_inventory_overflows e_2 (cost=0.00..1.10 rows=5 width=4)
Filter: (status = ANY ('{2,5}'::integer[]))