Jump to content

Most effecient method of using mysql's RAND() ?


unsider

Recommended Posts

I need to generate random results, maybe 5-10 random entries from a single DB table, (although this table could become quite large) and outputting them as quickly as possible. So therefore I need to optimize my query.

 

Also, I would like to prevent repetition.

 

Well we all know that I can use:

 

SELECT * FROM table ORDER BY RAND() LIMIT 0,9;

 

But from what I've read, it creates a temp file in the process of doing this (could be old info, didn't confirm), so this technically isn't the quickest way to do this.

 

Well I kept reading, and came up with this:

 

SELECT * FROM table WHERE id = (SELECT FLOOR( MAX(id) * RAND()) FROM table ) ORDER BY id LIMIT 1;

 

So my question is, is this a good method, are there better methods, what do YOU do to overcome this problem?

 

Thanks.

 

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.