QUERY PLAN
Limit (cost=29.82..29.82 rows=1 width=538)
-> Sort (cost=29.82..29.82 rows=1 width=538)
Sort Key: r.wrote_date DESC
-> GroupAggregate (cost=29.15..29.80 rows=1 width=538)
Group Key: r.id
-> Nested Loop Left Join (cost=29.15..29.76 rows=1 width=786)
-> Nested Loop Left Join (cost=29.02..29.49 rows=1 width=274)
-> Merge Join (cost=28.88..28.92 rows=1 width=274)
Merge Cond: (rp.erp_sales_return_id = r.id)
-> Sort (cost=15.97..15.97 rows=2 width=72)
Sort Key: rp.erp_sales_return_id
-> Hash Right Join (cost=14.28..15.96 rows=2 width=72)
Hash Cond: (rp2.id = rp.erp_sales_outbound_spent_product_id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products rp2 (cost=0.00..1.49 rows=49 width=8)
-> Hash (cost=14.25..14.25 rows=2 width=72)
-> Seq Scan on tbl_erp_sales_return_products rp (cost=0.00..14.25 rows=2 width=72)
Filter: (product_id = 2105)
-> Sort (cost=12.91..12.92 rows=3 width=206)
Sort Key: r.id
-> Seq Scan on tbl_erp_sales_returns r (cost=0.00..12.89 rows=3 width=206)
Filter: (status = ANY ('{1,2,5}'::integer[]))
-> Index Scan using tbl_erp_inventory_products_pkey on tbl_erp_inventory_products eip (cost=0.14..0.57 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.25 rows=1 width=520)
Index Cond: (eip.erp_store_id = id)