Wed 21 Sep 2005
“Do not use ORDER BY RAND()” or “How to get random rows from table?”
Posted by Anton Titov under SQLQuite often people use queries like:
SELECT quote FROM quotes ORDER BY RAND() LIMIT 1
to get a random row (rows) from a table. That’s quite bad idea. For big tables, if your table have just
50-100 rows, use whatever you want.
What happens when you run such a query? Let’s say you run this query on a table with 10000 rows, than the SQL server generates 10000 random numbers, scans this numbers for the smallest one and gives you this row. Generating random numbers is relatively expensive operation, scaning them for the lowest one (if you have LIMIT 10, it will need to find 10 smallest numbers) is also not so fast (if quote is text it’s slower, if it’s something with fixed size it is faster, maybe because of need to create temporary table).
So. How can you select random row (rows) without this overhead? There is no easy drop in replacement. You can use something like:
SELECT COUNT(*) AS cnt FROM quotes
generate random number between 0 and cnt-1 in your programming language and run the query:
SELECT quote FROM quotes LIMIT $generated_number, 1
Yes. This are two queries, but they are MUCH faster than the first one. This option is good if you need just one random row. If you need more rows, you can still use this trick, just substract X (X is number of rows you need) from cnt when generating random number and modify query to:
SELECT quote FROM quotes LIMIT $generated_number, X
But this will give you X subsequent rows, starting from a random position. If that’s not an option, than you can use another approach: most of the time you have unique numeric field in tables, that start from 1 and continue to grow, so you can do something like:
SELECT MAX(id) AS maxid FROM quotes
generate X random numbers between 1 and X, join this numbers in string with ‘,’ for separator and run this query:
SELECT quote FROM quotes WHERE id IN ($idlist)
Yes. I know, you may have some deleted id’s and the query than may return less rows than you need. But you may generate 10 times more random ids and modify the query to look like:
SELECT quote FROM quotes WHERE id IN ($list_with_10_times_more_ids_than_x) LIMIT $x
now if you do not have too many rows deleted from the table the chances that you will not find let’s say 10 existing ids from list with 100 random numbers are near zero and you can include a code in your program, that will check if you have $x rows in the result and if not (let’s say once in 10000 times) it will run the equivalent ORDER BY RAND() code. You can also generate 100 times more ids than you need - the only overhead you’re introducing is generating 100 times more random numbers than $x (if you need 5 random rows from table with 10000 rows it’s better to generate 500 random numbers than 10000) and parsing of the query will be a bit slower, but the table scan will not be slower - the SQL server will stop searching for rows with corresponding ids from the list as soon
as it finds $x existing rows.
November 12th, 2010 at 1:25 am
… track backe bei http://erlenelaurelli.blog.com/ …
bon , votre site web thème est certainement grand, je suis recherche tout nouveau thème pour mon moncler doudoune personnel site web , j’aime vôtre, maintenant je vais aller cherchez le même disposition style !
February 12th, 2011 at 3:26 pm
Habbo Retros
Habbo Retros are slowly attracting more users with each passing day, most people prefer to play Habbo Retros with pets according to a recent google study, further evidence also supports that Habbo Retros have lead to an annual decrease in revenue for s…
June 12th, 2011 at 5:51 pm
hello
really good article. Ready to hear more next week,my blog http://www.kamusta.ph/topic/talonmpgraham/102860 Many Thanks.
June 13th, 2011 at 10:13 pm
hello
really good article. Ready to hear more next week,my blog http://talonmpgraham.xanga.com/749901497/adressing-wedding-invitations-envelopes/ Many Thanks.
June 16th, 2011 at 9:25 am
really good article
I have spent a bit of time going through your posts, more than I should have but I must say, its worth it! http://night071.beeplog.com/blog.pl?blogid=188845 many Thanks.
June 20th, 2011 at 10:03 am
hello
Hello there just quality post! http://ritama.20six.de/ ,i had a great read.thank you for your article,My problem has been resolved.
June 20th, 2011 at 11:09 pm
very helpful
I preferred to thank you for this good article. http://qmdxb.hostablog.net/ I by all odds liked every little bit of it
June 21st, 2011 at 8:32 pm
Great
You did a great job! http://denisse.mojblog.hr/p-an-available-letter-to-victorias-secret/221144.html
June 22nd, 2011 at 11:23 am
Great
You did a great job! http://deanaa.blogfa.com/post-12.aspx
June 26th, 2011 at 9:21 am
quality post
I have spent a bit of time going through your posts! http://denese.canalblog.com/archives/2011/06/19/21433915.html ,i had a good read.
June 27th, 2011 at 1:22 pm
Greate
It’s such a great site! http://ayanajvdixon.over-blog.com/article-british-bonanza-for-wimbledon-followers-77726528.html Great post, I just bookmarked it on Digg.
July 18th, 2011 at 9:14 pm
Great One
I must say, its worth it! My link!http://shannon071.oldright.com/ ,thanks haha
August 7th, 2011 at 6:04 am
very helpful
I preferred to thank you for this good article. I by all odds liked every little bit of it
August 8th, 2011 at 8:27 pm
Great
love your blog, http://phlog.net/users/jean11/entries/1725831 ,Thanks again.
August 9th, 2011 at 10:17 am
Great
love your blog, http://www.yousaytoo.com/eva11/blogs/dress/35767?37376 ,Thanks again.
August 13th, 2011 at 2:14 am
Great One
I must say, its worth it! My link, http://christi.monportefolio.com/,thanks haha
August 18th, 2011 at 3:03 am
really
Fat women and girls always worried because of their ugly shaped bodies. http://blog.livedoor.jp/uopljuy/ , but they can not wear these outwears
September 1st, 2011 at 11:31 pm
very helpful
I preferred to thank you for this good article. http://pcztew.bloginthedark.com/2011/08/31/40s-a-wedding-dress/ I by all odds liked every little bit of it
September 2nd, 2011 at 8:18 am
very helpful
I preferred to thank you for this good article. http://pcztew.blogtrue.com/ I by all odds liked every little bit of it
October 21st, 2011 at 9:38 pm
Great
I’ve been playing a bit of tennis lately and reckon that is right up there is terms of mentally demanding. And I’ve also been doing some sprint cycling and reckon that’s fairly physically demanding. . . What sports have other people played? What do …
December 2nd, 2011 at 5:27 am
quality post
girls ugg boots love, http://www.ballaterholidaycottage.com ,thanks ugg
January 12th, 2012 at 11:59 am
Hi
http://www.webcamgirls4.com/