QUERY PLAN
Limit (cost=277.50..277.55 rows=20 width=188)
-> Sort (cost=277.50..277.66 rows=64 width=188)
Sort Key: p.in_out_at DESC NULLS LAST, p.updated_at DESC
-> GroupAggregate (cost=274.15..275.80 rows=64 width=188)
Group Key: p.id
-> Merge Left Join (cost=274.15..274.84 rows=64 width=188)
Merge Cond: (p.id = eip_1.product_id)
-> Sort (cost=230.20..230.36 rows=64 width=156)
Sort Key: p.id
-> Hash Right Join (cost=215.29..228.28 rows=64 width=156)
Hash Cond: (esosps.erp_inventory_product_id = eip.id)
-> Seq Scan on tbl_erp_sales_outbound_spent_products esosps (cost=0.00..10.34 rows=534 width=8)
-> Hash (cost=214.49..214.49 rows=64 width=160)
-> Hash Right Join (cost=132.55..214.49 rows=64 width=160)
Hash Cond: (sc.from_project = epips.project_id)
-> Seq Scan on tbl_sales_contracts sc (cost=0.00..68.32 rows=1732 width=4)
-> Hash (cost=131.75..131.75 rows=64 width=164)
-> Hash Left Join (cost=114.88..131.75 rows=64 width=164)
Hash Cond: (epis.id = epips.erp_purchase_inbound_id)
-> Hash Left Join (cost=108.54..124.51 rows=64 width=164)
Hash Cond: (eip.erp_purchase_inbound_id = epis.id)
-> Hash Join (cost=98.75..114.55 rows=64 width=164)
Hash Cond: (eip.product_id = p.id)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..15.64 rows=64 width=32)
Filter: (inventory > '0'::numeric)
-> Hash (cost=72.22..72.22 rows=2122 width=156)
-> Seq Scan on tbl_products p (cost=0.00..72.22 rows=2122 width=156)
-> Hash (cost=7.13..7.13 rows=213 width=4)
-> Seq Scan on tbl_erp_purchase_inbounds epis (cost=0.00..7.13 rows=213 width=4)
-> Hash (cost=3.93..3.93 rows=193 width=8)
-> Seq Scan on tbl_erp_purchase_inbound_projects epips (cost=0.00..3.93 rows=193 width=8)
-> GroupAggregate (cost=43.95..44.17 rows=11 width=68)
Group Key: eip_1.product_id
-> Sort (cost=43.95..43.98 rows=11 width=8)
Sort Key: eip_1.product_id
-> Nested Loop Left Join (cost=21.05..43.76 rows=11 width=8)
-> Hash Left Join (cost=20.77..39.24 rows=11 width=8)
Hash Cond: (eips.erp_inventory_disassemble_spent_id = eid.id)
Filter: (((eips.erp_sales_outbound_id IS NOT NULL) AND (esos.status = 1)) OR ((eips.erp_sales_gift_id IS NOT NULL) AND (esg.status = 1)) OR ((eips.erp_inventory_lost_id IS NOT NULL) AND (eil.status = 1)) OR ((eips.erp_inventory_build_spent_id IS NOT NULL) AND (eib.status = 1)) OR ((eips.erp_inventory_disassemble_spent_id IS NOT NULL) AND (eid.status = 1)))
-> Hash Left Join (cost=19.66..36.98 rows=439 width=36)
Hash Cond: (eips.erp_inventory_build_spent_id = eib.id)
-> Hash Left Join (cost=18.39..34.54 rows=439 width=34)
Hash Cond: (eips.erp_inventory_lost_id = eil.id)
-> Hash Left Join (cost=17.19..32.18 rows=439 width=32)
Hash Cond: (eips.erp_sales_gift_id = esg.id)
-> Hash Left Join (cost=9.32..23.14 rows=439 width=30)
Hash Cond: (eips.erp_sales_outbound_id = esos.id)
-> Seq Scan on tbl_erp_inventory_product_spents eips (cost=0.00..12.65 rows=439 width=28)
Filter: ((erp_sales_outbound_id IS NOT NULL) OR (erp_sales_gift_id IS NOT NULL) OR (erp_inventory_lost_id IS NOT NULL) OR (erp_inventory_build_spent_id IS NOT NULL) OR (erp_inventory_disassemble_spent_id IS NOT NULL))
-> Hash (cost=6.92..6.92 rows=192 width=6)
-> Seq Scan on tbl_erp_sales_outbounds esos (cost=0.00..6.92 rows=192 width=6)
-> Hash (cost=5.72..5.72 rows=172 width=6)
-> Seq Scan on tbl_erp_sales_gifts esg (cost=0.00..5.72 rows=172 width=6)
-> Hash (cost=1.09..1.09 rows=9 width=6)
-> Seq Scan on tbl_erp_inventory_losts eil (cost=0.00..1.09 rows=9 width=6)
-> Hash (cost=1.12..1.12 rows=12 width=6)
-> Seq Scan on tbl_erp_inventory_builds eib (cost=0.00..1.12 rows=12 width=6)
-> Hash (cost=1.05..1.05 rows=5 width=6)
-> Seq Scan on tbl_erp_inventory_disassembles eid (cost=0.00..1.05 rows=5 width=6)
-> Index Scan using tbl_erp_inventory_products_pkey on tbl_erp_inventory_products eip_1 (cost=0.28..0.41 rows=1 width=8)
Index Cond: (eips.erp_inventory_product_id = id)