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

不言
Release: 2023-02-28 16:18:02
Original
1505 people have browsed it


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分割。
Copy after login

2.在mysql中运行代码代码

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


3.运行结果(12秒)

[SQL] LOAD DATA local INFILE &#39;c:/insert.sql&#39; INTO TABLE `cache`(`id`, `name`);
受影响的行: 1000000时间: 12.855ms
Copy after login
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!