1

我正在尝试在 MySQL v8 中创建性能测试。为此,我需要生成固定数量的行,以便将它们插入到我的表中。

在 PostgreSQL 中,我会做类似的事情:

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

这里,random_string(int)是一个自定义函数。在 MySQL 中,我可以使用https://stackoverflow.com/a/47884557/9740433中提到的东西,我想这已经足够了。

如何在 MySQL v8 中生成 100k 行?

4

2 回答 2

0

您可以使用老式cross join方法:

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;

cross join测试递归 CTE的性能会很有趣。

于 2018-05-28T12:40:10.090 回答
0

根据草莓的评论,回答我自己的问题:

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

您可能想要更改递归深度:SET SESSION cte_max_recursion_depth = 1000000;.

于 2018-05-28T08:29:26.903 回答