gpt4 book ai didi

php - 生成大量唯一促销代码的最佳方式是什么?

转载 作者:可可西里 更新时间:2023-11-01 07:54:47 24 4
gpt4 key购买 nike

我正在尝试大批量创建促销代码(使用 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的batches的时候,好像还可以。但是客户端希望能够一次生成 30k。当我将循环提高到 30k 时,我遇到了问题。我是否可以进行任何明显的性能调整,或者我可以采用不同的方式来执行此操作?

最佳答案

您可能不需要将所有 30,000 个代码都加载到单个巨大字符串中的内存中。只需在您的数据库中创建一个表,添加一个 code 唯一字段(主键或唯一索引)并插入随机代码,直到您有 30,000 次成功插入。

关于php - 生成大量唯一促销代码的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4737327/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com