0

The query takes forever to update all Domains in the second table.

Maybe there is a more efficient way to approach this?

$mysqli = new mysqli("localhost","xxxx","xxxx","xxx");

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

// Perform queries
$result = $mysqli->query(SELECT DISTINCT dom FROM pmcts_mt_pipeline_details);

$rows = $result->fetch_all(MYSQLI_ASSOC);
foreach ($rows as $row) {
$dom = $row['dom'];
$mysqli->query("UPDATE pmcts_email_marketing_companies_pharma_all_unique SET id_ind_mailer=1 WHERE email_dom='$dom' AND (mailerdaemon=0 AND mail_excluded=0 AND unsubscribe=0 AND changeto=0)");
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 1
    What have you tried to resolve the problem? Why do you need to read all columns and run a seperate `UPDATE` query? Why not do all that directly with one query? – Nico Haase Jun 23 '21 at 09:18
  • 1
    It seems that an [UPDATE from SELECT](https://stackoverflow.com/questions/1262786/mysql-update-query-based-on-select-query) should solve your problem. – Gabriel Heming Jun 23 '21 at 09:38
  • I even loop to 40.000 Domains, Brilliant! – Jackwiz007 Jun 23 '21 at 16:45
  • @NicoHaase: I Used the tip from Martijn. I Only used the suggestion with a small modification. Not using the foreach Loop which was the cause for slowing it down drasticly. – Jackwiz007 Jun 23 '21 at 16:55

1 Answers1

0

It's likely not the query that's slow, but the loop. Looping in PHP is slow.

You want to update one value to all matching rows, you might be able to use IN():

$result = $mysqli->query(SELECT DISTINCT dom FROM pmcts_mt_pipeline_details);
$rows = $result->fetch_all(MYSQLI_ASSOC);
$doms = array_column($rows, 'dom'); // *
$domsString = implode("','", $doms)

* Im not 100% about the array column, but the goals is to get all values-only in an array

UPDATE pmcts_email_marketing_companies_pharma_all_unique 
SET id_ind_mailer=1 
WHERE email_dom IN ('".$domsString."') 
   AND (
      mailerdaemon=0 
      AND mail_excluded=0 
      AND unsubscribe=0
      AND changeto=0
   )

Note: Im assuming you are in control of the $dom-values and that they're clean and safe for mysql. If they're able to be manipulated by user input, please make sure you sanatize, or even better: use prepared statements.

Martijn
  • 15,791
  • 4
  • 36
  • 68