I have a database with 3 million records. I want it to randomly fetch 10 records. I read somewhere that you can randomly select records from a range of 1000 records so that it does not search the entire database.
I am currently using the following code which is very heavy.
$result = mysqli_query($conn, "SELECT FLOOR(RAND() * COUNT(*)) AS offset FROM miasta");
$offset_row = mysqli_fetch_object($result);
$offset = $offset_row->offset;
$result = mysqli_query($conn, "select * from (SELECT * FROM miasta) as podseld ORDER BY RAND() LIMIT $offset, 10");
$row = mysqli_fetch_array($result)
Please help me to speed up this query. Thank you in advance.
Randomly selected records take a long time to upload.