Just curious to know- if we need to do a conditional Update in a large table, then which is the best approach-
Directly doing an Update or check for existing entry before Update.
function doDirectUpdate()
{
// UPDATE table WHERE condn
}
OR
function doCheckAndUpdate()
{
// SELECT COUNT(id) AS exist FROM table WHERE condn
if(id exists)
{
// UPDATE table WHERE condn
}
else
{
echo 'No matching entry';
}
}
One should not perform both a
SELECT, then a conditionalUPDATE, simply to display the number of matching rows — or lack of matching rows. The only time that you should perform theSELECTis if you have other logic that states to not update if there are more than X matching rows.UPDATEreturns the number of rows that were updated, and therefor matched. You should take the return value, fromUPDATE, then alert if there are no matching entries.reference: http://dev.mysql.com/doc/refman/5.0/en/update.html#id844302