What is a fast way to select a random row from a large mysql table?
I'm working in php, but I'm interested in any solution even if it's in another language.
What is a fast way to select a random row from a large mysql table?
I'm working in php, but I'm interested in any solution even if it's in another language.
With a order yo will do a full scan table. Its best if you do a select count(*) and later get a random row=rownum between 0 and the last registry
Use the below query to get the random row
In pseudo code:
This assumes that
id
is a unique (primary) key.Create a Function to do this most likely the best answer and most fastest answer here!
Pros - Works even with Gaps and extremely fast.
Please keep in mind this code as not been tested but is a working concept to return random entries even with gaps.. As long as the gaps are not huge enough to cause a load time issue.
I see here a lot of solution. One or two seems ok but other solutions have some constraints. But the following solution will work for all situation
Here, id, don't need to be sequential. It could be any primary key/unique/auto increment column. Please see the following Fastest way to select a random row from a big MySQL table
Thanks Zillur - www.techinfobest.com
I have used this and the job was done the reference from here