QUERY PLAN
Limit (cost=35.13..35.15 rows=8 width=501)
-> Sort (cost=35.13..35.15 rows=8 width=501)
Sort Key: r.wrote_date DESC
-> Append (cost=8.46..35.01 rows=8 width=501)
-> GroupAggregate (cost=8.46..8.56 rows=2 width=395)
Group Key: r.id
Filter: (sum(eip.inventory) > '0'::numeric)
-> Sort (cost=8.46..8.46 rows=2 width=593)
Sort Key: r.id
-> Nested Loop Left Join (cost=3.56..8.45 rows=2 width=593)
-> Hash Right Join (cost=3.44..7.92 rows=2 width=81)
Hash Cond: (eip.erp_purchase_inbound_product_id = rp.id)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..4.07 rows=107 width=11)
-> Hash (cost=3.42..3.42 rows=1 width=78)
-> Hash Join (cost=1.78..3.42 rows=1 width=78)
Hash Cond: (r.id = rp.erp_purchase_inbound_id)
-> Seq Scan on tbl_erp_purchase_inbounds r (cost=0.00..1.51 rows=35 width=59)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> Hash (cost=1.76..1.76 rows=1 width=23)
-> Seq Scan on tbl_erp_purchase_inbound_products rp (cost=0.00..1.76 rows=1 width=23)
Filter: (product_id = 2105)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip.erp_store_id = id)
-> GroupAggregate (cost=3.20..7.80 rows=1 width=538)
Group Key: r_1.id
Filter: (sum(eip_1.inventory) > '0'::numeric)
-> Nested Loop Left Join (cost=3.20..7.74 rows=1 width=796)
-> Nested Loop Left Join (cost=3.07..7.48 rows=1 width=284)
Join Filter: (rp_1.erp_inventory_build_id = r_1.id)
Filter: ((rp_1.product_id = 2105) OR (rp2.product_id = 2105))
-> Merge Left Join (cost=2.05..2.07 rows=1 width=266)
Merge Cond: (r_1.id = rp2.erp_inventory_build_id)
-> Sort (cost=1.02..1.03 rows=1 width=198)
Sort Key: r_1.id
-> Seq Scan on tbl_erp_inventory_builds r_1 (cost=0.00..1.01 rows=1 width=198)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> Sort (cost=1.02..1.03 rows=1 width=72)
Sort Key: rp2.erp_inventory_build_id
-> Seq Scan on tbl_erp_inventory_build_spent_products rp2 (cost=0.00..1.01 rows=1 width=72)
Filter: (product_id = 2105)
-> Hash Right Join (cost=1.02..5.39 rows=1 width=30)
Hash Cond: (eip_1.erp_inventory_build_product_id = rp_1.id)
-> Seq Scan on tbl_erp_inventory_products eip_1 (cost=0.00..4.07 rows=107 width=26)
-> Hash (cost=1.01..1.01 rows=1 width=12)
-> Seq Scan on tbl_erp_inventory_build_products rp_1 (cost=0.00..1.01 rows=1 width=12)
Filter: (product_id = 2105)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_1 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_1.erp_store_id = id)
-> GroupAggregate (cost=8.08..8.19 rows=2 width=538)
Group Key: r_2.id
Filter: (sum(eip_2.inventory) > '0'::numeric)
-> Sort (cost=8.08..8.09 rows=2 width=817)
Sort Key: r_2.id
-> Nested Loop Left Join (cost=3.19..8.07 rows=2 width=817)
-> Hash Right Join (cost=3.06..7.54 rows=2 width=305)
Hash Cond: (eip_2.erp_inventory_start_product_id = rp_2.id)
-> Seq Scan on tbl_erp_inventory_products eip_2 (cost=0.00..4.07 rows=107 width=11)
-> Hash (cost=3.05..3.05 rows=1 width=302)
-> Hash Join (cost=1.61..3.05 rows=1 width=302)
Hash Cond: (r_2.id = rp_2.erp_inventory_start_id)
-> Seq Scan on tbl_erp_inventory_starts r_2 (cost=0.00..1.31 rows=31 width=202)
-> Hash (cost=1.60..1.60 rows=1 width=104)
-> Seq Scan on tbl_erp_inventory_start_products rp_2 (cost=0.00..1.60 rows=1 width=104)
Filter: (product_id = 2105)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_2 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_2.erp_store_id = id)
-> GroupAggregate (cost=0.01..0.04 rows=1 width=538)
Group Key: id
-> Sort (cost=0.01..0.02 rows=0 width=786)
Sort Key: id
-> Result (cost=0.00..0.00 rows=0 width=786)
One-Time Filter: false
-> GroupAggregate (cost=2.07..10.34 rows=2 width=538)
Group Key: r_3.id
Filter: (sum(eip_3.inventory) > '0'::numeric)
-> Nested Loop Left Join (cost=2.07..9.33 rows=54 width=736)
Join Filter: (eip_3.erp_store_id = s_3.id)
-> Nested Loop Left Join (cost=2.07..7.51 rows=54 width=224)
Join Filter: (eip_3.erp_purchase_gift_product_id = rp_3.id)
-> Merge Join (cost=2.07..2.10 rows=1 width=206)
Merge Cond: (r_3.id = rp_3.erp_purchase_gift_id)
-> Sort (cost=1.04..1.04 rows=2 width=202)
Sort Key: r_3.id
-> Seq Scan on tbl_erp_purchase_gifts r_3 (cost=0.00..1.03 rows=2 width=202)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> Sort (cost=1.03..1.04 rows=1 width=8)
Sort Key: rp_3.erp_purchase_gift_id
-> Seq Scan on tbl_erp_purchase_gift_products rp_3 (cost=0.00..1.02 rows=1 width=8)
Filter: (product_id = 2105)
-> Seq Scan on tbl_erp_inventory_products eip_3 (cost=0.00..4.07 rows=107 width=26)
-> Materialize (cost=0.00..1.01 rows=1 width=520)
-> Seq Scan on tbl_erp_stores s_3 (cost=0.00..1.01 rows=1 width=520)