QUERY PLAN
Aggregate (cost=99.85..99.86 rows=1 width=8)
-> Sort (cost=96.71..97.24 rows=209 width=360)
Sort Key: (min(CASE WHEN (1 = r.verify_type) THEN CASE WHEN (rvr.status = 0) THEN 1 WHEN (rsvr.status = 0) THEN 2 ELSE 4 END ELSE CASE WHEN ((rvr.status = 0) AND (r.next_verify_user = 5495)) THEN 1 WHEN ((rsvr.status = 0) AND (r.next_stop_verify_user = 5495)) THEN 2 ELSE 4 END END)), r.created_at DESC
-> HashAggregate (cost=86.57..88.66 rows=209 width=360)
Group Key: r.id
-> Hash Right Join (cost=52.57..79.19 rows=328 width=30)
Hash Cond: (rsvr.erp_sales_return_id = r.id)
-> Seq Scan on tbl_erp_sales_return_stop_verify_rels rsvr (cost=0.00..19.70 rows=970 width=8)
-> Hash (cost=51.68..51.68 rows=71 width=26)
-> Hash Left Join (cost=26.74..51.68 rows=71 width=26)
Hash Cond: (r.client_id = client.id)
Filter: ((client.sales_rep = 5495) OR (r.created_by = 5495) OR (rvr.user_id = 5495) OR (r.created_by = ANY ('{5493,5495,5495}'::integer[])))
-> Hash Right Join (cost=16.29..38.59 rows=965 width=38)
Hash Cond: (rvr.erp_sales_return_id = r.id)
-> Seq Scan on tbl_erp_sales_return_verify_rels rvr (cost=0.00..19.70 rows=970 width=12)
-> Hash (cost=13.68..13.68 rows=209 width=30)
-> Seq Scan on tbl_erp_sales_returns r (cost=0.00..13.68 rows=209 width=30)
Filter: ((status <> 0) OR ((status = 0) AND (created_by = 5495)))
-> Hash (cost=10.20..10.20 rows=20 width=8)
-> Seq Scan on tbl_clients client (cost=0.00..10.20 rows=20 width=8)