Yes, the idea seems good:
select min(ID), max(ID) from table into @min, @max; set @range = @max - @min; set @mr = @min + ((@range / 1000) * (rand() * 1000)); select ID from table where ID >= @mr and ID <= @mr + 1000 order by rand() limit 1
It can vary from 1000 to 10000 or any other that is necessary for scaling ...
EDIT: you can also try the following:
select ID from table where (ID % 1000) = floor(rand() * 1000) order by rand() limit 1 ;
Separates it into different lines ...
EDIT 2:
See: What is the best way to select a random row from a table in MySQL?
This is perhaps the fastest way:
select @row := floor(count(*) * rand()) from some_tbl; select some_ID from some_tbl limit @row, 1;
Unfortunately, variables cannot be used in the limit clause, so you have to use a dynamic query, either by writing a query string to the code, or using PREPARE and EXECUTE. In addition, the n, 1 constraint still requires scanning n elements in the table, so this is about twice as fast as the second method, indicated above on average. (Although it is probably more uniform and guarantees matching the found string)
user645280
source share