I need help to speed up this code. It works, but is really slow (updates 2 items/second). Any tips on how to speed it up?
The script functions like this:
- Break up an email to username and domain
- If not already in database, add username to username table and the domain to domain table
- Add the username ID and domain ID to the email table (i.e. "username.ID 1"@"domain.ID 1")
- Same thing with password
- Add a relationship to the username, domain, email and password to each other and to the source table
- Delete the old row
- Print each email/password
- Print final results
My MySQL database layout:
- domain: ID - domain (i.e. hotmail.com)
- username: ID - username (i.e. myname)
- email: ID - username - domain (i.e. username.ID - domain.ID)
- source: ID - source (i.e. old_userlist)
- relationships: ID - username - email - password - source
- old_userlist: ID - email - password
//conn
$servername = "xx";
$username = "xx";
$password = "xx";
$dbname = "xx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// List all email
$sql = "SELECT * FROM old_userlist ORDER BY id ASC LIMIT 1000";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Get id of original row
$original_email_id = $row["ID"];
//count
$domain_name_total_count = $domain_name_total_count + 1;
$password_total_count = $password_total_count + 1;
$username_id = 0;
$domain_id = 0;
$email_id = 0;
$row_email = $row["email"];
$row_password = $row["password"];
//break the string up around the "@" character in $row_email
$row_email_parts = explode("@",$row_email);
$row_email_username = $row_email_parts['0'];
$row_email_domain = $row_email_parts['1'];
// ############ USERNAME
//check if domain name exists in the database
$sql = "SELECT * FROM username WHERE username LIKE ('$row_email_username')";
$result_emailadress = $conn->query($sql);
if ($result_emailadress->num_rows > 0) {
//count duplicates
$username_duplicate_count = $username_duplicate_count + 1;
//get usernamne id
while($row_username = $result_emailadress->fetch_assoc()) {
$username_id = $row_username["ID"];
}
} else {
//count new
$username_new_count = $username_new_count + 1;
//Add domains to database
$sql= "INSERT INTO username (username) VALUES ('$row_email_username')";
$insert = $conn->query($sql);
$username_id = $conn->insert_id;
} // /add username
// ############ DOMAIN
//check if domain name exists in the database
$sql = "SELECT * FROM domain WHERE domain LIKE ('$row_email_domain')";
$result_domain = $conn->query($sql);
if ($result_domain->num_rows > 0) {
//count duplicates
$domain_name_duplicate_count = $domain_name_duplicate_count + 1;
//get domain id
while($row_domain = $result_domain->fetch_assoc()) {
$domain_id = $row_domain["ID"];
}
} else {
//count new
$domain_name_new_count = $domain_name_new_count + 1;
//Add domains to database
$sql= "INSERT INTO domain (domain) VALUES ('$row_email_domain')";
$insert_domain = $conn->query($sql);
$domain_id = $conn->insert_id;
} // /add domain
// ############ CREATE EMAIL entity
//check if email name exists in the database
$sql = "SELECT * FROM email_address WHERE u LIKE ('$username_id') AND d LIKE ('$domain_id')";
$result_emailadress = $conn->query($sql);
if ($result_emailadress->num_rows > 0) {
//count duplicates
$email_adress_duplicate_count = $email_adress_duplicate_count + 1;
while($row_email_new = $result_emailadress->fetch_assoc()) {
$email_id = $row_email_new["ID"];
}
} else {
//count new
$email_adress_new_count = $email_adress_new_count + 1;
//Add email to database
$sql= "INSERT INTO email_address (u, d) VALUES ('$username_id','$domain_id')";
$insert_email = $conn->query($sql);
$email_id = $conn->insert_id;
} // /add email
// ############ CREATE PASSWORD entity
//check if email name exists in the database
$sql = "SELECT * FROM password WHERE p LIKE ('$row_password')";
$result_password = $conn->query($sql);
if ($result_password->num_rows > 0) {
//count duplicates
$password_duplicate_count = $password_duplicate_count + 1;
while($row_password_new = $result_password->fetch_assoc()) {
$password_id = $row_password_new["ID"];
}
} else {
//count new
$password_new_count = $password_new_count + 1;
//Add email to database
$sql= "INSERT INTO password (p) VALUES ('$row_password')";
$insert_password = $conn->query($sql);
$password_id = $conn->insert_id;
} // /password
// // ############ CREATE relationship
$source_id = "1"; // old_userlist
//check if username connected to source.
$sql = "SELECT * FROM relationship WHERE u LIKE ('$username_id') AND s LIKE ('$source_id')";
$result_relationship_user_source = $conn->query($sql);
if ($result_relationship_user_source->num_rows > 0) {
//count duplicates
$relationship_user_source_duplicate_count = $relationship_user_source_duplicate_count + 1;
} else {
//count new
$relationship_user_source_new_count = $relationship_user_source_new_count + 1;
//Add email to database
$sql= "INSERT INTO relationship (u,s) VALUES ('$username_id','$source_id')";
$insert_relationship_user_source = $conn->query($sql);
} // /relationship username-source
//check if email connected to source.
$sql = "SELECT * FROM relationship WHERE e LIKE ('$email_id') AND s LIKE ('$source_id')";
$result_relationship_email_source = $conn->query($sql);
if ($result_relationship_email_source->num_rows > 0) {
//count duplicates
$relationship_email_source_duplicate_count = $relationship_email_source_duplicate_count + 1;
} else {
//count new
$relationship_email_source_new_count = $relationship_email_source_new_count + 1;
//Add email to database
$sql= "INSERT INTO relationship (e,s) VALUES ('$email_id','$source_id')";
$insert_relationship_email_source = $conn->query($sql);
} // /relationship username-source
//check if password connected to source.
$sql = "SELECT * FROM relationship WHERE p LIKE ('$password_id') AND s LIKE ('$source_id')";
$result_relationship_password_source = $conn->query($sql);
if ($result_relationship_password_source->num_rows > 0) {
//count duplicates
$relationship_password_source_duplicate_count = $relationship_password_source_duplicate_count + 1;
} else {
//count new
$relationship_password_source_new_count = $relationship_password_source_new_count + 1;
//Add email to database
$sql= "INSERT INTO relationship (p,s) VALUES ('$password_id','$source_id')";
$insert_relationship_email_source = $conn->query($sql);
} // /relationship password-source
//check if email connected to password.
$sql = "SELECT * FROM relationship WHERE p LIKE ('$password_id') AND e LIKE ('$email_id')";
$result_relationship_email_password = $conn->query($sql);
if ($result_relationship_email_password->num_rows > 0) {
//count duplicates
$relationship_email_password_duplicate_count = $relationship_email_password_duplicate_count + 1;
} else {
//count new
$relationship_email_password_new_count = $relationship_email_password_new_count + 1;
//Add email to database
$sql= "INSERT INTO relationship (p,e) VALUES ('$password_id','$email_id')";
$insert_relationship_email_password = $conn->query($sql);
} // /relationship email-password
// ############ DELETE ORIGINAL entity
$sql= "DELETE FROM old_userlist WHERE id = '$original_email_id'";
$delete_original_email = $conn->query($sql);
// ############ PRINT email managed
echo $domain_name_total_count." - ".$row_email_username."@".$row_email_domain."/".$row_password."\n";
}
} else {
echo "0 results";
}
//list results
echo "\n\n";
echo "Imported ".$domain_name_total_count." emails and ";
echo $password_total_count." passwords\n";
echo $username_new_count." new username entires of which ";
echo $username_duplicate_count." where duplicate.\n";
echo $domain_name_new_count." new domain entires of which ";
echo $domain_name_duplicate_count." where duplicate.\n";
echo $email_adress_new_count." new email address entires of which ";
echo $email_adress_duplicate_count." where duplicate.\n";
echo $password_new_count." new password entires of which ";
echo $password_duplicate_count." where duplicate.\n";
echo $relationship_user_source_new_count." new relationship entires (user/source) of which ";
echo $relationship_user_source_duplicate_count." where duplicate.\n";
echo $relationship_email_source_new_count." new relationship entires (email adress/source) of which ";
echo $relationship_email_source_duplicate_count." where duplicate.\n";
echo $relationship_password_source_new_count." new relationship entires (password/source) of which ";
echo $relationship_password_source_duplicate_count." where duplicate.\n";
echo $relationship_email_password_new_count." new relationship entires (email/password) of which ";
echo $relationship_email_password_duplicate_count." where duplicate.\n";
$conn->close();
count
..select count(*) from table
is faster than aselect *
and an incrementing php loop. In fact, even using PHP'scount
function is faster. \$\endgroup\$