Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

My Sql RAND function, a boon to Sql query makers

1 view
Skip to first unread message

Weasley

unread,
Dec 5, 2009, 2:19:00 AM12/5/09
to
The RAND function in MySQL is precisely there to prevent this kind of
problem. Instead of picking a random id with a PHP code, let the
server do it for you:

Must read the content, and let us know your experiences on using
Rand ??

http://www.ezdia.com/My_Sql_Rand%2528%2529_function%252C_limitations_and_usage/Content.do?id=711

Can anyone suggest alternate methods for such functionalities ?

John B. Matthews

unread,
Dec 5, 2009, 8:03:20 AM12/5/09
to
In article
<e4e7eb67-fc92-4545...@13g2000prl.googlegroups.com>,
Weasley <samyak...@gmail.com> wrote:

> The RAND function in MySQL is precisely there to prevent this kind of
> problem. Instead of picking a random id with a PHP code, let the
> server do it for you:
>
> Must read the content, and let us know your experiences on using

> Rand? Can anyone suggest alternate methods for such functionalities?

Bypassing a nearly 10K spamdex, the direct link follows:

<http://www.mustap.com/databasezone_post_141_mysql-rand-function>

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

David Lee Lambert

unread,
Dec 9, 2009, 10:46:43 AM12/9/09
to
On Dec 5, 8:03 am, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <e4e7eb67-fc92-4545-a3f1-107f3687e...@13g2000prl.googlegroups.com>,

>
>  Weasley <samyaksul...@gmail.com> wrote:
> > The RAND function in MySQL is precisely there to prevent this kind of
> > problem. Instead of picking a random id with a PHP code, let the
> > server do it for you:

As several of the comments on the article pointed out, in many cases
this technique results in a slow, I/O-intensive query (as would
similar
queries on Postgres or Oracle). I can think of reasons to do the
random ID selection in Java code:

* ability to use SecureRandom
* not using a database-specific random-number function

One of the comments had a link to a better explanations of the issues
and
possible solutions; that otehr article tries to to everything in SQL,
but
could be applied to Java:

http://jan.kneschke.de/projects/mysql/order-by-rand/

--
DLL

John B. Matthews

unread,
Dec 9, 2009, 2:24:59 PM12/9/09
to
In article
<4e2187db-1c49-4d7d...@m25g2000yqc.googlegroups.co
m>,

David Lee Lambert <dav...@lmert.com> wrote:

> On Dec 5, 8:03 am, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > In article
> > <e4e7eb67-fc92-4545-a3f1-107f3687e...@13g2000prl.googlegroups.com>,
> >
> >  Weasley <samyaksul...@gmail.com> wrote:
> > > The RAND function in MySQL is precisely there to prevent
> > > this kind of problem. Instead of picking a random id with
> > > a PHP code, let the server do it for you:
>
> As several of the comments on the article pointed out, in
> many cases this technique results in a slow, I/O-intensive
> query (as would similar queries on Postgres or Oracle). I
> can think of reasons to do the random ID selection in Java
> code:
>
> * ability to use SecureRandom
> * not using a database-specific random-number function
>
> One of the comments had a link to a better explanations of

> the issues and possible solutions; that other article tries

> to to everything in SQL, but could be applied to Java:
>
> http://jan.kneschke.de/projects/mysql/order-by-rand/

On the client side, I would add

* client side testing
* distribution sensitive data

Given an instance of Random, it's easy to accumulate a
List<Integer> of ids during initialization and obtain uniform
results with nextInt(ids.size()).

0 new messages