Re: SQL - RANDOM

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



thx it looks good

ceo@xxxxxxxxx napsal(a):
For large tables, I generally create a "static_rand" column, and pre-populated it with random numbers and create an index on it.

Then, after "using up" the records, I have application logic to reset those records (and only those records) to new random numbers.

This provides MUCH better performance than using rand() on huge tables.

alter table whatever add static_rand float;
create index rand_index on whatever(static_rand);

select id from whatever order by static_rand limit 3;

-- PHP code to create an array of the 3 IDs you got:
$ids_sql = implode(',', $ids);
$query = "update whatever set static = rand() where id in ($ids_sql)";

It's a bit crude in that you have to remember to do the UPDATE, but quite fast, and not THAT tricky to modularize your code to always do the UPDATE.

Or not, actually, if it's okay to "cache" the random answer for a short period of time.  Then you can just have a cron job that does:
update whatever set static_rand = rand() order by static_rand limit 3;


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux