晚上好,我有一个400mb的csv文件,我必须在mysql数据库中加载它。 csv文件是“不规则的”,因为它包含如下信息:
user|email|password\n
user|password|otherdata\n
我在php-cli中创建了一个脚本来逐行读取文件并获取我需要的信息:用户名和密码。 l'用户名始终是第一条记录。要知道密码的长度是什么?
我在五个小时前运行该脚本,但仍未完成加载数据库中的所有数据。
如何改善此脚本的性能?
<?php
$fileHandle = fopen("C:/Users/AT/Documents/Backup/forumusers.csv", "r");
$mysqlHandle = mysql_connect("localhost", "root", "");
mysql_select_db("testbackupboard");
while(!feof($fileHandle))
{
$fileRow = fgets($fileHandle);
$line2Record = explode("|", $fileRow);
foreach ($line2Record as $rowRecord)
{
if (strlen($rowRecord) == 40)
{
$datatoMysql[0] = $rowRecord; // password hash
}
}
$datatoMysql[1] = $line2Record[0]; // username
$execQuery = mysql_query("INSERT INTO forumusers (username, hash) VALUES ('".mysql_real_escape_string(utf8_encode($datatoMysql[1]))."', '".mysql_real_escape_string(utf8_encode($datatoMysql[0]))."')");
if($execQuery)
{
print"Record ".$rowRecord[1]." ".$rowRecord[0]." loaded into db\n";
}
else
{
die(mysql_error());
}
}
fclose($fileHandle);
?>
答案 0 :(得分:0)
fgetcsv()?
怎么样?$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "|")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . "<br />\n";
}
}
fclose($handle);
}
答案 1 :(得分:0)
CREATE TABLE tmp
有4列:id
AUTO_INCREMENT,user
,col2
,col3
。这不是决赛桌。LOAD DATA
CREATE TABLE final (user, email, password, otherdata, PRIMARY KEY(user))
INSERT INTO final (user, email, password, otherdata) SELECT a.user, a.col2 AS email, a.col3 AS password, b.col3 AS otherdata FROM tmp AS a JOIN tmp AS b ON b.id = a.id+1 WHERE (a.id % 2) = 1;
(警告:如果
auto_increment_increment
不是1,这将不起作用,例如在基于Galera的群集上。相反,您需要@variable
来进行排序。)