delimiter $mysql>SET AUTOCOMMIT = 0$$mysql> create procedure test" />
最近想到創(chuàng)建一個大量數(shù)據(jù)的測試環(huán)境,于是找了一下怎么插入100W條數(shù)據(jù),我用的是20個字段。對比一下,首先是用 mysql 的存儲過程弄的:
mysql>delimiter $mysql>SET AUTOCOMMIT = 0$$mysql> create procedure test() begindeclare i decimal (10) default 0 ;dd:loop INSERT INTO `million` (`categ_id`, `categ_fid`, `SortPath`, `address`, `p_identifier`, `pro_specification`, `name`, `add_date`, `picture_url`, `thumb_url`, `is_display_front`, `create_html_time`, `hit`, `buy_sum`, `athor`, `templete _style`, `is_hot`, `is_new`, `is_best`) VALUES(268, 2, '0,262,268,', 0, '2342', '423423', '123123', '2012-01-09 09:55:43', 'upload/product/20111205153432_53211.jpg', 'upload/product/thumb_20111205153432_53211.jpg', 1, 0, 0, 0, 'admin', '0', 0, 0, 0); commit; set i = i+1; if i= 1000000 then leave dd; end if; end loop dd ;end;$mysql>delimiter ;mysql> call test;
結(jié)果
mysql> call test; Query OK, 0 rows affected (58 min 30.83 sec)
非常耗時。
于是我又找了一個方法
先用PHP代碼生成數(shù)據(jù),再導入:
然后再導入
LOAD DATA local INFILE 'e:/insert.sql' INTO TABLE tenmillion(`categ_id`, `categ_fid`, `SortPath`, `address`, `p_identifier`, `pro_specification`, `name`, `description`, `add_date`, `picture_url`, `thumb_url`, `shop_url`, `shop_thumb_url`, `brand_id`, `unit`, `square_meters_unit`, `market_price`, `true_price`, `square_meters_price`);
注意字段不再以逗號分割,以/t分割,條記錄以/r/n分割。結(jié)果我插入10次數(shù)據(jù),100W平均只要1分鐘搞定。
第二種方式mysql中間省略了很多中間步驟,導致插入速度遠勝于第一種,具體的沒有研究。
bitsCN.com聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com