MySQL批量入庫的方式
循環一條一條入庫
批量入庫
通過程序組合 insert into (字段) tbl vlaues(), vlaues(), vlaues(),...
事務入庫
$autoCommit = (isset($this->startTransaction) ? !$this->startTransaction : true);
$ids = array();
if ($autoCommit) {
$this->startTransaction();
}
foreach ($multiInsertData as $insertData) {
if ($dataKeys !== null) {
// apply column-names if given, else assume they're already given in the data
$insertData = array_combine($dataKeys, $insertData);
}
$id = $this->insert($tableName, $insertData);
if (!$id) {
if ($autoCommit) {
$this->rollback();
}
return false;
}
$ids[] = $id;
}
if ($autoCommit) {
$this->commit();
}
使用 LOAD DATA INFILE 文件導入
Tip: 通過PHP程序處理好對應格式的內容生成文件, 然后放到數據庫服務器中導入操作.
imagecode.txt
http://www.qyule01.fun/media/videos/tmb/000/005/451/1.jpg###100000
http://pic.rmb.bdstatic.com/81923c4cd60b8731b09cc55a0ee82cb8.jpeg###100000
http://pcookie.cnzz.com/app.gif?&cna=Q/1fFKG8SggCAXRVIh2iBCTo###100000
http://inews.gtimg.com/newsapp_ls/0/6134659193_150120/0###100000
load data local infile'/Users/home/xx/temp/xxyun/imagecode.txt'into table imagecode fields terminated by'###'lines terminated by'\n'(url,code);
測試數據
vbox-ubuntu-16.04 2G2核
// 寫入一個文件-本地vbox服務器 run time total :0.47590112686157
// 本地vbox服務器-耗費時間: run time total :46.867498874664
// 本地vbox服務器-去除rand函數: run time total :48.011456012726
// 本地vbox服務器-去除var_dump函數: run time total :0.12039113044739
// 本地vbox服務器-使用insertMulti函數 : run time total :43.925640106201
// 內網服務器-耗費時間: run time total :42.424664020538
相關資料
–secure-file-priv選項問題的解決方法 # show global variables like '%secure_file_priv%';
secure_file_priv 為 NULL 時,表示限制mysqld不允許導入或導出。
secure_file_priv 為 /tmp 時,表示限制mysqld只能在/tmp目錄中執行導入導出,其他目錄不能執行。
secure_file_priv 沒有值時,表示不限制mysqld在任意目錄的導入導出。