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
Like @wildplasser says this query is doing exactly what you asked it to do.
It can’t pick the lowest random() for your “LIMIT 1” without fetching all 800,000 rows, generating a random number and then sorting them.
Try googling “postgresql select random row” and you’ll get half a dozen useful stackoverflow questions and also a link to an old blog post on depesz.com which covers the various options:
http://www.depesz.com/2007/09/16/my-thoughts-on-getting-random-row/