I have the following query in rails which uses PostgreSQL 9 as the DB.
@user_random = User.where(:id => User.where(:pubic_profile_visible => true).order('random()').limit(1).select(:id).collect(&:id)).first
Which then on the DB Server:
Explain plan
Query plan Limit (cost=48655.53..48655.53 rows=1 width=4)
Query plan -> Sort (cost=48655.53..50880.35 rows=889930 width=4)
Query plan Sort Key: (random())
Query plan -> Seq Scan on users (cost=0.00..44205.88 rows=889930 width=4)
Query plan Filter: (pubic_profile_visible AND (deleted_at IS NULL))
Any suggestions on why this is causing a bottleneck? Thank you