0

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

1
  • 3
    Well, it appears you are sorting 800K records on random(), to pick only one. Commented Nov 10, 2012 at 18:07

1 Answer 1

1

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.