I have only 1 table with 1 million rows that i need to update. The data what I need to update is in a file.txt and its only 1 value to update in 1 million rows.
I use PHP to read file and make the Update.
The problem is the time. Is so slow. Any idea to improve and make more fast?
What I have is:
UPDATE productos SET urlRedirect = getChangeProducto WHERE url LIKE 'getUrlProducto'
TXT file, 2 values separated by (;) first are getChangeProducto and second getUrlProducto:
https://www.example.com/guess/8595651?o=34&t=direct&f=102.301&ord=111;https://example.com/p/guess-jeans-116-175-cm-327554`
PHP FILE:
$mysqli = get_dbc();
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (".$mysqli->connect_errno.") ".$mysqli->connect_error;
exit();
}
$mysqli->set_charset("utf8");
if (!$mysqli) exit(mysqli_connect_error());
$handle = fopen("fileToUpdate.txt", "r");
$total_lines = 0;
if ($handle) {
$line = fgets($handle);
while ($line !== false) {
$url_to_delete = trim($line);
$trozos = explode(";", $url_to_delete);
$total_lines++;
if ($stmt = $mysqli->prepare("UPDATE productos SET urlRedirect = ? WHERE url = ? ")) {
$stmt->bind_param('ss', $trozos[1],$trozos[0]);
$stmt->execute(); // Execute the prepared query.
}
$stmt->close();
$line = fgets($handle);
}
fclose($handle);
}
// Output — Total Lines: 6305.
echo "Total Lines: $total_lines.\n";