Given a table named tbl with one million entries, we
want to select a random row from this table, fast. Our table
definition looks like this:
create table tbl (
id INTEGER NOT NULL,
d VARCHAR(200) NOT NULL,
INDEX(id)
);
We can generate some test data using a recursive CTE:
mysql> set cte_max_recursion_depth = 100000;
mysql> insert into tbl
-> with recursive c(n, u) as (
-> select 1, uuid()
-> union all
-> select n+1, uuid() from c where n < 100000
-> ) select * from c ;
The Recursive CTE will generate 100k pairs of (number, uuid()).
The initial row is defined in the upper row of the
UNION, each subsequent row builds recursively on top
of that, by simply counting …