Home     Articles & Projects     Products & Web Services     Forum

MySQL Question

Hi David,

I was wondering if this little script is the most efficient way of checking if a particular handset has any deals associated with it. It works, just seems to take ages.

<?php
$sql
= "SELECT hs_id FROM `".DB_PREFIX."handsets`";
database_querySelect($sql,$rows);

foreach(
$rows as $deal) {
$sql = "SELECT hs_id FROM `".DB_PREFIX."deals` WHERE hs_id = '".$deal["hs_id"]."' LIMIT 1";
    if (
database_querySelect($sql,$rows)) {
   
$sql = "UPDATE `".DB_PREFIX."handsets` SET hs_deals = '1' WHERE hs_id='".$deal["hs_id"]."'";
   
database_queryModify($sql,$insertId);
    }
}
?>

Thanks,
Simon

Hi Simon, Logically

Hi Simon,

Logically speaking, it is as efficient as it could be - even if you wanted to get fancy with SQL and combine statements the database would have to go through the same process internally.

However, from a query point of view, it is worth checking that hs_deals.hs_id is indexed, as this is used in a WHERE clause and if not, that query would require a full table scan. If that's not the case, add an index to the hs_id field of the hs_deals table and that should speed things up...

Cheers!
David.

Thanks David, That made a

Thanks David,

That made a huge difference.

Simon
Compare Electricals