QUERY PLAN
Limit (cost=122.43..122.46 rows=15 width=490)
-> Sort (cost=122.43..122.46 rows=15 width=490)
Sort Key: r.wrote_date DESC
-> Append (cost=9.18..122.13 rows=15 width=490)
-> GroupAggregate (cost=9.18..9.41 rows=5 width=395)
Group Key: r.id
-> Sort (cost=9.18..9.19 rows=5 width=593)
Sort Key: r.id
-> Nested Loop Left Join (cost=3.53..9.12 rows=5 width=593)
Join Filter: (eip.erp_store_id = s.id)
-> Hash Right Join (cost=3.53..8.03 rows=5 width=81)
Hash Cond: (eip.erp_purchase_inbound_product_id = rp.id)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..4.07 rows=107 width=11)
-> Hash (cost=3.49..3.49 rows=3 width=78)
-> Hash Join (cost=1.80..3.49 rows=3 width=78)
Hash Cond: (r.id = rp.erp_purchase_inbound_id)
-> Seq Scan on tbl_erp_purchase_inbounds r (cost=0.00..1.46 rows=32 width=59)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=1.76..1.76 rows=3 width=23)
-> Seq Scan on tbl_erp_purchase_inbound_products rp (cost=0.00..1.76 rows=3 width=23)
Filter: (product_id = 1040)
-> 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=34.79..34.99 rows=1 width=538)
Group Key: r_1.id
-> Nested Loop Left Join (cost=34.79..34.95 rows=1 width=782)
-> Merge Join (cost=34.66..34.69 rows=1 width=270)
Merge Cond: (rp_1.erp_purchase_return_id = r_1.id)
-> Sort (cost=22.03..22.03 rows=2 width=72)
Sort Key: rp_1.erp_purchase_return_id
-> Hash Right Join (cost=17.52..22.02 rows=2 width=72)
Hash Cond: (eip_1.erp_purchase_inbound_product_id = epip.id)
-> Seq Scan on tbl_erp_inventory_products eip_1 (cost=0.00..4.07 rows=107 width=8)
-> Hash (cost=17.50..17.50 rows=2 width=72)
-> Hash Right Join (cost=15.65..17.50 rows=2 width=72)
Hash Cond: (epip.id = rp_1.erp_purchase_inbound_product_id)
-> Seq Scan on tbl_erp_purchase_inbound_products epip (cost=0.00..1.61 rows=61 width=4)
-> Hash (cost=15.62..15.62 rows=2 width=72)
-> Seq Scan on tbl_erp_purchase_return_products rp_1 (cost=0.00..15.62 rows=2 width=72)
Filter: (product_id = 1040)
-> Sort (cost=12.63..12.64 rows=2 width=202)
Sort Key: r_1.id
-> Seq Scan on tbl_erp_purchase_returns r_1 (cost=0.00..12.62 rows=2 width=202)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_1 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_1.erp_store_id = id)
-> GroupAggregate (cost=6.73..6.92 rows=1 width=538)
Group Key: r_2.id
-> Nested Loop Left Join (cost=6.73..6.89 rows=1 width=746)
-> Merge Join (cost=6.60..6.63 rows=1 width=234)
Merge Cond: (rp_2.erp_inventory_lost_id = r_2.id)
-> Sort (cost=5.57..5.57 rows=1 width=40)
Sort Key: rp_2.erp_inventory_lost_id
-> Hash Right Join (cost=1.07..5.56 rows=1 width=40)
Hash Cond: (eip_2.id = rp_2.erp_inventory_product_id)
-> Seq Scan on tbl_erp_inventory_products eip_2 (cost=0.00..4.07 rows=107 width=8)
-> Hash (cost=1.06..1.06 rows=1 width=40)
-> Seq Scan on tbl_erp_inventory_lost_products rp_2 (cost=0.00..1.06 rows=1 width=40)
Filter: (product_id = 1040)
-> Sort (cost=1.03..1.04 rows=2 width=198)
Sort Key: r_2.id
-> Seq Scan on tbl_erp_inventory_losts r_2 (cost=0.00..1.02 rows=2 width=198)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_2 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_2.erp_store_id = id)
-> GroupAggregate (cost=3.19..7.79 rows=1 width=538)
Group Key: r_3.id
-> Nested Loop Left Join (cost=3.19..7.74 rows=1 width=796)
-> Nested Loop Left Join (cost=3.07..7.47 rows=1 width=284)
Join Filter: (rp_3.erp_inventory_build_id = r_3.id)
Filter: ((rp_3.product_id = 1040) OR (rp2.product_id = 1040))
-> Merge Left Join (cost=2.04..2.07 rows=1 width=266)
Merge Cond: (r_3.id = rp2.erp_inventory_build_id)
-> Sort (cost=1.02..1.03 rows=1 width=198)
Sort Key: r_3.id
-> Seq Scan on tbl_erp_inventory_builds r_3 (cost=0.00..1.01 rows=1 width=198)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=1.02..1.03 rows=1 width=72)
Sort Key: rp2.erp_inventory_build_id
-> Seq Scan on tbl_erp_inventory_build_spent_products rp2 (cost=0.00..1.01 rows=1 width=72)
Filter: (product_id = 1040)
-> Hash Right Join (cost=1.02..5.39 rows=1 width=30)
Hash Cond: (eip_3.erp_inventory_build_product_id = rp_3.id)
-> Seq Scan on tbl_erp_inventory_products eip_3 (cost=0.00..4.07 rows=107 width=26)
-> Hash (cost=1.01..1.01 rows=1 width=12)
-> Seq Scan on tbl_erp_inventory_build_products rp_3 (cost=0.00..1.01 rows=1 width=12)
Filter: (product_id = 1040)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_3 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_3.erp_store_id = id)
-> GroupAggregate (cost=8.08..8.18 rows=2 width=538)
Group Key: r_4.id
-> Sort (cost=8.08..8.09 rows=2 width=817)
Sort Key: r_4.id
-> Nested Loop Left Join (cost=3.19..8.07 rows=2 width=817)
-> Hash Right Join (cost=3.06..7.54 rows=2 width=305)
Hash Cond: (eip_4.erp_inventory_start_product_id = rp_4.id)
-> Seq Scan on tbl_erp_inventory_products eip_4 (cost=0.00..4.07 rows=107 width=11)
-> Hash (cost=3.05..3.05 rows=1 width=302)
-> Hash Join (cost=1.61..3.05 rows=1 width=302)
Hash Cond: (r_4.id = rp_4.erp_inventory_start_id)
-> Seq Scan on tbl_erp_inventory_starts r_4 (cost=0.00..1.31 rows=31 width=202)
-> Hash (cost=1.60..1.60 rows=1 width=104)
-> Seq Scan on tbl_erp_inventory_start_products rp_4 (cost=0.00..1.60 rows=1 width=104)
Filter: (product_id = 1040)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_4 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_4.erp_store_id = id)
-> GroupAggregate (cost=7.46..7.69 rows=1 width=538)
Group Key: r_5.id
-> Nested Loop Left Join (cost=7.46..7.63 rows=1 width=850)
-> Merge Join (cost=7.34..7.37 rows=1 width=338)
Merge Cond: (rp_5.erp_sales_outbound_id = r_5.id)
-> Sort (cost=6.12..6.12 rows=1 width=136)
Sort Key: rp_5.erp_sales_outbound_id
-> Hash Right Join (cost=1.62..6.11 rows=1 width=136)
Hash Cond: (eip_5.id = rp_5.erp_inventory_product_id)
-> Seq Scan on tbl_erp_inventory_products eip_5 (cost=0.00..4.07 rows=107 width=8)
-> Hash (cost=1.61..1.61 rows=1 width=136)
-> Seq Scan on tbl_erp_sales_outbound_spent_products rp_5 (cost=0.00..1.61 rows=1 width=136)
Filter: (product_id = 1040)
-> Sort (cost=1.22..1.23 rows=2 width=206)
Sort Key: r_5.id
-> Seq Scan on tbl_erp_sales_outbounds r_5 (cost=0.00..1.21 rows=2 width=206)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_5 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_5.erp_store_id = id)
-> GroupAggregate (cost=28.87..29.51 rows=1 width=538)
Group Key: r_6.id
-> Nested Loop Left Join (cost=28.87..29.47 rows=1 width=786)
-> Nested Loop Left Join (cost=28.75..29.21 rows=1 width=274)
-> Merge Join (cost=28.60..28.63 rows=1 width=274)
Merge Cond: (rp_6.erp_sales_return_id = r_6.id)
-> Sort (cost=15.97..15.97 rows=2 width=72)
Sort Key: rp_6.erp_sales_return_id
-> Hash Right Join (cost=14.28..15.96 rows=2 width=72)
Hash Cond: (rp2_1.id = rp_6.erp_sales_outbound_spent_product_id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products rp2_1 (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_6 (cost=0.00..14.25 rows=2 width=72)
Filter: (product_id = 1040)
-> Sort (cost=12.63..12.64 rows=2 width=206)
Sort Key: r_6.id
-> Seq Scan on tbl_erp_sales_returns r_6 (cost=0.00..12.62 rows=2 width=206)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Index Scan using tbl_erp_inventory_products_pkey on tbl_erp_inventory_products eip_6 (cost=0.14..0.57 rows=1 width=8)
Index Cond: (id = rp2_1.erp_inventory_product_id)
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_6 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_6.erp_store_id = id)
-> GroupAggregate (cost=2.07..10.20 rows=2 width=538)
Group Key: r_7.id
-> Nested Loop Left Join (cost=2.07..9.33 rows=54 width=736)
Join Filter: (eip_7.erp_store_id = s_7.id)
-> Nested Loop Left Join (cost=2.07..7.51 rows=54 width=224)
Join Filter: (eip_7.erp_purchase_gift_product_id = rp_7.id)
-> Merge Join (cost=2.07..2.10 rows=1 width=206)
Merge Cond: (r_7.id = rp_7.erp_purchase_gift_id)
-> Sort (cost=1.03..1.04 rows=2 width=202)
Sort Key: r_7.id
-> Seq Scan on tbl_erp_purchase_gifts r_7 (cost=0.00..1.02 rows=2 width=202)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=1.03..1.04 rows=1 width=8)
Sort Key: rp_7.erp_purchase_gift_id
-> Seq Scan on tbl_erp_purchase_gift_products rp_7 (cost=0.00..1.02 rows=1 width=8)
Filter: (product_id = 1040)
-> Seq Scan on tbl_erp_inventory_products eip_7 (cost=0.00..4.07 rows=107 width=26)
-> Materialize (cost=0.00..1.01 rows=1 width=520)
-> Seq Scan on tbl_erp_stores s_7 (cost=0.00..1.01 rows=1 width=520)
-> GroupAggregate (cost=7.09..7.29 rows=1 width=538)
Group Key: r_8.id
-> Nested Loop Left Join (cost=7.09..7.26 rows=1 width=754)
-> Merge Join (cost=6.96..6.99 rows=1 width=242)
Merge Cond: (rp_8.erp_sales_gift_id = r_8.id)
-> Sort (cost=5.80..5.81 rows=1 width=40)
Sort Key: rp_8.erp_sales_gift_id
-> Hash Right Join (cost=1.31..5.79 rows=1 width=40)
Hash Cond: (eip_8.id = rp_8.erp_inventory_product_id)
-> Seq Scan on tbl_erp_inventory_products eip_8 (cost=0.00..4.07 rows=107 width=8)
-> Hash (cost=1.30..1.30 rows=1 width=40)
-> Seq Scan on tbl_erp_sales_gift_spent_products rp_8 (cost=0.00..1.30 rows=1 width=40)
Filter: (product_id = 1040)
-> Sort (cost=1.16..1.16 rows=2 width=206)
Sort Key: r_8.id
-> Seq Scan on tbl_erp_sales_gifts r_8 (cost=0.00..1.15 rows=2 width=206)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Index Scan using tbl_erp_stores_pkey on tbl_erp_stores s_8 (cost=0.12..0.25 rows=1 width=520)
Index Cond: (eip_8.erp_store_id = id)