商品促销php设计,php – 什么是生成大量独特促销代码的最佳方式?

我正在尝试大批量创建促销代码(使用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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值