Home >Backend Development >PHP Tutorial >php+mysql中迅速插入百万条测试数据的方法(12s)

php+mysql中迅速插入百万条测试数据的方法(12s)

不言
不言Original
2018-05-17 14:14:341592browse


1.php代码

<?php 
$t=mktime(); 
set_time_limit(1000); 
$myFile="c:/insert.sql"; 
$fhandler=fopen($myFile,&#39;wb&#39;); 
if($fhandler){ 
$i=0; 
while($i<1000000)//1,0000,000 { 
$i++; 
$sql="$i\t&#39;bb&#39;"; 
fwrite($fhandler,$sql."\r\n"); } 
echo"写入成功,耗时:",mktime()-$t; 
} //注意字段不再以逗号分割,以\t分割,条记录以\r\n分割。

2.在mysql中运行代码代码

LOAD DATA local INFILE &#39;c:/insert.sql&#39; INTO TABLE `cache`(`id`, `name`);

 
 3.运行结果(12秒)  

[SQL] LOAD DATA local INFILE &#39;c:/insert.sql&#39; INTO TABLE `cache`(`id`, `name`);
受影响的行: 1000000时间: 12.855ms
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn