快速插入会导致数据丢失

时间:2012-05-28 12:35:36

标签: php mysql

我有一个与mysql和php相关的奇怪错误。我想知道它是否也代表我们服务器上的性能问题。

我有一个用来管理回扣促销代码的课程。代码很棒,工作正常,并不完全符合预期。 saveChanges()操作根据对象的状态发送INSERT或UPDATE,并且在当前上下文中只会插入因为我试图生成优惠券代码。

classe的saveChanges是这样的:(我知道,我不应该使用旧的mysql,但由于软件的架构限制,我别无选择,所以请不要抱怨那部分)

public function saveChanges($asNew = false){

    //Get the connection
    global $conn_panier;

    //Check if the rebate still exists
    if($this->isNew() || $asNew){

        //Check unicity if new
        if(reset(mysql_fetch_assoc(mysql_query('SELECT COUNT(*) FROM panier_rabais_codes WHERE code_coupon = "'.mysql_real_escape_string($this->getCouponCode(), $conn_panier).'"', $conn_panier))) > 0){
            throw new Activis_Catalog_Models_Exceptions_ValidationException('Coupon code "'.$this->getCouponCode().'" already exists in the system', $this, __METHOD__, $this->getCouponCode());
        }

        //Update the existing rebate
        mysql_query($q = 'INSERT INTO panier_rabais_codes 
                        (
                            `no_rabais`,
                            `code_coupon`,
                            `utilisation`,
                            `date_verrou`
                        )VALUES(
                            '.$this->getRebate()->getId().',
                            "'.mysql_real_escape_string(stripslashes($this->getCouponCode()), $conn_panier).'",
                            '.$this->getCodeUsage().',
                            "'.($this->getInvalidityDate() === NULL ? '0000-00-00 00:00:00' : date('Y-m-d G:i:s', strtotime($this->getInvalidityDate()))).'"
                        )', $conn_panier);
        return (mysql_affected_rows($conn_panier) >= 1);

    }else{

        //Update the existing rebate
        mysql_query('UPDATE panier_rabais_codes
                        SET
                            `utilisation` = '.$this->getCodeUsage().',
                            `date_verrou` = "'.($this->getInvalidityDate() === NULL ? '0000-00-00 00:00:00' : date('Y-m-d G:i:s', strtotime($this->getInvalidityDate()))).'"
                        WHERE 
                            no_rabais = '.$this->getRebate()->getId().' AND code_coupon = "'.mysql_real_escape_string($this->getCouponCode(), $conn_panier).'"', $conn_panier);
        return (mysql_affected_rows($conn_panier) >= 0);

    }

}

正如您所看到的,代码本身非常简单和干净,如果插入成功则返回true,否则返回false。

代码的另一部分使用随机算法生成代码,如下所示:

while($codes_to_generate > 0){

    //Sleep to delay mysql choking on the input
    usleep(100);

    //Generate a random code
    $code = strtoupper('RC'.$rebate->getId().rand(254852, 975124));
    $code .= strtoupper(substr(md5($code), 0, 1));
    $rebateCode = new Activis_Catalog_Models_RebateCode($rebate);
    $rebateCode->setCouponCode($code);
    $rebateCode->setCodeUsage($_REQUEST['utilisation_generer']);
    try{
        if($rebateCode->saveChanges()){
            $codes_to_generate--;
            $generated_codes[] = $code;
        }
    }catch(Exception $ex){
    }

}

正如你在这里看到的,有两点需要注意。如果从saveChanges返回true,则生成的代码数和生成的代码数组只会被填充,因此mysql必须报告为此部分插入了信息。

另一个花絮是while的第一行:

//Sleep to delay mysql choking on the input
usleep(100);

跆拳道?那么这篇文章就是这个。我的代码完美无缺地生成少量代码。但是,如果我要求mysql同时保存多个代码,我必须使用usleep来限制自己,或者使用mysql删除其中的一些代码。它将报告存在受影响的行但未保存它们。

在100行以下,我不需要节流,然后我需要睡觉,具体取决于要插入的行数。它必须是简单的东西,但我不知道是什么。以下是我尝试插入的线和我必须实现的最小usleep油门的总和:

  • < 100行:无
  • < 300行:2毫秒
  • < 1000行:5毫秒
  • < 2000行:10毫秒
  • < 5000行:20毫秒
  • < 10000行:100毫秒

感谢您的时间

1 个答案:

答案 0 :(得分:2)

您确定所有代码都已插入且未更新,因为更新非现有行不会执行任何操作。

相关问题