有没有一种简单的方法可以创建一个包含 500000 (dummy/zero/empty) 行的 table
Is there an easy way to create a table with 500000 (dummy/zero/empty) rows
我想做一些听起来很简单的事情,但我找不到简单的方法来解决它:
创建一个新的 table,假设有 500000 行,并让它们自动编号 (ID)。
使用PHP可能是一个简单的循环,但我想知道是否真的有必要使用PHP。
我几乎可以肯定有一个使用 MySQL 命令甚至 phpMyAdmin 的简单解决方案。
是的,您只能使用 mysql
CREATE PROCEDURE insertProcedure()
BEGIN
DECLARE i int DEFAULT 0;
WHILE i <= 500000 DO
INSERT INTO table_name (id, col1, col2) VALUES (i, null, null);
SET i = i + 1;
END WHILE;
END
然后像这样计算:
CALL insertProcedure();
当然,您需要根据您的情况调整插入行(table_name、列数、值)。
如果 id
设置为自动递增,那么您可以将第一个 i
替换为 null
并让 mysql 使用它的计算值。
注意:要对其进行测试,请使用低得多的限制(比如 500)。插入 50 万行 将 需要一些时间,尤其是在 普通 PC
上
无条件连接任意两个表,结果行数将相乘。使用局部变量从该结果中增加每个 select 的 id。
这将创建大约 100 万行 (2^20);
set @i = 0;
drop TEMPORARY table if exists dummyids;
create TEMPORARY table dummyids
select @i := @i + 1 as id
from (select true union all select true) t0
join (select true union all select true) t1
join (select true union all select true) t2
join (select true union all select true) t3
join (select true union all select true) t4
join (select true union all select true) t5
join (select true union all select true) t6
join (select true union all select true) t7
join (select true union all select true) t8
join (select true union all select true) t9
join (select true union all select true) t10
join (select true union all select true) t11
join (select true union all select true) t12
join (select true union all select true) t13
join (select true union all select true) t14
join (select true union all select true) t15
join (select true union all select true) t16
join (select true union all select true) t17
join (select true union all select true) t18
join (select true union all select true) t19
;
select * from dummyids;
我想做一些听起来很简单的事情,但我找不到简单的方法来解决它:
创建一个新的 table,假设有 500000 行,并让它们自动编号 (ID)。
使用PHP可能是一个简单的循环,但我想知道是否真的有必要使用PHP。
我几乎可以肯定有一个使用 MySQL 命令甚至 phpMyAdmin 的简单解决方案。
是的,您只能使用 mysql
CREATE PROCEDURE insertProcedure()
BEGIN
DECLARE i int DEFAULT 0;
WHILE i <= 500000 DO
INSERT INTO table_name (id, col1, col2) VALUES (i, null, null);
SET i = i + 1;
END WHILE;
END
然后像这样计算:
CALL insertProcedure();
当然,您需要根据您的情况调整插入行(table_name、列数、值)。
如果 id
设置为自动递增,那么您可以将第一个 i
替换为 null
并让 mysql 使用它的计算值。
注意:要对其进行测试,请使用低得多的限制(比如 500)。插入 50 万行 将 需要一些时间,尤其是在 普通 PC
上无条件连接任意两个表,结果行数将相乘。使用局部变量从该结果中增加每个 select 的 id。
这将创建大约 100 万行 (2^20);
set @i = 0;
drop TEMPORARY table if exists dummyids;
create TEMPORARY table dummyids
select @i := @i + 1 as id
from (select true union all select true) t0
join (select true union all select true) t1
join (select true union all select true) t2
join (select true union all select true) t3
join (select true union all select true) t4
join (select true union all select true) t5
join (select true union all select true) t6
join (select true union all select true) t7
join (select true union all select true) t8
join (select true union all select true) t9
join (select true union all select true) t10
join (select true union all select true) t11
join (select true union all select true) t12
join (select true union all select true) t13
join (select true union all select true) t14
join (select true union all select true) t15
join (select true union all select true) t16
join (select true union all select true) t17
join (select true union all select true) t18
join (select true union all select true) t19
;
select * from dummyids;