QUERY PLAN
Limit (cost=97.38..97.43 rows=20 width=434)
-> Sort (cost=97.38..97.45 rows=29 width=434)
Sort Key: r.wrote_date DESC
-> Append (cost=55.52..96.68 rows=29 width=434)
-> GroupAggregate (cost=55.52..55.83 rows=3 width=546)
Group Key: r.id
Filter: (max((sum(eip.inventory))) > '0'::numeric)
-> Merge Left Join (cost=55.52..55.71 rows=3 width=486)
Merge Cond: (r.id = rp.erp_inventory_disassemble_id)
Filter: ((rp.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.id = tbl_erp_inventory_disassemble_spent_products.erp_inventory_disassemble_id)
-> Sort (cost=12.50..12.51 rows=3 width=262)
Sort Key: r.id
-> Seq Scan on tbl_erp_inventory_disassembles r (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.erp_inventory_disassemble_id
-> Sort (cost=27.14..27.15 rows=2 width=537)
Sort Key: rp.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.erp_inventory_disassemble_product_id = rp.id)
-> Seq Scan on tbl_erp_inventory_products eip (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 (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 (cost=0.12..0.17 rows=1 width=520)
Index Cond: (eip.erp_store_id = id)
-> GroupAggregate (cost=20.00..20.55 rows=10 width=427)
Group Key: r_1.id
Filter: (sum(eip_1.inventory) > '0'::numeric)
-> Sort (cost=20.00..20.02 rows=10 width=649)
Sort Key: r_1.id
-> Nested Loop Left Join (cost=4.36..19.83 rows=10 width=649)
Join Filter: (eip_1.erp_store_id = s_1.id)
-> Hash Join (cost=4.36..18.67 rows=10 width=137)
Hash Cond: (rp_1.erp_inventory_start_id = r_1.id)
-> Hash Right Join (cost=1.64..15.92 rows=10 width=58)
Hash Cond: (eip_1.erp_inventory_start_product_id = rp_1.id)
-> Seq Scan on tbl_erp_inventory_products eip_1 (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_1 (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_1 (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_1 (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_2.id
Filter: (sum(eip_2.inventory) > '0'::numeric)
-> Sort (cost=19.00..19.06 rows=24 width=602)
Sort Key: r_2.id
-> Nested Loop Left Join (cost=2.71..18.44 rows=24 width=602)
Join Filter: (eip_2.erp_store_id = s_2.id)
-> Hash Join (cost=2.71..17.07 rows=24 width=90)
Hash Cond: (rp_2.erp_purchase_gift_id = r_2.id)
-> Hash Right Join (cost=1.27..15.56 rows=24 width=25)
Hash Cond: (eip_2.erp_purchase_gift_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=1.26..1.26 rows=1 width=8)
-> Seq Scan on tbl_erp_purchase_gift_products rp_2 (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_2 (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_2 (cost=0.00..1.01 rows=1 width=520)