MariaDB [raw_data]> drop table if exists test_table; Query OK, 0 rows affected (0.02 sec) MariaDB [raw_data]> MariaDB [raw_data]> create table test_table (id int, random_data varchar(36), static_int int, static_varchar varchar(10)) engine=innodb; Query OK, 0 rows affected (0.05 sec) MariaDB [raw_data]> MariaDB [raw_data]> insert into test_table(id, random_data, static_int, static_varchar) -> select id, random_data, 42, 'Hello' -> from ( -> with recursive data_generator(id, random_data) as ( -> select 1 as id, uuid() as random_data -> union all -> select id + 1, uuid() from data_generator where id < 1000 -> ) -> select * from data_generator -> ) as a; Query OK, 1000 rows affected (0.02 sec) Records: 1000 Duplicates: 0 Warnings: 0 MariaDB [raw_data]> MariaDB [raw_data]> commit; Query OK, 0 rows affected (0.00 sec) MariaDB [raw_data]> MariaDB [raw_data]> analyze table test_table; +---------------------+---------+----------+----------+ | Table | Op | Msg_type | Msg_text | +---------------------+---------+----------+----------+ | raw_data.test_table | analyze | status | OK | +---------------------+---------+----------+----------+ 1 row in set (0.01 sec) MariaDB [raw_data]> MariaDB [raw_data]> explain select * from (select id, lead(id) over(order by id) next_id from test_table order by id) a limit 10; +------+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 1000 | | | 2 | DERIVED | test_table | ALL | NULL | NULL | NULL | NULL | 1000 | Using temporary; Using filesort | +------+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+ 2 rows in set (0.00 sec) MariaDB [raw_data]> MariaDB [raw_data]> select * from (select id, lead(id) over(order by id) next_id from test_table order by id) a limit 10; +------+---------+ | id | next_id | +------+---------+ | 1 | 2 | | 2 | 3 | | 3 | 4 | | 4 | 5 | | 5 | 6 | | 6 | 7 | | 7 | 8 | | 8 | 9 | | 9 | 10 | | 10 | 11 | +------+---------+ 10 rows in set (0.00 sec) MariaDB [raw_data]> MariaDB [raw_data]> drop table if exists test_table; Query OK, 0 rows affected (0.03 sec) MariaDB [raw_data]> MariaDB [raw_data]> create table test_table (id int, random_data varchar(36), static_int int, static_varchar varchar(10)) engine=innodb; Query OK, 0 rows affected (0.05 sec) MariaDB [raw_data]> MariaDB [raw_data]> insert into test_table(id, random_data, static_int, static_varchar) -> select id, random_data, 42, 'Hello' -> from ( -> with recursive data_generator(id, random_data) as ( -> select 1 as id, uuid() as random_data -> union all -> select id + 1, uuid() from data_generator where id < 100000 -> ) -> select * from data_generator -> ) as a; Query OK, 100000 rows affected (0.63 sec) Records: 100000 Duplicates: 0 Warnings: 0 MariaDB [raw_data]> MariaDB [raw_data]> commit; Query OK, 0 rows affected (0.00 sec) MariaDB [raw_data]> MariaDB [raw_data]> analyze table test_table; +---------------------+---------+----------+----------+ | Table | Op | Msg_type | Msg_text | +---------------------+---------+----------+----------+ | raw_data.test_table | analyze | status | OK | +---------------------+---------+----------+----------+ 1 row in set (0.01 sec) MariaDB [raw_data]> MariaDB [raw_data]> explain select * from (select id, lead(id) over(order by id) next_id from test_table order by id) a limit 10; +------+-------------+------------+------+---------------+------+---------+------+-------+---------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+------------+------+---------------+------+---------+------+-------+---------------------------------+ | 1 | PRIMARY | | ALL | NULL | NULL | NULL | NULL | 99792 | | | 2 | DERIVED | test_table | ALL | NULL | NULL | NULL | NULL | 99792 | Using temporary; Using filesort | +------+-------------+------------+------+---------------+------+---------+------+-------+---------------------------------+ 2 rows in set (0.00 sec) MariaDB [raw_data]> MariaDB [raw_data]> select * from (select id, lead(id) over(order by id) next_id from test_table order by id) a limit 10; +------+---------+ | id | next_id | +------+---------+ | 1 | 1 | | 2 | 4097 | | 3 | 4097 | | 4 | 4097 | | 5 | 4097 | | 6 | 4097 | | 7 | 4097 | | 8 | 4097 | | 9 | 4097 | | 10 | 4097 | +------+---------+ 10 rows in set (0.50 sec) MariaDB [raw_data]>