QUERY PLAN
Aggregate (cost=99.49..99.50 rows=1 width=8)
-> Sort (cost=99.31..99.34 rows=12 width=538)
Sort Key: sub.wrote_date DESC
-> Result (cost=7.87..99.09 rows=12 width=538)
-> Append (cost=7.87..98.97 rows=12 width=4)
-> Subquery Scan on sub (cost=7.87..7.92 rows=2 width=4)
-> GroupAggregate (cost=7.87..7.90 rows=2 width=538)
Group Key: r.id
-> Sort (cost=7.87..7.88 rows=2 width=8)
Sort Key: r.id
-> Hash Right Join (cost=3.38..7.86 rows=2 width=8)
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=8)
-> Hash (cost=3.37..3.37 rows=1 width=12)
-> Hash Join (cost=1.78..3.37 rows=1 width=12)
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=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Hash (cost=1.76..1.76 rows=1 width=8)
-> Seq Scan on tbl_erp_purchase_inbound_products rp (cost=0.00..1.76 rows=1 width=8)
Filter: (product_id = 1812)
-> Subquery Scan on "*SELECT* 2" (cost=34.66..34.71 rows=1 width=4)
-> GroupAggregate (cost=34.66..34.70 rows=1 width=538)
Group Key: r_1.id
-> Merge Join (cost=34.66..34.69 rows=1 width=8)
Merge Cond: (rp_1.erp_purchase_return_id = r_1.id)
-> Sort (cost=22.03..22.03 rows=2 width=4)
Sort Key: rp_1.erp_purchase_return_id
-> Hash Right Join (cost=17.52..22.02 rows=2 width=4)
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=8)
-> Hash Right Join (cost=15.65..17.50 rows=2 width=8)
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=8)
-> Seq Scan on tbl_erp_purchase_return_products rp_1 (cost=0.00..15.62 rows=2 width=8)
Filter: (product_id = 1812)
-> 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[]))
-> Subquery Scan on "*SELECT* 3" (cost=2.12..2.15 rows=1 width=4)
-> GroupAggregate (cost=2.12..2.14 rows=1 width=538)
Group Key: r_2.id
-> Sort (cost=2.12..2.13 rows=1 width=8)
Sort Key: r_2.id
-> Nested Loop (cost=0.00..2.11 rows=1 width=8)
Join Filter: (r_2.id = rp_2.erp_inventory_lost_id)
-> Seq Scan on tbl_erp_inventory_lost_products rp_2 (cost=0.00..1.06 rows=1 width=8)
Filter: (product_id = 1812)
-> Seq Scan on tbl_erp_inventory_losts r_2 (cost=0.00..1.02 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Subquery Scan on "*SELECT* 4" (cost=3.07..7.50 rows=1 width=4)
-> GroupAggregate (cost=3.07..7.49 rows=1 width=538)
Group Key: r_3.id
-> Nested Loop Left Join (cost=3.07..7.47 rows=1 width=8)
Join Filter: (rp_3.erp_inventory_build_id = r_3.id)
Filter: ((rp_3.product_id = 1812) OR (rp2.product_id = 1812))
-> Merge Left Join (cost=2.04..2.07 rows=1 width=12)
Merge Cond: (r_3.id = rp2.erp_inventory_build_id)
-> Sort (cost=1.02..1.03 rows=1 width=8)
Sort Key: r_3.id
-> Seq Scan on tbl_erp_inventory_builds r_3 (cost=0.00..1.01 rows=1 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Sort (cost=1.02..1.03 rows=1 width=8)
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=8)
Filter: (product_id = 1812)
-> Hash Right Join (cost=1.02..5.39 rows=1 width=8)
Hash Cond: (eip_2.erp_inventory_build_product_id = rp_3.id)
-> Seq Scan on tbl_erp_inventory_products eip_2 (cost=0.00..4.07 rows=107 width=8)
-> 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 = 1812)
-> Subquery Scan on "*SELECT* 5" (cost=7.55..7.60 rows=2 width=4)
-> GroupAggregate (cost=7.55..7.58 rows=2 width=538)
Group Key: r_4.id
-> Sort (cost=7.55..7.56 rows=2 width=8)
Sort Key: r_4.id
-> Hash Right Join (cost=3.06..7.54 rows=2 width=8)
Hash Cond: (eip_3.erp_inventory_start_product_id = rp_4.id)
-> Seq Scan on tbl_erp_inventory_products eip_3 (cost=0.00..4.07 rows=107 width=8)
-> Hash (cost=3.05..3.05 rows=1 width=12)
-> Hash Join (cost=1.61..3.05 rows=1 width=12)
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=8)
-> Hash (cost=1.60..1.60 rows=1 width=8)
-> Seq Scan on tbl_erp_inventory_start_products rp_4 (cost=0.00..1.60 rows=1 width=8)
Filter: (product_id = 1812)
-> Subquery Scan on "*SELECT* 6" (cost=2.86..2.89 rows=1 width=4)
-> GroupAggregate (cost=2.86..2.88 rows=1 width=538)
Group Key: r_5.id
-> Sort (cost=2.86..2.86 rows=1 width=8)
Sort Key: r_5.id
-> Nested Loop (cost=0.00..2.85 rows=1 width=8)
Join Filter: (r_5.id = rp_5.erp_sales_outbound_id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products rp_5 (cost=0.00..1.61 rows=1 width=8)
Filter: (product_id = 1812)
-> Seq Scan on tbl_erp_sales_outbounds r_5 (cost=0.00..1.21 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Subquery Scan on "*SELECT* 7" (cost=26.89..26.95 rows=1 width=4)
-> GroupAggregate (cost=26.89..26.94 rows=1 width=538)
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 = 1812)
-> Subquery Scan on "*SELECT* 8" (cost=6.70..6.74 rows=2 width=4)
-> HashAggregate (cost=6.70..6.72 rows=2 width=538)
Group Key: r_7.id
-> Hash Right Join (cost=2.09..6.57 rows=54 width=8)
Hash Cond: (eip_4.erp_purchase_gift_product_id = rp_7.id)
-> Seq Scan on tbl_erp_inventory_products eip_4 (cost=0.00..4.07 rows=107 width=8)
-> Hash (cost=2.07..2.07 rows=1 width=12)
-> Nested Loop (cost=0.00..2.07 rows=1 width=12)
Join Filter: (r_7.id = 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 = 1812)
-> Seq Scan on tbl_erp_purchase_gifts r_7 (cost=0.00..1.02 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))
-> Subquery Scan on "*SELECT* 9" (cost=2.48..2.51 rows=1 width=4)
-> GroupAggregate (cost=2.48..2.50 rows=1 width=538)
Group Key: r_8.id
-> Sort (cost=2.48..2.49 rows=1 width=8)
Sort Key: r_8.id
-> Nested Loop (cost=0.00..2.48 rows=1 width=8)
Join Filter: (r_8.id = rp_8.erp_sales_gift_id)
-> Seq Scan on tbl_erp_sales_gift_spent_products rp_8 (cost=0.00..1.30 rows=1 width=8)
Filter: (product_id = 1812)
-> Seq Scan on tbl_erp_sales_gifts r_8 (cost=0.00..1.15 rows=2 width=8)
Filter: (status = ANY ('{2,5}'::integer[]))