婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識(shí)庫 > php往mysql中批量插入數(shù)據(jù)實(shí)例教程

php往mysql中批量插入數(shù)據(jù)實(shí)例教程

熱門標(biāo)簽:亳州企業(yè)外呼系統(tǒng) 海南外呼系統(tǒng)方案 蘇州外呼系統(tǒng)有效果嗎 山東電銷卡外呼系統(tǒng)原理是什么 打開百度地圖標(biāo)注 智能電銷語音機(jī)器人資訊 地圖標(biāo)注怎么做商戶驗(yàn)證 400 電話 辦理 兼職做地圖標(biāo)注好賺錢嗎

前言

假如說我有這樣一個(gè)表,我想往這個(gè)表里面插入大量數(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ù)的時(shí)候,我們一般用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í)間會(huì)比較長。

對(duì)比一下插入少量數(shù)據(jù)與插入大量數(shù)據(jù),使用上面的for循環(huán)插入耗費(fèi)的時(shí)間:

條數(shù) 時(shí)間 (單位:秒)
10 0.011
1000 0.585
10000 5.733
100000 60.587

方法二、使用insert語句合并插入

mysql里面是可以使用insert語句進(jìn)行合并插入的,比如

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;
  }

 // 去除最后一個(gè)逗號(hào),并且加上結(jié)束分號(hào)
 $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ù)的時(shí)間對(duì)比。從總體時(shí)間上,可以看出insert合并插入比剛才for循環(huán)插入節(jié)約了很多時(shí)間

條數(shù) 時(shí)間 (單位:秒)
10 0.006
1000 0.025
10000 0.131
100000 1.23

當(dāng)然,如果你覺得數(shù)組太大,想要減少sql錯(cuò)誤的風(fēng)險(xiǎn),也可以使用array_chunk將數(shù)組切成指定大小的塊,然后對(duì)每個(gè)塊進(jìn)行insert合并插入

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。

您可能感興趣的文章:
  • PHP執(zhí)行批量mysql語句的解決方法
  • 使用phpMyAdmin批量修改Mysql數(shù)據(jù)表前綴的方法
  • php+mysqli實(shí)現(xiàn)批量執(zhí)行插入、更新及刪除數(shù)據(jù)的方法
  • php+mysqli批量查詢多張表數(shù)據(jù)的方法
  • PHP mysqli 增強(qiáng) 批量執(zhí)行sql 語句的實(shí)現(xiàn)代碼
  • php從memcache讀取數(shù)據(jù)再批量寫入mysql的方法
  • php+mysqli實(shí)現(xiàn)批量替換數(shù)據(jù)庫表前綴的方法
  • php中批量刪除Mysql中相同前綴的數(shù)據(jù)表的代碼
  • PHP實(shí)現(xiàn)mysqli批量執(zhí)行多條語句的方法示例
  • PHP數(shù)據(jù)庫編程之MySQL優(yōu)化策略概述
  • php+mysql查詢優(yōu)化簡單實(shí)例
  • PHP優(yōu)化之批量操作MySQL實(shí)例分析

標(biāo)簽:紹興 綏化 清遠(yuǎn) 萊蕪 安康 呼倫貝爾 金華 溫州

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《php往mysql中批量插入數(shù)據(jù)實(shí)例教程》,本文關(guān)鍵詞  php,往,mysql,中,批量,插入,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《php往mysql中批量插入數(shù)據(jù)實(shí)例教程》相關(guān)的同類信息!
  • 本頁收集關(guān)于php往mysql中批量插入數(shù)據(jù)實(shí)例教程的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 阿瓦提县| 鹰潭市| 夹江县| 抚宁县| 辽源市| 新巴尔虎右旗| 乡城县| 称多县| 龙胜| 清远市| 咸阳市| 中阳县| 九江县| 忻城县| 措美县| 黄骅市| 中卫市| 镇江市| 略阳县| 仁布县| 德江县| 郓城县| 新乡市| 泽库县| 兴城市| 马鞍山市| 延寿县| 定结县| 且末县| 门头沟区| 湘潭县| 托里县| 合阳县| 邓州市| 宁德市| 滦南县| 墨江| 马尔康县| 钟山县| 张家口市| 麦盖提县|