I have a table with more than 800K rows
. I'm trying to get random 4 ID's. My query works fast, but it sometimes gives me one, sometimes two and sometimes no results given. Any idea why?
Here is the query:
select * from table
where (ID % 1000) = floor(rand() * 1000)
AND `type`='5'
order by rand()
limit 4
the type='5'
only has 1603
rows and it doesn't always give me 4 rows. when i change it to type='11'
it works fine. Any idea how to fix this?
here is my code in Yii
$criteria = new CDbCriteria();
$criteria->addCondition('`t`.`id` % 1000 = floor(rand() * 1000)');
$criteria->compare('`t`.`type`', $this->type);
$criteria->order = 'rand()';
$criteria->limit = 4;
return ABC::model()->findAll($criteria);
PS: being a big and growing table, will need a quick query