how can I improve the following code to import a CSV file of 75,000 data faster. What I am doing is comparing data from the csv with the data from mysql.
<?php
require('config.php');
$tipo = $_FILES['dataCliente']['type'];
$tamanio = $_FILES['dataCliente']['size'];
$archivotmp = $_FILES['dataCliente']['tmp_name'];
$lineas = file($archivotmp);
$i = 0;
foreach ($lineas as $linea) {
$cantidad_registros = count($lineas);
$cantidad_regist_agregados = ($cantidad_registros - 1);
if ($i != 0) {
$datos = explode("|", $linea);
$codigo_inmueble = !empty($datos[0]) ? ($datos[0]) : '';
$ruta = !empty($datos[1]) ? ($datos[1]) : '';
if( !empty($codigo_inmueble) ){
$sqlVerificarExistencia = ("SELECT codigo_inmueble FROM serviciosglforjson WHERE codigo_inmueble='".($codigo_inmueble)."' ");
$queryDuplicidad = mysqli_query($con, $sqlVerificarExistencia);
$cantidadDuplicidad = mysqli_num_rows($queryDuplicidad);
if ( $cantidadDuplicidad == 0 ) {
$insertarRegistro = ("INSERT INTO serviciosglforjson(ruta)
SELECT ruta
FROM serviciosglforjson WHERE codigo_inmueble = '$codigo_inmueble';");
mysqli_query($con, $insertarRegistro);
} else{
$updateData = ("UPDATE serviciosglforjson SET
ruta='" .$ruta. "'
WHERE codigo_inmueble='".$codigo_inmueble."'
");
$resultadoUpdate = mysqli_query($con, $updateData);
}
} //Cierre de mi 2 If
} //Cierre de mi 1 If
echo '<center><div>'. $i. "). " .$linea.'</div></center>';
$i++;
}
echo '<center><p style="text-aling:center; color:#333;">Total de Registros: '. $cantidad_regist_agregados .'</p></center>';
echo "<center><a href='index.php'>Atras</a></center>";
?>
For now it takes more than 2 hours to import. I need your help, please. I need to use another method to be able to improve the code and that the import of data in the CSV is fast.