QUERY PLAN
Sort (cost=66.32..66.32 rows=1 width=126)
Sort Key: eip.wrote_date, eip.created_at
-> Group (cost=66.30..66.31 rows=1 width=126)
Group Key: eip.id
-> Sort (cost=66.30..66.30 rows=1 width=126)
Sort Key: eip.id
-> Nested Loop Left Join (cost=30.81..66.29 rows=1 width=126)
Join Filter: (epgs.id = eip.erp_purchase_gift_id)
Filter: ((epips.client_id = 2128) OR (epgs.client_id = 2128) OR (eiops.client_id = 2128))
-> Hash Right Join (cost=30.81..64.63 rows=1 width=134)
Hash Cond: (eios.id = eip.erp_inventory_overflow_id)
Filter: ((epips.project_id = 3854) OR (eiops.project_id = 3854))
-> Hash Right Join (cost=1.23..34.68 rows=92 width=12)
Hash Cond: (eiops.erp_inventory_overflow_id = eios.id)
-> Seq Scan on tbl_erp_inventory_overflow_projects eiops (cost=0.00..28.50 rows=1850 width=12)
-> Hash (cost=1.10..1.10 rows=10 width=4)
-> Seq Scan on tbl_erp_inventory_overflows eios (cost=0.00..1.10 rows=10 width=4)
-> Hash (cost=29.58..29.58 rows=1 width=134)
-> Nested Loop Left Join (cost=8.17..29.58 rows=1 width=134)
-> Seq Scan on tbl_erp_inventory_products eip (cost=0.00..16.94 rows=1 width=126)
Filter: ((inventory > '0'::numeric) AND (product_id = 2266) AND (erp_store_id = 2))
-> Hash Right Join (cost=8.17..12.62 rows=1 width=12)
Hash Cond: (epips.erp_purchase_inbound_id = epis.id)
-> Seq Scan on tbl_erp_purchase_inbound_projects epips (cost=0.00..3.93 rows=193 width=12)
-> Hash (cost=8.16..8.16 rows=1 width=4)
-> Index Only Scan using tbl_erp_purchase_inbounds_pkey on tbl_erp_purchase_inbounds epis (cost=0.14..8.16 rows=1 width=4)
Index Cond: (id = eip.erp_purchase_inbound_id)
-> Seq Scan on tbl_erp_purchase_gifts epgs (cost=0.00..1.22 rows=22 width=8)