Possible Duplicate:
MySQL select 10 random rows from 600K rows fast
I have a table with over 300,000 records. I need to select 10 or 13 or 20 records from this table.
I have tried following ways.
This takes long time to select
SELECT * FROM products ORDER BY RAND() LIMIT 0,12
OR
This way same thing but i can't seem to select more then 1 record
$temp = mysql_query('SELECT id FROM products limit 0,12');
if ( count( $temp ) > 0 ) {
$j = 1;
foreach( $temp as $index => $row ) {
$p[$j++] = $row[id];
}
$my_p= $p[ rand( 1, --$j ) ];
$pp = 'SELECT id FROM products WHERE id = {$my_p}';
}
UPDATE: Following this MySQL select 10 random rows from 600K rows fast
I have
SELECT * FROM QM_Products AS r1
JOIN (SELECT (RAND() * (SELECT MAX(id)
FROM QM_Products )) AS id) AS r2 WHERE r1.id >= r2.id
ORDER BY r1.id ASC
LIMIT 0, 10
This works Thank you all.