QUERY PLAN
Limit (cost=20.49..20.53 rows=16 width=418)
-> Sort (cost=20.49..20.53 rows=16 width=418)
Sort Key: wrote_date DESC
-> Append (cost=0.01..20.17 rows=16 width=418)
-> 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.id
Filter: (sum(eip.inventory) > '0'::numeric)
-> Sort (cost=19.00..19.06 rows=24 width=602)
Sort Key: r.id
-> Nested Loop Left Join (cost=2.71..18.44 rows=24 width=602)
Join Filter: (eip.erp_store_id = s.id)
-> Hash Join (cost=2.71..17.07 rows=24 width=90)
Hash Cond: (rp.erp_purchase_gift_id = r.id)
-> Hash Right Join (cost=1.27..15.56 rows=24 width=25)
Hash Cond: (eip.erp_purchase_gift_product_id = rp.id)
-> Seq Scan on tbl_erp_inventory_products eip (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 (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 (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 (cost=0.00..1.01 rows=1 width=520)