我正在尝试在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行?
发布于 2018-05-28 16:29:27
根据Strawberry的评论,回答我自己的问题:
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-28 20:40:10
您可以使用老式的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的性能将是很有趣的。
https://stackoverflow.com/questions/50561574
复制相似问题