Jump to content

which is more efficient?


perky416

Recommended Posts

Hi everyone,

 

Please could somebody give me their views on which is a more efficient and faster way to update multiple rows of a database with different values?

 

Using a query within a while loop or using a query with a case?

 

Thanks

Link to comment
Share on other sites

Hi mate.

 

Even though with a while loop a new query is executed for each row? but with case only one query is executed for all rows? A while is still more efficient and faster?

 

Just trying to understand.

 

Thanks mate.

Link to comment
Share on other sites

The code im using is similar to the following:

 

Query within a while loop:

 

while($row = mysql_fetch_assoc($query)){
mysql_query("UPDATE products SET price='$_POST['price'][$i] WHERE product='$_POST['checkbox_value']'");
$i++;
}

 

single query using case:

 

while($row = mysql_fetch_assoc($query)){
$string .= "WHEN product='" . $_POST['checkbox_value'][$i] . "' THEN ' ". $_POST['price'][$i] . "'";
$i++; }

mysql_query("UPDATE products SET price = CASE $string ELSE price END");

Link to comment
Share on other sites

One query will always be preferable to multiple. But running the query inside the loop causes a ton of overhead, I would even just do this instead of that:

 

while($row = mysql_fetch_assoc($query)){
    $update .= "UPDATE products SET price='$_POST['price'][$i] WHERE product='$_POST['checkbox_value']';\n";
    $i++;
}

mysql_query($update) or trigger_error('Update Failed: ' . mysql_error());

 

As that way, only 1 function call is used and reduces the overhead on the php end of things. But if the CASE works, that would probably be even better than the multiple queries and 1 mysql_query call above.

Link to comment
Share on other sites

Hi everyone,

 

Please could somebody give me their views on which is a more efficient and faster way to update multiple rows of a database with different values?

 

Using a query within a while loop or using a query with a case?

 

Thanks

 

Why would you want to use a loop when using SQL UPDATE?

 

The whole point with SQL UPDATE is not to use loops anymore, because the DBMS will take care of it for you.

 

Of course, there are cases, that it is necessary to use loops.

 

But still, use UPDATE, use CASE WHEN in the SET clause if necessary, and refine your WHERE clause. That ought to do it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.