我正在使用Wamp在笔记本电脑上进行项目开发。我大约有17000行和6列数据要添加到数据库的4个不同表中。这是通过上传excel文件并单击导入按钮来完成的。注意!我正在使用excel到mysql插件,这对于项目是必需的,因为它是程序的功能之一。通常,表中不会插入太多数据。一切正常,尽管需要很长时间。我只是担心在上传过程中无法访问整个网站。以下是上传脚本。有什么技巧可以改善下面的脚本,从而加快插入速度并使插入繁忙时可访问网站?
<?php
$conn = mysqli_connect("localhost","root","","eftposcentral");
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"]))
{
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row)
{
$po = "";
if(isset($Row[0])) {
$po = mysqli_real_escape_string($conn,$Row[0]);
}
$business = "";
if(isset($Row[1])) {
$business = mysqli_real_escape_string($conn,$Row[1]);
}
$model = "";
if(isset($Row[2])) {
$model = mysqli_real_escape_string($conn,$Row[2]);
}
$serial = "";
if(isset($Row[3])) {
$serial = mysqli_real_escape_string($conn,$Row[3]);
}
$freight = "";
if(isset($Row[4])) {
$freight = mysqli_real_escape_string($conn,$Row[4]);
}
$depreciation = "";
if(isset($Row[5])) {
$depreciation = mysqli_real_escape_string($conn,$Row[5]);
}
$date_rec = "";
if(isset($Row[6])) {
$date_rec = mysqli_real_escape_string($conn,$Row[6]);
}
$raw_results = mysqli_query($conn,"SELECT serial FROM device_current_info
WHERE (`serial` = $serial)");
$results = mysqli_fetch_array($conn,$raw_results);
if($results > 0){
$type = "error";
$message = "Problem in Importing assets into the Database" .mysqli_error($conn);
}
else{
if (!empty($po) || !empty($model) || !empty($serial) || !empty($freight) || !empty($depreciation)|| !empty($date_rec) ) {
//Adds assets to the terminal_info table.
$query = "insert IGNORE into device_info(po,business,model,serial,freight,depreciation,date_rec) values('".$po."','".$business."','".$model."'
,'".$serial."','".$freight."','".$depreciation."','".$date_rec."')";
$result = mysqli_query($conn, $query)or die(mysqli_error($conn));
if (! empty($result)) {
$type = "success";
$message = "Assets added into the Database";
} else {
$type = "error";
$message = "Problem in Importing assets into the Database" .mysqli_error($conn);
}
}
if (!empty($po) || !empty($model) || !empty($serial) || !empty($freight) || !empty($depreciation)|| !empty($date_rec) ) {
//Adds terminals to the terminal_current_info table. Terminals will be linked on this table and only current info will be stored.
$currenLocation ="Stores"; //Default location for all new assets.
$newComment ="New asset"; //Default comments for all new assets.
$currentStatus = "Available";//Default status for all new assets.
$query2 = "insert IGNORE into device_current_info(business,model,serial,current_status,current_location,comments) values('".$business."','".$model."'
,'".$serial."','".$currentStatus."','".$currenLocation."','".$newComment."')";
$result2 = mysqli_query($conn, $query2) or die(mysqli_error($conn));
if (! empty($result)) {
$type = "success";
$message = "Assets added into the Database";
} else {
$type = "error";
$message = "Problem in Importing assets into the Database" .mysqli_error($conn);
}
}
if (!empty($po) || !empty($model) || !empty($serial) || !empty($freight) || !empty($depreciation)|| !empty($date_rec) ) {
//Creates first terminal movement. Every time a terminal is moved it this table will be updated.
$user = $_SESSION['login_user'];
$previousLocation ="Vendor"; //Default previoius location for all new assets.
$movementLocation ="Stores"; //Default location for all new assets.
$movementComment ="New asset"; //Default comments for all new assets.
$movementStatus ="Available"; //Default status for all new assets.
$query3 = "insert IGNORE into device_movements(serial,previous_location,movement_location,user,status,comments) values(
'".$serial."','".$previousLocation."','".$movementLocation."','".$user."','".$movementStatus."','".$movementComment."')";
$result3 = mysqli_query($conn, $query3) or die(mysqli_error($conn));
$query4 = "insert into activity_log(user_name,activity,old_value,new_value) values(
'".$user."','Added','','".$serial."')";
$result4 = mysqli_query($conn, $query4) or die(mysqli_error($conn));
if (! empty($result)) {
$type = "success";
$message = "Assets added into the Database";
} else {
$type = "error";
$message = "Problem in Importing assets into the Database" .mysqli_error($conn);
}
}
}
}
}
}
}
else
{
$type = "error";
$message = "Invalid File Type. Upload Excel File.";
}
?>
答案 0 :(得分:1)
如果使用SpreadSheet-Plugin将数据传输到csv文件中,则可能会提高速度。
然后,您可以使用LOAD DATA LOCAL INFILE
将带有mysql的csv文件导入。 (请参见MySQL Doku)
这个MySQL Function真的(是,真的)非常快,因此其余的将取决于您的电子表格插件的速度。
但是,当然,这比解决方案更新。
答案 1 :(得分:0)
PHP中有一个名为Gearman的函数,可让您将这些类型的任务包装到并行线程中,以便它们可以与其他任务一起运行。
也许,您可以考虑在进口商中整合此功能,因为代码本身就听起来不错!这里的问题是并行运行另一个任务,对吧?