[英]Whats the best way to generate large amounts of unique promo codes?
我正在嘗試大批量創建促銷代碼(使用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個代碼加載到內存中。 只需在數據庫中創建一個表,添加code
唯一字段(主鍵或唯一索引)並插入隨機代碼,直到您有30,000個成功插入。
具體是什么問題?
我的建議是:不要以CSV格式存儲代碼,而是創建一個新的索引列並將每個代碼存儲在自己的行中 - 同樣,使用准備好的查詢。
在~250 KB字符串上執行60,000個strpos()
可能不是最好的想法......
如果你不想在循環中插入(它們也很昂貴),請使用數組和方法in_array來檢查字符串。 查看in_array函數的注釋,有人說你可以使用array_flip獲得更好的性能,然后檢查數組鍵
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.