QUERY PLAN
Limit (cost=172.42..172.47 rows=20 width=434)
-> Sort (cost=172.42..172.52 rows=40 width=434)
Sort Key: r.wrote_date DESC
-> Append (cost=39.23..171.35 rows=40 width=434)
-> GroupAggregate (cost=39.23..39.72 rows=9 width=407)
Group Key: r.id
Filter: (sum(eip.inventory) > '0'::numeric)
-> Sort (cost=39.23..39.25 rows=9 width=608)
Sort Key: r.id
-> Nested Loop Left Join (cost=23.62..39.08 rows=9 width=608)
Join Filter: (eip.erp_store_id = s.id)
-> Hash Join (cost=23.62..37.94 rows=9 width=96)
Hash Cond: (rp.erp_purchase_inbound_id = r.id)
-> Hash Right Join (cost=12.48..26.76 rows=10 width=33)
Hash Cond: (eip.erp_purchase_inbound_product_id = rp.id)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..12.97 rows=497 width=11)
-> Hash (cost=12.35..12.35 rows=10 width=30)
-> Seq Scan on tbl_erp_purchase_inbound_products rp (cost=0.00..12.35 rows=10 width=30)
Filter: (product_id = 2177)
-> Hash (cost=8.65..8.65 rows=200 width=67)
-> Seq Scan on tbl_erp_purchase_inbounds r (cost=0.00..8.65 rows=200 width=67)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Materialize (cost=0.00..1.01 rows=1 width=520)
-> Seq Scan on tbl_erp_stores s (cost=0.00..1.01 rows=1 width=520)
-> GroupAggregate (cost=30.31..34.84 rows=2 width=546)
Group Key: r_1.id
Filter: (max((sum(eip_1.inventory))) > '0'::numeric)
-> Nested Loop Left Join (cost=30.31..34.76 rows=2 width=486)
Join Filter: (build_out.erp_inventory_build_id = r_1.id)
Filter: ((rp_1.erp_inventory_build_id IS NOT NULL) OR (build_out.erp_inventory_build_id IS NOT NULL))
-> Merge Left Join (cost=29.28..33.67 rows=2 width=426)
Merge Cond: (r_1.id = rp_1.erp_inventory_build_id)
-> Sort (cost=1.04..1.04 rows=2 width=262)
Sort Key: r_1.id
-> Seq Scan on tbl_erp_inventory_builds r_1 (cost=0.00..1.03 rows=2 width=262)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> GroupAggregate (cost=28.24..32.60 rows=1 width=164)
Group Key: rp_1.erp_inventory_build_id
-> Sort (cost=28.24..28.86 rows=248 width=537)
Sort Key: rp_1.erp_inventory_build_id
-> Hash Left Join (cost=2.06..18.38 rows=248 width=537)
Hash Cond: (eip_1.erp_store_id = s_1.id)
-> Hash Right Join (cost=1.04..15.32 rows=248 width=25)
Hash Cond: (eip_1.erp_inventory_build_product_id = rp_1.id)
-> Seq Scan on tbl_erp_inventory_products eip_1 (cost=0.00..12.97 rows=497 width=25)
-> Hash (cost=1.02..1.02 rows=1 width=8)
-> Seq Scan on tbl_erp_inventory_build_products rp_1 (cost=0.00..1.02 rows=1 width=8)
Filter: (product_id = 2177)
-> Hash (cost=1.01..1.01 rows=1 width=520)
-> Seq Scan on tbl_erp_stores s_1 (cost=0.00..1.01 rows=1 width=520)
-> Materialize (cost=1.03..1.06 rows=1 width=68)
-> Subquery Scan on build_out (cost=1.03..1.06 rows=1 width=68)
-> HashAggregate (cost=1.03..1.05 rows=1 width=68)
Group Key: tbl_erp_inventory_build_spent_products.erp_inventory_build_id
-> Seq Scan on tbl_erp_inventory_build_spent_products (cost=0.00..1.02 rows=1 width=68)
Filter: (product_id = 2177)
-> GroupAggregate (cost=55.52..55.83 rows=3 width=546)
Group Key: r_2.id
Filter: (max((sum(eip_2.inventory))) > '0'::numeric)
-> Merge Left Join (cost=55.52..55.71 rows=3 width=486)
Merge Cond: (r_2.id = rp_2.erp_inventory_disassemble_id)
Filter: ((rp_2.erp_inventory_disassemble_id IS NOT NULL) OR (tbl_erp_inventory_disassemble_spent_products.erp_inventory_disassemble_id IS NOT NULL))
-> Merge Left Join (cost=28.38..28.48 rows=3 width=330)
Merge Cond: (r_2.id = tbl_erp_inventory_disassemble_spent_products.erp_inventory_disassemble_id)
-> Sort (cost=12.50..12.51 rows=3 width=262)
Sort Key: r_2.id
-> Seq Scan on tbl_erp_inventory_disassembles r_2 (cost=0.00..12.47 rows=3 width=262)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> GroupAggregate (cost=15.88..15.94 rows=2 width=68)
Group Key: tbl_erp_inventory_disassemble_spent_products.erp_inventory_disassemble_id
-> Sort (cost=15.88..15.89 rows=2 width=68)
Sort Key: tbl_erp_inventory_disassemble_spent_products.erp_inventory_disassemble_id
-> Seq Scan on tbl_erp_inventory_disassemble_spent_products (cost=0.00..15.88 rows=2 width=68)
Filter: (product_id = 2177)
-> Materialize (cost=27.14..27.21 rows=1 width=164)
-> GroupAggregate (cost=27.14..27.20 rows=1 width=164)
Group Key: rp_2.erp_inventory_disassemble_id
-> Sort (cost=27.14..27.15 rows=2 width=537)
Sort Key: rp_2.erp_inventory_disassemble_id
-> Nested Loop Left Join (cost=12.64..27.13 rows=2 width=537)
-> Hash Right Join (cost=12.51..26.79 rows=2 width=25)
Hash Cond: (eip_2.erp_inventory_disassemble_product_id = rp_2.id)
-> Seq Scan on tbl_erp_inventory_products eip_2 (cost=0.00..12.97 rows=497 width=25)
-> Hash (cost=12.50..12.50 rows=1 width=8)
-> Seq Scan on tbl_erp_inventory_disassemble_products rp_2 (cost=0.00..12.50 rows=1 width=8)
Filter: (product_id = 2177)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_2 (cost=0.12..0.17 rows=1 width=520)
Index Cond: (eip_2.erp_store_id = id)
-> GroupAggregate (cost=20.00..20.55 rows=10 width=427)
Group Key: r_3.id
Filter: (sum(eip_3.inventory) > '0'::numeric)
-> Sort (cost=20.00..20.02 rows=10 width=649)
Sort Key: r_3.id
-> Nested Loop Left Join (cost=4.36..19.83 rows=10 width=649)
Join Filter: (eip_3.erp_store_id = s_3.id)
-> Hash Join (cost=4.36..18.67 rows=10 width=137)
Hash Cond: (rp_3.erp_inventory_start_id = r_3.id)
-> Hash Right Join (cost=1.64..15.92 rows=10 width=58)
Hash Cond: (eip_3.erp_inventory_start_product_id = rp_3.id)
-> Seq Scan on tbl_erp_inventory_products eip_3 (cost=0.00..12.97 rows=497 width=11)
-> Hash (cost=1.62..1.62 rows=1 width=55)
-> Seq Scan on tbl_erp_inventory_start_products rp_3 (cost=0.00..1.62 rows=1 width=55)
Filter: (product_id = 2177)
-> Hash (cost=2.32..2.32 rows=32 width=83)
-> Seq Scan on tbl_erp_inventory_starts r_3 (cost=0.00..2.32 rows=32 width=83)
-> 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)
-> GroupAggregate (cost=0.01..0.04 rows=1 width=546)
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=19.00..19.96 rows=15 width=409)
Group Key: r_4.id
Filter: (sum(eip_4.inventory) > '0'::numeric)
-> Sort (cost=19.00..19.06 rows=24 width=602)
Sort Key: r_4.id
-> Nested Loop Left Join (cost=2.71..18.44 rows=24 width=602)
Join Filter: (eip_4.erp_store_id = s_4.id)
-> Hash Join (cost=2.71..17.07 rows=24 width=90)
Hash Cond: (rp_4.erp_purchase_gift_id = r_4.id)
-> Hash Right Join (cost=1.27..15.56 rows=24 width=25)
Hash Cond: (eip_4.erp_purchase_gift_product_id = rp_4.id)
-> Seq Scan on tbl_erp_inventory_products eip_4 (cost=0.00..12.97 rows=497 width=25)
-> Hash (cost=1.26..1.26 rows=1 width=8)
-> Seq Scan on tbl_erp_purchase_gift_products rp_4 (cost=0.00..1.26 rows=1 width=8)
Filter: (product_id = 2177)
-> Hash (cost=1.25..1.25 rows=15 width=69)
-> Seq Scan on tbl_erp_purchase_gifts r_4 (cost=0.00..1.25 rows=15 width=69)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Materialize (cost=0.00..1.01 rows=1 width=520)
-> Seq Scan on tbl_erp_stores s_4 (cost=0.00..1.01 rows=1 width=520)