使用auto_increment填充表的最有效方法

时间:2013-11-05 04:54:53

标签: mysql sql

我有一个只有ID的表(INT auto_increment)。 在给定参数之前用条目填充该表的最有效方法是什么?

这是我提出的存储过程,但用100000条记录填充它需要相当多的时间:

DELIMITER $$
CREATE PROCEDURE insert_id(
IN createnum INT
)
BEGIN
  DECLARE i INT DEFAULT 1;

  WHILE i < createnum DO
    INSERT INTO table VALUES (i);
    SET i = i + 1;
  END WHILE;
END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

最快的方法是不使用任何循环,只使用纯SQL

insert into table1 (id)
select a.N + b.N * 10 + c.N * 100 + d.N * 1000 + e.N * 10000 + 1 as N
from (select 0 as N 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) a
      , (select 0 as N 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) b
      , (select 0 as N 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) c
      , (select 0 as N 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) d
      , (select 0 as N 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) e
order by N

此查询插入100000行

这是 SQLFiddle 演示


如果需要,可以将其包装在程序中

CREATE PROCEDURE insert_id(IN _maxid INT)
insert into table1(id)
select n
  from
(
  select a.N + b.N * 10 + c.N * 100 + d.N * 1000 + e.N * 10000 + f.N * 100000 + 1 N
    from (select 0 as N 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) a
      , (select 0 as N 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) b
      , (select 0 as N 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) c
      , (select 0 as N 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) d
      , (select 0 as N 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) e
      , (select 0 as N 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) f
) t
 where n <= _maxid;

此过程最多可填充1米行。

样本用法:

CALL insert_id(5500);
CALL insert_id(100000);

您还可以查看此帖MySQL: Fill a table within a Stored Procedure efficiently