關(guān)于PHP往mysql數(shù)據(jù)庫(kù)中批量插入數(shù)據(jù)實(shí)例教程

2019-01-22 15:07:51 來(lái)源:互聯(lián)網(wǎng)作者:nemo 人氣: 次閱讀 999 條評(píng)論

文章主要給大家介紹了關(guān)于php往mysql中批量插入數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧...

  文章主要給大家介紹了關(guān)于php往MySQL中批量插入數(shù)據(jù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧

前言

  假如說(shuō)我有這樣一個(gè)表,我想往這個(gè)表里面插入大量數(shù)據(jù)?

  1. CREATE TABLE IF NOT EXISTS `user_info` (
  2. `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵',
  3. `name` varchar(255) NOT NULL default '' COMMENT '姓名',
  4. `age` int(11) NOT NULL default '0' COMMENT '年齡',
  5. PRIMARY KEY (`id`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用戶信息表';

批量插入

方法一、使用for循環(huán)插入

  在往mysql插入少量數(shù)據(jù)的時(shí)候,我們一般用for循環(huán)?

  1. $arr = [
  2. [
  3. 'name' => 'testname1',
  4. 'age' => 18,
  5. ],
  6. [
  7. 'name' => 'testname2',
  8. 'age' => 19,
  9. ],
  10. [
  11. 'name' => 'testname3',
  12. 'age' => 18,
  13. ],
  14. ];
  15.  
  16. $servername = "localhost";
  17. $port = 3306;
  18. $username = "username";
  19. $password = "password";
  20. $dbname = "mytestdb";
  21.  
  22. // 創(chuàng)建連接
  23. $conn = new mysqli($servername, $username, $password, $dbname, $port);
  24.  
  25. // 檢測(cè)連接
  26. if ($conn->connect_error) {
  27. die("connect failed: " . $conn->connect_error);
  28. }
  29.  
  30. $costBegin = microtime(true);
  31.  
  32. foreach($arr as $item) {
  33. $sql = sprintf("INSERT INTO user_info (name, age) VALUES ( '%s', %d);", $item['name'], (int)$item['age']);
  34. if ($conn->query($sql) === TRUE) {
  35. echo "insert success";
  36. } else {
  37. echo "Error: " . $sql . "<br>" . $conn->error;
  38. }
  39. }
  40.  
  41. $costEnd = microtime(true);
  42. $cost = round($costEnd - $costBegin, 3);
  43. var_dump($cost);
  44.  
  45. $conn->close();

  假如說(shuō)要批量插入大量數(shù)據(jù),如果還用for循環(huán)的辦法插入是沒(méi)有問(wèn)題的,只是時(shí)間會(huì)比較長(zhǎng)。

  對(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語(yǔ)句合并插入

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

  INSERT INTO user_info (name, age) VALUES ('name1', 18), ('name2', 19);表示一次插入兩條數(shù)據(jù)

  下面看示例代碼,看看不同數(shù)據(jù)條數(shù)下?

  1. $arr = [
  2. [
  3. 'name' => 'testname1',
  4. 'age' => 18,
  5. ],
  6. [
  7. 'name' => 'testname2',
  8. 'age' => 19,
  9. ],
  10. [
  11. 'name' => 'testname3',
  12. 'age' => 18,
  13. ],
  14. // 此處省略
  15. ……
  16. ……
  17. ];
  18.  
  19. $servername = "localhost";
  20. $port = 3306;
  21. $username = "username";
  22. $password = "password";
  23. $dbname = "mytestdb";
  24.  
  25. // 創(chuàng)建連接
  26. $conn = new mysqli($servername, $username, $password, $dbname, $port);
  27.  
  28. // 檢測(cè)連接
  29. if ($conn->connect_error) {
  30. die("connect failed: " . $conn->connect_error);
  31. }
  32.  
  33. $costBegin = microtime(true);
  34.  
  35. if (!empty($arr)) {
  36. $sql = sprintf("INSERT INTO user_info (name, age) VALUES ");
  37.  
  38. foreach($arr as $item) {
  39. $itemStr = '( ';
  40. $itemStr .= sprintf("'%s', %d", $item['name'], (int)$item['age']);
  41. $itemStr .= '),';
  42. $sql .= $itemStr;
  43. }
  44.  
  45. // 去除最后一個(gè)逗號(hào),并且加上結(jié)束分號(hào)
  46. $sql = rtrim($sql, ',');
  47. $sql .= ';';
  48.  
  49. if ($conn->query($sql) === TRUE) {
  50. } else {
  51. echo "Error: " . $sql . "<br>" . $conn->error;
  52. }
  53. }
  54.  
  55. $costEnd = microtime(true);
  56. $cost = round($costEnd - $costBegin, 3);
  57. var_dump($cost);
  58.  
  59. $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)然,如果你覺(jué)得數(shù)組太大,想要減少sql錯(cuò)誤的風(fēng)險(xiǎn),也可以使用array_chunk將數(shù)組切成指定大小的塊,然后對(duì)每個(gè)塊進(jìn)行insert合并插入

總結(jié)

  以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值。

您可能感興趣的文章

相關(guān)文章