faster/less intensive way of getting a random row from db?

This is a discussion on faster/less intensive way of getting a random row from db? within the MySQL Database forums, part of the Database Forums category; Hello I am noob about mysql so I am not sure if there exists a better command/method to solve ...


Go Back   Usenet Forums > Database Forums > MySQL Database

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-31-2007
flagman5@gmail.com
 
Posts: n/a
Default faster/less intensive way of getting a random row from db?

Hello

I am noob about mysql so I am not sure if there exists a better
command/method to solve my problem. I read online about how using
rand() and the typical SELECT FROM table LIMIT rand(),1 is very slow
on a big table. The current method I was using is:

SELECT count(*) AS max FROM table
then use PHP code to generate a number from 0 to max, call it
$rand_row
then SELECT FROM table LIMIT $rand_row,1


This method has been working fine, until...sigh...i had to undergo
major site revision. and now I cannot just select from the table, I
have to check fields to only select the rows that contain certain
fields, so I do it like this:

SELECT count(*) AS max FROM table WHERE option1 = 'y'
then use PHP code to generate 0 to max, call it $rand_row
then SELECT * FROM table WHERE option1 = 'y', set the results to
$result
then random_id = mysql_result($result, $rand_row, 'id')

My question is, would this go crazy once my table is populated with
1000000 rows? I am doing a SELECT * but really only want to use 1 row.
Is there a better way?

Reply With Quote
  #2 (permalink)  
Old 05-31-2007
strawberry
 
Posts: n/a
Default Re: faster/less intensive way of getting a random row from db?

SELECT * FROM my_table ORDER BY RAND() LIMIT 1; ?

Reply With Quote
  #3 (permalink)  
Old 06-01-2007
flagman5
 
Posts: n/a
Default Re: faster/less intensive way of getting a random row from db?

On May 31, 3:10 pm, strawberry <zac.ca...@gmail.com> wrote:
> SELECT * FROM my_table ORDER BY RAND() LIMIT 1; ?


sir, u missed my points completely. and also I need to avoid rand()
function as it is documented to be very slow on big tables.

Reply With Quote
  #4 (permalink)  
Old 06-01-2007
Jon Slaughter
 
Posts: n/a
Default Re: faster/less intensive way of getting a random row from db?


"flagman5" <flagman5@gmail.com> wrote in message
news:1180652488.053756.143240@j4g2000prf.googlegro ups.com...
> On May 31, 3:10 pm, strawberry <zac.ca...@gmail.com> wrote:
>> SELECT * FROM my_table ORDER BY RAND() LIMIT 1; ?

>
> sir, u missed my points completely. and also I need to avoid rand()
> function as it is documented to be very slow on big tables.
>


How the hell do you expect to get a random entry without using random
numbers?

Why not generate a list of random numbers before hand and use them if your
concerned with the speed of rand()?


Reply With Quote
  #5 (permalink)  
Old 06-01-2007
lark
 
Posts: n/a
Default Re: faster/less intensive way of getting a random row from db?

flagman5@gmail.com wrote:
> Hello
>
> I am noob about mysql so I am not sure if there exists a better
> command/method to solve my problem. I read online about how using
> rand() and the typical SELECT FROM table LIMIT rand(),1 is very slow
> on a big table. The current method I was using is:
>
> SELECT count(*) AS max FROM table
> then use PHP code to generate a number from 0 to max, call it
> $rand_row
> then SELECT FROM table LIMIT $rand_row,1
>
>
> This method has been working fine, until...sigh...i had to undergo
> major site revision. and now I cannot just select from the table, I
> have to check fields to only select the rows that contain certain
> fields, so I do it like this:
>
> SELECT count(*) AS max FROM table WHERE option1 = 'y'
> then use PHP code to generate 0 to max, call it $rand_row
> then SELECT * FROM table WHERE option1 = 'y', set the results to
> $result
> then random_id = mysql_result($result, $rand_row, 'id')
>
> My question is, would this go crazy once my table is populated with
> 1000000 rows? I am doing a SELECT * but really only want to use 1 row.
> Is there a better way?
>

it depends! what database engine are you using? for myisam, this is not
gong to be a huge problem but for innodb, yes, it is! your biggest
problem is this code << select count(*) as max from table where
option1='y' >>
also, wouldn't use max as the column alias since it is a reserved word.
Reply With Quote
  #6 (permalink)  
Old 06-04-2007
Willem Bogaerts
 
Posts: n/a
Default Re: faster/less intensive way of getting a random row from db?

> On May 31, 3:10 pm, strawberry <zac.ca...@gmail.com> wrote:
>> SELECT * FROM my_table ORDER BY RAND() LIMIT 1; ?

>
> sir, u missed my points completely. and also I need to avoid rand()
> function as it is documented to be very slow on big tables.


The rand() function itself has nothing to do with tables, so it cannot
be "slow on large tables". What is slow is sorting an entire table by
random numbers and than just picking only one of them. Things would be
sped up tremendously if you could just use an indexed field for the
selection. One approach it to determine the maximum value of, for
instance, the primary key field. Then use the rand() function to
generate a ramdom number in the range covered by the existing key
values. If the generated key value does not exist, try again. If your
table does not have many "holes", this might be a fast option. You could
write a stored procedure to further minimize the queries.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 07:41 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0