从查询中获取10行
我有它由连接的并且具有约30,000的记录的输出的查询。我想从这些记录的10个随机行,而不使用SQL命令ORDER BY兰特()。
I have a query which consists of joins and has an output of about 30,000+ records. I would like to get 10 random rows from these records without using SQL command ORDER BY rand().
我试图通过循环记录,并把它们放在一个数组和随机(获得洗牌后,10月1日)他们,但它需要大约8-12秒钟生成。我想不惜一切代价来减少这种处理时间。
I tried looping through the records and put them in an array and shuffle (get the 1st 10 after shuffle) them but it takes about 8-12 seconds to generate. I would like to reduce this processing time at all cost.
我将如何做到这一点?
How will I accomplish this?
好吧,如果你不希望使用RAND()做一个更快的选择仅仅是ID列的第一个,然后只选择你想要的10行
Ok if you don't want to use RAND() do a much faster select of just the id column first then select only the 10 rows you want
- 选择ID添加到$ idarray
SELECT ID FROM表
- 随机播放和切片$ idarray
洗牌($ idarray);
$ IDS = array_slice($ idarray,0,10);
- 选择完整的行
$ SQL =SELECT ... WHERE ID IN(.implode('',$ idarray)。);
- Select the IDs into $idarray
SELECT id FROM table
- Shuffle and slice $idarray
shuffle($idarray);
$ids=array_slice($idarray,0,10);
- Select the complete rows
$sql="SELECT ... WHERE id IN (".implode(', ', $idarray).")";
编辑:这肯定是比使用ORDER BY RAND()
This is certainly much faster than using ORDER BY RAND()!