QUERY PLAN
Aggregate (cost=260.54..260.55 rows=1 width=8)
-> Sort (cost=260.41..260.43 rows=9 width=506)
Sort Key: sub.wrote_date DESC
-> Result (cost=24.27..260.26 rows=9 width=506)
-> Append (cost=24.27..260.17 rows=9 width=4)
-> Subquery Scan on sub (cost=24.27..25.41 rows=1 width=4)
-> GroupAggregate (cost=24.27..25.40 rows=1 width=506)
Group Key: r.id
-> Nested Loop Left Join (cost=24.27..25.39 rows=1 width=8)
Join Filter: (eip.erp_purchase_inbound_product_id = rp.id)
-> Merge Join (cost=24.27..24.30 rows=1 width=12)
Merge Cond: (r.id = rp.erp_purchase_inbound_id)
-> Sort (cost=12.13..12.14 rows=2 width=8)
Sort Key: r.id
-> Seq Scan on tbl_erp_purchase_inbounds r (cost=0.00..12.12 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=12.13..12.14 rows=1 width=8)
Sort Key: rp.erp_purchase_inbound_id
-> Seq Scan on tbl_erp_purchase_inbound_products rp (cost=0.00..12.12 rows=1 width=8)
Filter: (product_id = 947)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..1.04 rows=4 width=8)
-> Subquery Scan on "*SELECT* 2" (cost=28.41..37.58 rows=1 width=4)
-> GroupAggregate (cost=28.41..37.57 rows=1 width=506)
Group Key: r_1.id
-> Nested Loop Left Join (cost=28.41..37.56 rows=1 width=8)
-> Merge Join (cost=28.27..28.30 rows=1 width=12)
Merge Cond: (r_1.id = rp_1.erp_purchase_return_id)
-> Sort (cost=12.63..12.64 rows=2 width=8)
Sort Key: r_1.id
-> Seq Scan on tbl_erp_purchase_returns r_1 (cost=0.00..12.62 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=15.63..15.64 rows=2 width=8)
Sort Key: rp_1.erp_purchase_return_id
-> Seq Scan on tbl_erp_purchase_return_products rp_1 (cost=0.00..15.62 rows=2 width=8)
Filter: (product_id = 947)
-> Nested Loop Left Join (cost=0.14..9.25 rows=1 width=4)
Join Filter: (eip_1.erp_purchase_inbound_product_id = epip.id)
-> Index Only Scan using tbl_erp_purchase_inbound_products_pkey on tbl_erp_purchase_inbound_products epip (cost=0.14..8.16 rows=1 width=4)
Index Cond: (id = rp_1.erp_purchase_inbound_product_id)
-> Seq Scan on tbl_erp_inventory_products eip_1 (cost=0.00..1.04 rows=4 width=8)
-> Subquery Scan on "*SELECT* 3" (cost=28.52..28.57 rows=1 width=4)
-> GroupAggregate (cost=28.52..28.56 rows=1 width=506)
Group Key: r_2.id
-> Merge Join (cost=28.52..28.55 rows=1 width=8)
Merge Cond: (r_2.id = rp_2.erp_inventory_lost_id)
-> Sort (cost=12.63..12.64 rows=2 width=8)
Sort Key: r_2.id
-> Seq Scan on tbl_erp_inventory_losts r_2 (cost=0.00..12.62 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=15.88..15.89 rows=2 width=8)
Sort Key: rp_2.erp_inventory_lost_id
-> Seq Scan on tbl_erp_inventory_lost_products rp_2 (cost=0.00..15.88 rows=2 width=8)
Filter: (product_id = 947)
-> Subquery Scan on "*SELECT* 4" (cost=25.02..42.12 rows=1 width=4)
-> GroupAggregate (cost=25.02..42.11 rows=1 width=506)
Group Key: r_3.id
-> Nested Loop Left Join (cost=25.02..42.10 rows=1 width=8)
Join Filter: (eip_2.erp_inventory_build_product_id = rp_3.id)
-> Nested Loop Left Join (cost=25.02..41.01 rows=1 width=12)
Join Filter: (rp2.erp_inventory_build_id = r_3.id)
Filter: ((rp_3.product_id = 947) OR (rp2.product_id = 947))
-> Merge Left Join (cost=25.02..25.05 rows=2 width=16)
Merge Cond: (r_3.id = rp_3.erp_inventory_build_id)
-> Sort (cost=12.26..12.27 rows=2 width=8)
Sort Key: r_3.id
-> Seq Scan on tbl_erp_inventory_builds r_3 (cost=0.00..12.25 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=12.76..12.77 rows=1 width=12)
Sort Key: rp_3.erp_inventory_build_id
-> Seq Scan on tbl_erp_inventory_build_products rp_3 (cost=0.00..12.75 rows=1 width=12)
Filter: (product_id = 947)
-> Materialize (cost=0.00..15.88 rows=2 width=8)
-> Seq Scan on tbl_erp_inventory_build_spent_products rp2 (cost=0.00..15.88 rows=2 width=8)
Filter: (product_id = 947)
-> Seq Scan on tbl_erp_inventory_products eip_2 (cost=0.00..1.04 rows=4 width=8)
-> Subquery Scan on "*SELECT* 5" (cost=21.46..21.48 rows=1 width=4)
-> GroupAggregate (cost=21.46..21.47 rows=1 width=506)
Group Key: r_4.id
-> Sort (cost=21.46..21.46 rows=1 width=8)
Sort Key: r_4.id
-> Nested Loop Left Join (cost=0.14..21.45 rows=1 width=8)
Join Filter: (eip_3.erp_inventory_start_product_id = rp_4.id)
-> Nested Loop (cost=0.14..20.36 rows=1 width=12)
-> Seq Scan on tbl_erp_inventory_start_products rp_4 (cost=0.00..12.12 rows=1 width=8)
Filter: (product_id = 947)
-> Index Scan using tbl_erp_inventory_starts_pkey on tbl_erp_inventory_starts r_4 (cost=0.14..8.16 rows=1 width=8)
Index Cond: (id = rp_4.erp_inventory_start_id)
-> Seq Scan on tbl_erp_inventory_products eip_3 (cost=0.00..1.04 rows=4 width=8)
-> Subquery Scan on "*SELECT* 6" (cost=26.27..26.32 rows=1 width=4)
-> GroupAggregate (cost=26.27..26.31 rows=1 width=506)
Group Key: r_5.id
-> Merge Join (cost=26.27..26.30 rows=1 width=8)
Merge Cond: (r_5.id = rp_5.erp_sales_outbound_id)
-> Sort (cost=12.51..12.52 rows=2 width=8)
Sort Key: r_5.id
-> Seq Scan on tbl_erp_sales_outbounds r_5 (cost=0.00..12.50 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=13.76..13.77 rows=2 width=8)
Sort Key: rp_5.erp_sales_outbound_id
-> Seq Scan on tbl_erp_sales_outbound_spent_products rp_5 (cost=0.00..13.75 rows=2 width=8)
Filter: (product_id = 947)
-> Subquery Scan on "*SELECT* 7" (cost=26.89..26.95 rows=1 width=4)
-> GroupAggregate (cost=26.89..26.94 rows=1 width=506)
Group Key: r_6.id
-> Merge Join (cost=26.89..26.93 rows=1 width=8)
Merge Cond: (r_6.id = rp_6.erp_sales_return_id)
-> Sort (cost=12.63..12.64 rows=2 width=8)
Sort Key: r_6.id
-> Seq Scan on tbl_erp_sales_returns r_6 (cost=0.00..12.62 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=14.26..14.27 rows=2 width=8)
Sort Key: rp_6.erp_sales_return_id
-> Seq Scan on tbl_erp_sales_return_products rp_6 (cost=0.00..14.25 rows=2 width=8)
Filter: (product_id = 947)
-> Subquery Scan on "*SELECT* 8" (cost=24.27..25.41 rows=1 width=4)
-> GroupAggregate (cost=24.27..25.40 rows=1 width=506)
Group Key: r_7.id
-> Nested Loop Left Join (cost=24.27..25.39 rows=1 width=8)
Join Filter: (eip_4.erp_purchase_gift_product_id = rp_7.id)
-> Merge Join (cost=24.27..24.30 rows=1 width=12)
Merge Cond: (r_7.id = rp_7.erp_purchase_gift_id)
-> Sort (cost=12.13..12.14 rows=2 width=8)
Sort Key: r_7.id
-> Seq Scan on tbl_erp_purchase_gifts r_7 (cost=0.00..12.12 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=12.13..12.14 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..12.12 rows=1 width=8)
Filter: (product_id = 947)
-> Seq Scan on tbl_erp_inventory_products eip_4 (cost=0.00..1.04 rows=4 width=8)
-> Subquery Scan on "*SELECT* 9" (cost=26.27..26.32 rows=1 width=4)
-> GroupAggregate (cost=26.27..26.31 rows=1 width=506)
Group Key: r_8.id
-> Merge Join (cost=26.27..26.30 rows=1 width=8)
Merge Cond: (r_8.id = rp_8.erp_sales_gift_id)
-> Sort (cost=12.51..12.52 rows=2 width=8)
Sort Key: r_8.id
-> Seq Scan on tbl_erp_sales_gifts r_8 (cost=0.00..12.50 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=13.76..13.77 rows=2 width=8)
Sort Key: rp_8.erp_sales_gift_id
-> Seq Scan on tbl_erp_sales_gift_spent_products rp_8 (cost=0.00..13.75 rows=2 width=8)
Filter: (product_id = 947)