mysql中迅速插入百萬條測試資料的方法

來源:互聯網
上載者:User

標籤:

對比一下,首先是用 mysql 的預存程序弄的: 

複製代碼代碼如下:
mysql>delimiter $ 
mysql>SET AUTOCOMMIT = 0$$ 
mysql> create procedure test() 
begin 
declare 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; 


結果 
mysql> call test; Query OK, 0 rows affected (58 min 30.83 sec) 
非常耗時。 
於是我又找了一個方法 
先用PHP代碼產生資料,再匯入: 

複製代碼代碼如下:
<?php 
$t=mktime(); 
set_time_limit(1000); 
$myFile="e:/insert.sql"; 
$fhandler=fopen($myFile,‘wb‘); 
if($fhandler){ 
$sql="268\t2\t‘0,262,268,‘\t0\t ‘2342‘\t‘423423‘\t‘123123‘\t‘23423423‘\t‘2012-01-09 09:55:43‘\t‘upload/product/20111205153432_53211.jpg‘\t‘upload/product/thumb_20111205153432_53211.jpg‘\tNULL\tNULL\t38\t‘件‘\t‘‘\t123\t123\t0"; 
$i=0; 
while($i<1000000)//1,000,000 
{ 
$i++; 
fwrite($fhandler,$sql."\r\n"); 
} 
echo"寫入成功,耗時:",mktime()-$t; 
} 


然後再匯入 

複製代碼代碼如下:
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分割。結果我插入10次資料,100W平均只要1分鐘搞定。 
第二種方式mysql中間省略了很多中間步驟,導致插入速度遠勝於第一種,具體的沒有研究。 

快速產生mysql上百萬條測試資料
由於測試需要,原表中只有1萬條資料,現在隨機複製插入記錄,快速達到100萬條。 

itemid是主鍵。 

運行幾次下面代碼。隨機取1000條插入, 

insert into downitems (chid,catid,softid,....) 
SELECT chid,catid,softid... FROM `downitems` WHERE itemid >= (SELECT floor(RAND() * (SELECT MAX(itemid) FROM `downitems`))) ORDER BY itemid LIMIT 1000; 

然後可以修改1000的數字了。改為5000或者1萬。很快可以達到100萬的資料量了。

 

轉自:http://www.jb51.net/article/30099.htm

 

 

 只是插入隨機的百萬條資料?如果是這種就使用預存程序,相關例子看這裡:http://fc-lamp.blog.163.com/blog/static/17456668720108305520576/

如果是從外部匯入大資料檔案,那麼可以嘗試使用LOAD DATA INFILE ,參看:http://fc-lamp.blog.163.com/blog/static/17456668720121021104916483/

或者參看官網:http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#insert-speed

mysql中迅速插入百萬條測試資料的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.