在MYSQL

时间:2015-10-17 16:49:55

标签: mysql stored-procedures

我在mysql中编写了以下简单程序来运行插入语句1,000,000次。

 DELIMITER $$
 DROP PROCEDURE IF EXISTS INIT_DEGREE_PRECISION$$
 CREATE PROCEDURE INIT_DEGREE_PRECISION()
       BEGIN
               DECLARE x  INT;
               DECLARE zeros  VARCHAR(8);
               DELETE FROM degree_precision;
               SET x = 1;
               WHILE x  <= 999999 DO
                           insert into degree_precision (degree_precision_id) values (x);
                           SET  x = x + 1;
               END WHILE;
       END$$
 DELIMITER ;

但是当我调用这个程序时,我的本地机器上花了太多时间。我应该在远程服务器上运行它。有没有更好的方法呢?

我想做什么?

我的表只包含表degree_precision_id中的一列degree_precision。该表只有1,000,000行没有degree_precision_id值0 - 999999。要做到这一点,我已经写了一个程序,但需要花费很多时间。

2 个答案:

答案 0 :(得分:2)

我建议使用cross join

insert into degree_precision (degree_precision_id)
    select (@rn := @rn + 1) - 1 as rn
    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) d1 cross join 
         (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) d2 cross join 
         (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) d3 cross join 
         (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) d4 cross join 
         (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) d5 cross join 
         (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) d6 cross join
         (select @rn := 0) params

请注意,由于记录开销,create table as可能更快。

答案 1 :(得分:1)

在我的书中没有什么比这更快的了(我的书很小)

drop table myTable7;
create table myTable7
(   id int auto_increment primary key,
    blah int not null
)engine=myISAM;  -- <---- chosen for speed, and no-gaps, read comments below

insert myTable7(blah) values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1);
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;
insert myTable7(blah) select blah from myTable7;    -- 1.3M rows

-- total time 2.1 seconds

select count(*),min(id),max(id) from myTable7;
delete from myTable7 where id>1000000;  -- 1.1 secs
select count(*),min(id),max(id) from myTable7;  -- 1 M rows

then on to ALTER TABLE to reset auto inc if necessary

修改

关于下面N.B.的评论,请参阅此罚款answer和引用,并查看我的速度比较并尝试自己的

  

因此,在重读环境中,可以使用MyISAM表   使用固定行格式优于InnoDB读取InnoDB   缓冲池,如果有足够的数据写入撤消日志   包含在ibdata1中以支持强加的事务行为   关于InnoDB数据。

如此清楚(对我而言),INNODB陷入了撤消日志/“交易行为”的困境