开发者

Can these queries be turned into a REPLACE INTO?

$sql2 = "SELECT `id` FROM `saa_game` WHERE `domain_id` = '".$row['id']."' AND `unique_id` = '".s($oGame->unique_id)."' AND `year` = '".$iYear."' AND `month` = '".$iMonth."' LIMIT 1";
$result2 = mysql_query($sql2) or die(mail('rpaiva@golevel.com','SAA Gather Error',mysql_error()));
if(mysql_num_rows($result2) == 1)
{
    $row = mysql_fetch_assoc($result2);
    $sql3 = "UPDATE `saa_game` SET `plays` = '".s($o开发者_如何学编程Game->plays)."' WHERE `id` = '".$row['id']."' LIMIT 1";
    $result3 = mysql_query($sql3) or die(mail('email@sample.com','SAA Gather Error',mysql_error()));
}
else
{
    $sql3 = "INSERT INTO `saa_game` (`domain_id`,`type`,`source`,`unique_id`,`plays`,`year`,`month`) VALUES ('".$row['id']."','".s($oGame->type)."','".s($oGame->source)."','".s($oGame->unique_id)."','".s($oGame->plays)."','".$iYear."','".$iMonth."')";
    $result3 = mysql_query($sql3) or die(mail('email@sample.com','SAA Gather Error',mysql_error()));
}

I've got this set of queries running 40,000 times on a single page load on a cron job every 10 minutes. This takes so long that it almost runs into the next cron job. If I could reduce this into one query instead of two, that'd be great. (as long as there will actually be a performance difference)


If the select hits all of the right indexes, there won't be a performance increase. In fact, it's likely to be worse! MySQL's REPLACE INTO is implemented as a delete, then an insert!

Consider MySQL's INSERT INTO ... ON DUPLICATE KEY UPDATE instead.

Keep in mind that both of these options are exclusive to MySQL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜