当前位置:首页 > PHP教程 > php应用 > 列表

php实现向mysql批量插入数据

发布:smiling 来源: PHP粉丝网  添加日期:2022-06-02 08:58:03 浏览: 评论:0 

现在有这样一个表,我们想往这个表里面插入大量数据该如何实现呢?

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

批量插入

方法一、使用for循环插入

在往mysql插入少量数据的时候,我们一般用for循环

  1. $arr = [  
  2.  
  3. [ 
  4.  
  5. 'name' => 'testname1', 
  6.  
  7. 'age' => 18, 
  8.  
  9. ], 
  10.  
  11. [ 
  12.  
  13. 'name' => 'testname2', 
  14.  
  15. 'age' => 19, 
  16.  
  17. ], 
  18.  
  19. [ 
  20.  
  21. 'name' => 'testname3', 
  22.  
  23. 'age' => 18, 
  24.  
  25. ], 
  26.  
  27. ];  
  28.  
  29. $servername = "localhost"; 
  30.  
  31. $port = 3306; 
  32.  
  33. $username = "username"; 
  34.  
  35. $password = "password"; 
  36.  
  37. $dbname = "mytestdb"; 
  38.   
  39. // 创建连接 
  40.  
  41. $conn = new mysqli($servername, $username, $password, $dbname, $port); 
  42.   
  43. // 检测连接 
  44.  
  45. if ($conn->connect_error) { 
  46.  
  47. die("connect failed: " . $conn->connect_error); 
  48.  
  49. }  
  50.   
  51. $costBegin = microtime(true); 
  52.   
  53. foreach($arr as $item) { 
  54.  
  55. $sql = sprintf("INSERT INTO user_info (name, age) VALUES ( '%s', %d);", $item['name'], (int)$item['age']);  
  56.  
  57. if ($conn->query($sql) === TRUE) { 
  58.  
  59. echo "insert success"; 
  60.  
  61. } else { 
  62.  
  63. echo "Error: " . $sql . "<br>" . $conn->error; 
  64.  
  65. } 
  66.  
  67. } 
  68.   
  69. $costEnd = microtime(true); 
  70.  
  71. $cost = round($costEnd - $costBegin, 3); 
  72.  
  73. var_dump($cost); 
  74.  
  75.  
  76.  
  77. $conn->close(); 

假如说要批量插入大量数据,如果还用for循环的办法插入是没有问题的,只是时间会比较长。对比一下插入少量数据与插入大量数据,使用上面的for循环插入耗费的时间:条数时间(单位:秒)

php实现向mysql批量插入数据

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

mysql里面是可以使用insert语句进行合并插入的,比如

INSERT INTO user_info (name, age) VALUES ('name1', 18), ('name2', 19);表示一次插入两条数据:

  1. $arr = [  
  2.  
  3. [ 
  4.  
  5. 'name' => 'testname1', 
  6.  
  7. 'age' => 18, 
  8.  
  9. ], 
  10.  
  11. [ 
  12.  
  13. 'name' => 'testname2', 
  14.  
  15. 'age' => 19, 
  16.  
  17. ], 
  18.  
  19. [ 
  20.  
  21. 'name' => 'testname3', 
  22.  
  23. 'age' => 18, 
  24.  
  25. ], 
  26.  
  27. // 此处省略 
  28.  
  29. …… 
  30.  
  31. …… 
  32.  
  33. ]; 
  34.  
  35.  
  36.  
  37. $servername = "localhost"; 
  38.  
  39. $port = 3306; 
  40.  
  41. $username = "username"; 
  42.  
  43. $password = "password"; 
  44.  
  45. $dbname = "mytestdb"; 
  46.  
  47.  
  48.  
  49. // 创建连接 
  50.  
  51. $conn = new mysqli($servername, $username, $password, $dbname, $port); 
  52.  
  53.  
  54.  
  55. // 检测连接 
  56.  
  57. if ($conn->connect_error) { 
  58.  
  59. die("connect failed: " . $conn->connect_error); 
  60.  
  61. }  
  62.  
  63.  
  64.  
  65. $costBegin = microtime(true); 
  66.  
  67.  
  68.  
  69. if (!emptyempty($arr)) { 
  70.  
  71. $sql = sprintf("INSERT INTO user_info (name, age) VALUES "); 
  72.  
  73.  
  74.  
  75. foreach($arr as $item) { 
  76.  
  77. $itemStr = '( '; 
  78.  
  79. $itemStr .= sprintf("'%s', %d", $item['name'], (int)$item['age']); 
  80.  
  81. $itemStr .= '),'; 
  82.  
  83. $sql .= $itemStr; 
  84.  
  85. } 
  86.  
  87.  
  88.  
  89. // 去除最后一个逗号,并且加上结束分号 
  90.  
  91. $sql = rtrim($sql, ','); 
  92.  
  93. $sql .= ';'; 
  94.  
  95.  
  96.  
  97. if ($conn->query($sql) === TRUE) { 
  98.  
  99. } else { 
  100.  
  101. echo "Error: " . $sql . "<br>" . $conn->error; 
  102.  
  103. } 
  104.  
  105. } 
  106.  
  107.  
  108.  
  109. $costEnd = microtime(true); 
  110.  
  111. $cost = round($costEnd - $costBegin, 3); 
  112.  
  113. var_dump($cost); 
  114.  
  115.  
  116.  
  117. $conn->close(); 

下面看一下少量数据与大量数据的时间对比。从总体时间上,可以看出insert合并插入比刚才for循环插入节约了很多时间,效果很明显条数时间(单位:秒)

php实现向mysql批量插入数据

如果你觉得数组太大,想要减少sql错误的风险,也可以使用array_chunk将数组切成指定大小的块,然后对每个块进行insert合并插入。

Tags: mysql批量插入数据

分享到: