QUERY PLAN
Limit (cost=31.37..31.41 rows=16 width=418)
-> Sort (cost=31.37..31.41 rows=16 width=418)
Sort Key: r.wrote_date DESC
-> Append (cost=2.71..31.05 rows=16 width=418)
-> GroupAggregate (cost=2.71..11.03 rows=1 width=546)
Group Key: r.id
-> Nested Loop Left Join (cost=2.71..10.99 rows=1 width=786)
-> Nested Loop Left Join (cost=2.59..10.78 rows=1 width=274)
-> Merge Join (cost=2.04..2.07 rows=1 width=274)
Merge Cond: (r.id = rp.erp_sales_return_id)
-> Sort (cost=1.02..1.03 rows=1 width=206)
Sort Key: r.id
-> Seq Scan on tbl_erp_sales_returns r (cost=0.00..1.01 rows=1 width=206)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=1.02..1.03 rows=1 width=72)
Sort Key: rp.erp_sales_return_id
-> Seq Scan on tbl_erp_sales_return_products rp (cost=0.00..1.01 rows=1 width=72)
Filter: (product_id = 2177)
-> Nested Loop Left Join (cost=0.55..8.70 rows=1 width=8)
-> Index Scan using tbl_erp_sales_outbound_spent_products_pkey on tbl_erp_sales_outbound_spent_products rp2 (cost=0.27..8.29 rows=1 width=8)
Index Cond: (rp.erp_sales_outbound_spent_product_id = id)
-> Index Scan using tbl_erp_inventory_products_pkey on tbl_erp_inventory_products eip (cost=0.27..0.41 rows=1 width=8)
Index Cond: (id = rp2.erp_inventory_product_id)
-> 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=19.00..19.87 rows=15 width=409)
Group Key: r_1.id
-> Sort (cost=19.00..19.06 rows=24 width=602)
Sort Key: r_1.id
-> Nested Loop Left Join (cost=2.71..18.44 rows=24 width=602)
Join Filter: (eip_1.erp_store_id = s_1.id)
-> Hash Join (cost=2.71..17.07 rows=24 width=90)
Hash Cond: (rp_1.erp_purchase_gift_id = r_1.id)
-> Hash Right Join (cost=1.27..15.56 rows=24 width=25)
Hash Cond: (eip_1.erp_purchase_gift_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.26..1.26 rows=1 width=8)
-> Seq Scan on tbl_erp_purchase_gift_products rp_1 (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_1 (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_1 (cost=0.00..1.01 rows=1 width=520)