QUERY PLAN
Subquery Scan on sub (cost=298.10..304.34 rows=1 width=292)
Filter: (sub.id = 1560)
-> WindowAgg (cost=298.10..301.74 rows=208 width=292)
-> Sort (cost=298.10..298.62 rows=208 width=284)
Sort Key: sub_1.updated_at DESC
-> Subquery Scan on sub_1 (cost=285.93..290.09 rows=208 width=284)
-> HashAggregate (cost=285.93..288.01 rows=208 width=284)
Group Key: r.id
-> Hash Right Join (cost=237.26..285.41 rows=208 width=284)
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=235.04..235.04 rows=178 width=284)
-> Hash Left Join (cost=208.67..235.04 rows=178 width=284)
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=78.77..101.03 rows=1559 width=288)
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=59.28..59.28 rows=1559 width=284)
-> Seq Scan on tbl_erp_sales_orders r (cost=0.00..59.28 rows=1559 width=284)
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)