1

I am trying to create performance tests in MySQL v8. For this, I need to generate a fixed number of rows, so I can insert them into my tables.

In PostgreSQL, I would do something like:

insert into film(title)
select random_string(30)
from   generate_series(1, 100000);

Here, random_string(int) is a custom function. In MySQL, I could use something as mentioned in https://stackoverflow.com/a/47884557/9740433, which would be sufficient I suppose.

How do I generate the 100k rows in MySQL v8?

Jelly Orns
  • 197
  • 1
  • 2
  • 8

2 Answers2

0

To answer my own question, as per comment by Strawberry:

WITH RECURSIVE cte (n) AS
(
  SELECT 1
  UNION ALL
  SELECT n + 1 FROM cte WHERE n < 5
)
SELECT * FROM cte;

You might want to alter the recursion depth: SET SESSION cte_max_recursion_depth = 1000000;.

Jelly Orns
  • 197
  • 1
  • 2
  • 8
0

You can use the old-fashioned cross join method:

with d as (
      select 0 as d union all select 1 union all select 2 union all select 3 union all
             select 4 union all select 5 union all select 6 union all
             select 7 union all select 8 union all select 9
     ),
     n as (
      select (1 + d1 + d2 * 10 + d3 * 100 + d4 * 1000 + d5 * 10000) as n
      from d d1 cross join
           d d2 cross join
           d d3 cross join
           d d4 cross join
           d d5
     )
select *
from n;

It would be interesting to test the performance of the cross join against the recursive CTE.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786