php往mysql中批量插入數(shù)據(jù)實例教程
前言
假如說我有這樣一個表,我想往這個表里面插入大量數(shù)據(jù)
CREATE TABLE IF NOT EXISTS `user_info` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', `name` varchar(255) NOT NULL default '' COMMENT '姓名', `age` int(11) NOT NULL default '0' COMMENT '年齡', PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶信息表';
批量插入
方法一、使用for循環(huán)插入
在往mysql插入少量數(shù)據(jù)的時候,我們一般用for循環(huán)
$arr = [
[
'name' => 'testname1',
'age' => 18,
],
[
'name' => 'testname2',
'age' => 19,
],
[
'name' => 'testname3',
'age' => 18,
],
];
$servername = "localhost";
$port = 3306;
$username = "username";
$password = "password";
$dbname = "mytestdb";
// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname, $port);
// 檢測連接
if ($conn->connect_error) {
die("connect failed: " . $conn->connect_error);
}
$costBegin = microtime(true);
foreach($arr as $item) {
$sql = sprintf("INSERT INTO user_info (name, age) VALUES ( '%s', %d);", $item['name'], (int)$item['age']);
if ($conn->query($sql) === TRUE) {
echo "insert success";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$costEnd = microtime(true);
$cost = round($costEnd - $costBegin, 3);
var_dump($cost);
$conn->close();
假如說要批量插入大量數(shù)據(jù),如果還用for循環(huán)的辦法插入是沒有問題的,只是時間會比較長。
對比一下插入少量數(shù)據(jù)與插入大量數(shù)據(jù),使用上面的for循環(huán)插入耗費的時間:
| 條數(shù) | 時間 (單位:秒) |
|---|---|
| 10 | 0.011 |
| 1000 | 0.585 |
| 10000 | 5.733 |
| 100000 | 60.587 |
方法二、使用insert語句合并插入
mysql里面是可以使用insert語句進行合并插入的,比如
INSERT INTO user_info (name, age) VALUES ('name1', 18), ('name2', 19);表示一次插入兩條數(shù)據(jù)
下面看示例代碼,看看不同數(shù)據(jù)條數(shù)下
$arr = [
[
'name' => 'testname1',
'age' => 18,
],
[
'name' => 'testname2',
'age' => 19,
],
[
'name' => 'testname3',
'age' => 18,
],
// 此處省略
……
……
];
$servername = "localhost";
$port = 3306;
$username = "username";
$password = "password";
$dbname = "mytestdb";
// 創(chuàng)建連接
$conn = new mysqli($servername, $username, $password, $dbname, $port);
// 檢測連接
if ($conn->connect_error) {
die("connect failed: " . $conn->connect_error);
}
$costBegin = microtime(true);
if (!empty($arr)) {
$sql = sprintf("INSERT INTO user_info (name, age) VALUES ");
foreach($arr as $item) {
$itemStr = '( ';
$itemStr .= sprintf("'%s', %d", $item['name'], (int)$item['age']);
$itemStr .= '),';
$sql .= $itemStr;
}
// 去除最后一個逗號,并且加上結(jié)束分號
$sql = rtrim($sql, ',');
$sql .= ';';
if ($conn->query($sql) === TRUE) {
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$costEnd = microtime(true);
$cost = round($costEnd - $costBegin, 3);
var_dump($cost);
$conn->close();
下面看一下少量數(shù)據(jù)與大量數(shù)據(jù)的時間對比。從總體時間上,可以看出insert合并插入比剛才for循環(huán)插入節(jié)約了很多時間
| 條數(shù) | 時間 (單位:秒) |
|---|---|
| 10 | 0.006 |
| 1000 | 0.025 |
| 10000 | 0.131 |
| 100000 | 1.23 |
當然,如果你覺得數(shù)組太大,想要減少sql錯誤的風險,也可以使用array_chunk將數(shù)組切成指定大小的塊,然后對每個塊進行insert合并插入
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- PHP執(zhí)行批量mysql語句的解決方法
- 使用phpMyAdmin批量修改Mysql數(shù)據(jù)表前綴的方法
- php+mysqli實現(xiàn)批量執(zhí)行插入、更新及刪除數(shù)據(jù)的方法
- php+mysqli批量查詢多張表數(shù)據(jù)的方法
- PHP mysqli 增強 批量執(zhí)行sql 語句的實現(xiàn)代碼
- php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法
- php+mysqli實現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法
- php中批量刪除Mysql中相同前綴的數(shù)據(jù)表的代碼
- PHP實現(xiàn)mysqli批量執(zhí)行多條語句的方法示例
- PHP數(shù)據(jù)庫編程之MySQL優(yōu)化策略概述
- php+mysql查詢優(yōu)化簡單實例
- PHP優(yōu)化之批量操作MySQL實例分析
相關(guān)文章
自己寫的兼容低于PHP 5.5版本的array_column()函數(shù)
這篇文章主要介紹了自己寫的兼容低于PHP 5.5版本的array_column()函數(shù),array_column是PHP 5.5新增函數(shù),有時在低版本中也可能要用到,需要的朋友可以參考下2014-10-10
PHP嚴重致命錯誤處理:php Fatal error: Cannot redeclare class or funct
這篇文章主要介紹了PHP嚴重致命錯誤處理:php Fatal error: Cannot redeclare class or function,需要的朋友可以參考下2017-02-02
PHP實現(xiàn)根據(jù)數(shù)組某個鍵值大小進行排序的方法
這篇文章主要介紹了PHP實現(xiàn)根據(jù)數(shù)組某個鍵值大小進行排序的方法,涉及php針對數(shù)組的遍歷、排序等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
php str_replace替換指定次數(shù)的方法詳解
本篇文章主要介紹了php str_replace替換指定次數(shù)的方法,具有很好的參考價值。下面跟著小編一起來看下吧2017-05-05

