Mysql random rows

Posted by n00b on Stack Overflow See other posts from Stack Overflow or by n00b
Published on 2011-03-12T11:13:38Z Indexed on 2011/03/12 16:10 UTC
Read the original article Hit count: 242

Filed under:
|

please read the whole question... 90% of you dont seem to do that and some of you only read the title obviously...

and if you dont know the solution, dont answer - i wont have to downvote you -.-''


im entertaining the idea of getting random rows directly from mysql.

what i found was

SELECT * FROM tablename WHERE somefield='something' ORDER BY RAND() LIMIT 5

but even i see how slow that would be..

is the only way to do this doing something like

SELECT * FROM tablename WHERE somefield='something' LIMIT RAND(aincrementvalue-5), 1

5 times?

or is there a way that i with my little knowlege of databases cant come up with ?

(no i dont want random indexes. i hate the idea of them...)

@commenters - please first look, then think, then look again, think again and then post. i wont point fingers but i dislike stupid comments

and why i think random indexes are a nasty hack ?

  • it doesnt give you random results. it gives you x results from a random index in a predefined order
  • its like a gapless id only in the wrong order
  • if you fetch by 1 row and get true randomness you fall back to my method but with an additional junk field
  • finally the reason the field exists is only to serve as a helper to something that can be done without it with almost same performance (but the quality (randomness) is better), so it is a nasty hack ;)

i solved it, look @ my answer... if you think its incorrect please tell me :)

© Stack Overflow or respective owner

Related posts about mysql

Related posts about relational-database