QUERY PLAN
Limit (cost=268704.44..268704.45 rows=3 width=311)
-> Sort (cost=268704.44..268704.45 rows=3 width=311)
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
-> GroupAggregate (cost=91.55..268704.41 rows=3 width=311)
Group Key: r.id
-> Merge Left Join (cost=91.55..3898.17 rows=11769165 width=315)
Merge Cond: (r.id = rsvr.erp_purchase_order_id)
-> Merge Left Join (cost=23.73..178.32 rows=48532 width=311)
Merge Cond: (r.id = epop.erp_purchase_order_id)
-> Nested Loop Left Join (cost=0.13..138.36 rows=719 width=311)
Join Filter: (rvr.erp_purchase_order_id = r.id)
Filter: ((r.created_by = 5495) OR (rvr.user_id = 5495) OR (r.created_by = ANY ('{5493,5495,5495}'::integer[])))
-> Nested Loop Left Join (cost=0.13..47.12 rows=3 width=307)
Join Filter: (cs.object_id = c.id)
-> Nested Loop Left Join (cost=0.13..13.56 rows=3 width=311)
Join Filter: (c.id = r.purchase_contract_id)
-> Index Scan using tbl_erp_purchase_orders_pkey on tbl_erp_purchase_orders r (cost=0.13..12.22 rows=3 width=307)
Filter: ((status <> 0) OR ((status = 0) AND (created_by = 5495)))
-> Materialize (cost=0.00..1.09 rows=6 width=4)
-> Seq Scan on tbl_purchase_contracts c (cost=0.00..1.06 rows=6 width=4)
-> Materialize (cost=0.00..33.17 rows=9 width=4)
-> Seq Scan on tbl_attachement_relationships cs (cost=0.00..33.12 rows=9 width=4)
Filter: (object_type = 108)
-> Materialize (cost=0.00..24.55 rows=970 width=12)
-> Seq Scan on tbl_erp_purchase_order_verify_rels rvr (cost=0.00..19.70 rows=970 width=12)
-> Sort (cost=23.60..24.28 rows=270 width=8)
Sort Key: epop.erp_purchase_order_id
-> Seq Scan on tbl_erp_purchase_order_products epop (cost=0.00..12.70 rows=270 width=8)
-> Sort (cost=67.82..70.25 rows=970 width=8)
Sort Key: rsvr.erp_purchase_order_id
-> Seq Scan on tbl_erp_purchase_order_stop_verify_rels rsvr (cost=0.00..19.70 rows=970 width=8)