QUERY PLAN
Aggregate (cost=329.85..329.86 rows=1 width=8)
-> Sort (cost=326.64..327.18 rows=214 width=413)
Sort Key: r.updated_at DESC
-> HashAggregate (cost=316.22..318.36 rows=214 width=413)
Group Key: r.id
-> Hash Right Join (cost=267.47..315.69 rows=214 width=12)
Hash Cond: (epop.erp_sales_order_id = r.id)
-> Seq Scan on tbl_erp_sales_order_products epop (cost=0.00..39.23 rows=1823 width=8)
-> Hash (cost=265.17..265.17 rows=184 width=12)
-> Hash Left Join (cost=238.79..265.17 rows=184 width=12)
Hash Cond: (r.client_id = client.id)
Filter: ((client.sales_rep = 1004) OR (r.created_by = 1004) OR (rvr.user_id = 1004) OR (r.created_by = 1004))
-> Hash Right Join (cost=108.89..131.15 rows=1563 width=24)
Hash Cond: (rvr.erp_sales_order_id = r.id)
-> Seq Scan on tbl_erp_sales_order_verify_rels rvr (cost=0.00..19.70 rows=970 width=8)
-> Hash (cost=89.35..89.35 rows=1563 width=20)
-> Seq Scan on tbl_erp_sales_orders r (cost=0.00..89.35 rows=1563 width=20)
Filter: ((status <> 0) OR ((status = 0) AND (created_by = 1004)))
-> Hash (cost=105.51..105.51 rows=1951 width=8)
-> Seq Scan on tbl_clients client (cost=0.00..105.51 rows=1951 width=8)