Get 10 rows from a query

I have a query that consists of joins and has a yield of about 30,000 + records. I would like to get 10 random rows from these records without using the SQL ORDER BY rand () command.

I tried to scroll through the records and put them into an array and mix (get the 1st 10 after shuffling), but it takes about 8-12 seconds to generate. I would like to reduce this processing time at all costs.

How can i do this?

+1
source share
1 answer

Well, if you do not want to use RAND (), select only the identifier column much faster and then select only 10 rows that you want

  • Select identifiers in $ idarray
    SELECT id FROM table
  • Shuffle and cut $ idarray
    shuffle($idarray);
    $ids=array_slice($idarray,0,10);
  • Select full lines
    $sql="SELECT ... WHERE id IN (".implode(', ', $idarray).")";

Edit: this is definitely much faster than using ORDER BY RAND ()!

+8
source

All Articles