我正在尝试大批量创建促销代码(使用php / mysql).
目前我的代码看起来像这样:
$CurrentCodesInMyDB = "asdfasdf,asdfsdfx"; // this is a giant comma delimited string of the current promo codes in the database.
$PromoCodes = "";
for($i=0;$i<=30000;$i++)
{
$NewCode = GetNewCode($PromoCodes, $CurrentCodesInMyDB );
$PromoCodes .= $NewCode . ","; //this string gets used to allow them to download a txt file
//insert $newcode into database here
}
function GetNewCode($CurrentList, $ExistingList)
{
$NewPromo = GetRandomString();
if(strpos($CurrentList, $NewPromo) === false && strpos($ExistingList, $NewPromo) === false)
{
return $NewPromo;
}
else
{
return GetNewCode($CurrentList, $ExistingList);
}
}
function GetRandomString()
{
return "xc34cv87"; //some random 8 character alphanumeric string
}
当我在10k批量生产时,似乎没问题.但客户希望能够一次产生30k.当我将循环撞到30k时,我一直遇到问题.我可以做出任何明显的性能调整,或者我可以采用不同的方式吗?
解决方法:
您可能不需要在一个巨大的字符串中将所有30,000个代码加载到内存中.只需在数据库中创建一个表,添加代码唯一字段(主键或唯一索引)并插入随机代码,直到您有30,000个成功插入.
标签:php,performance,mysql
来源: https://codeday.me/bug/20190730/1580097.html