【问题标题】:Is there an easy way to create a table with 500000 (dummy/zero/empty) rows有没有一种简单的方法可以创建一个包含 500000(虚拟/零/空)行的表
【发布时间】:2016-02-16 14:54:40
【问题描述】:

我想做一些听起来很容易的事情,但我找不到简单的方法来解决它:
创建一个包含 500000 行的新表,并为它们自动编号 (ID)。

使用PHP可能是一个简单的循环,但我想知道是否真的有必要使用PHP。

我几乎可以肯定有一个使用 MySQL 命令甚至使用 phpMyAdmin 的简单解决方案。

【问题讨论】:

  • 如果您将该列设为自动编号列,您只需插入 50000 行并自动编号。使用 Notepad++ 之类的编辑器,您可以轻松生成插入 500000 行的插入语句,其余的由字段的自动编号处理。如果您遇到任何这些问题,请提出更具体的问题。
  • 似乎有点极端@GolezTrol。你在哪里画线?我会把它画在 1 行。这是我将如何处理 470 万行,需要 1 分钟:stackoverflow.com/a/33666394
  • 我怀疑我的是否极端,但你的肯定很聪明。但话又说回来。我想在我记得的几年里,我从来没有在表格中插入过 50 万行空行。即使我必须这样做,而且只做一次,我也不会太担心这个过程的效率。

标签: php mysql phpmyadmin


【解决方案1】:

是的,你只能使用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();

当然,您需要根据您的情况调整插入行(表名、列数、值)。

如果id 设置为自动增量,那么您可以将第一个i 替换为null 并让mysql 使用它的计算值。

注意:要对其进行测试,请使用低得多的限制(例如 500)。插入 50 万行需要一些时间,尤其是在 普通 PC 上

【讨论】:

  • 对不起,我没有设法存储过程,我总是得到 #1064 - 你的 SQL 语法有错误。我把它放在我的 phpMyAdmin 的命令行中,对吧?我的版本是 5.6.24....
  • 我认为这会插入 500001 条记录。
【解决方案2】:

无条件连接任意两个表,结果行数将成倍增加。使用局部变量从该结果中增加每个选择的 id。

这会创建大约 1M 行 (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;

【讨论】:

  • 太棒了,这 100% 解决了我的问题——虽然它不像我想的那么简单......
猜你喜欢
  • 1970-01-01
  • 2012-10-27
  • 2011-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-06
  • 2014-01-10
相关资源
最近更新 更多