我的PHP脚本有问题。如果我运行它,加载至少需要5秒钟。这是我的代码:
$informs = " AND `id`!=5198 AND `id`!=14045 AND `id`!=6948 AND `id`!=4414 AND `id`!=4451 AND `id`!=773 AND `id`!=14105 AND `id`!=3191 AND `id`!=14093
AND `id`!=14397 AND `id`!=13319 AND `id`!=4297";
$displayed = array();
for ($i=0;$i<24;$i++) {
$whereClause = "";
//generating additional where clause
foreach($displayed as $d){
$whereClause .= " AND `id`!=".$d;
}
$random = rand(1,100000);
if ($random >= 1 && $random <= 2) {
//pele
$sql = "SELECT * FROM `players_db` WHERE `revision`=12 AND `id`=4570";
}
else if ($random >= 3 && $random <= 6) {
//revision 12 (no pele)
$sql = "SELECT * FROM `players_db` WHERE `revision`=12 AND `id`!=4570 ORDER BY RAND() LIMIT 1";
}
else if ($random >= 7 && $random <= 10) {
//messi, ronaldo
$sql = "SELECT * FROM `players_db` WHERE `id`=186 OR `id`=1574 ORDER BY RAND() LIMIT 1";
}
else if ($random >= 11 && $random <= 59) {
//revision TOTW
$sql = "SELECT * FROM `players_db` WHERE `TOTW`=30 ORDER BY RAND() LIMIT 1";
}
else if ($random >= 60 && $random <= 129) {
//PA Coin
$sql = "UPDATE users SET balance = balance + 1 WHERE username = '" . $usernamez . "'";
$result = $conn->query($sql);
$sql = "SELECT * FROM `players_db` WHERE `revision`=98 ";
}
else if ($random >= 130 && $random <= 144) {
//revision 0 and price BETWEEN >250000 AND <650000
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 250000 AND 650000$informs". $whereClause ." ORDER BY RAND() LIMIT 1";
}
else if ($random >= 145 && $random <= 164) {
//revision 0 and price >149000 AND <250000
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 150000 AND 250000$informs". $whereClause ." ORDER BY RAND() LIMIT 1";
}
else if ($random >= 165 && $random <= 189) {
//revision 0 and price >49000 AND <150000
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 50000 AND 150000$informs". $whereClause ." ORDER BY RAND() LIMIT 1";
}
else if ($random >= 190 && $random <= 219) {
//revision 0 and price >24000 AND <50000
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 25000 AND 50000$informs". $whereClause ." ORDER BY RAND() LIMIT 1";
}
else if ($random >= 220 && $random <= 100000) {
//revision 0 and price < 25000
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` < 25000$informs". $whereClause ." ORDER BY RAND() LIMIT 1";
}
$result = $conn->query($sql);
$row = mysqli_fetch_array($result);
// add displayed data set to array
$result4 = $conn->query($sql);
while($row4=mysqli_fetch_array($result4)){
$displayed[] = $row['id'];
}
你看到的是生成一个随机数,然后它会查看它所属的类别,然后它会选择一个sql查询并添加所选择的id&#39;到一个数组,所以它不能被选中两次。 我希望有人可以帮助我改进我的剧本。
谢谢!
答案 0 :(得分:-1)
“脚本”不是什么运行缓慢...把mysql从等式中取出来它会运行得很好。
echo 'Starting at: ' . microtime(true) . '<br>';
$informs = " AND `id`!=5198 AND `id`!=14045 AND `id`!=6948 AND `id`!=4414 AND `id`!=4451 AND `id`!=773 AND `id`!=14105 AND `id`!=3191 AND `id`!=14093 AND `id`!=14397 AND `id`!=13319 AND `id`!=4297";
$displayed = array();
$usernamez = 'Some user';
for ($i = 0; $i < 24; $i++) {
$whereClause = "";
foreach ($displayed as $d) {
$whereClause .= " AND `id`!=" . $d;
}
$random = rand(1, 100000);
if ($random >= 1 && $random <= 2) {
$sql = "SELECT * FROM `players_db` WHERE `revision`=12 AND `id`=4570";
} else if ($random >= 3 && $random <= 6) {
$sql = "SELECT * FROM `players_db` WHERE `revision`=12 AND `id`!=4570 ORDER BY RAND() LIMIT 1";
} else if ($random >= 7 && $random <= 10) {
$sql = "SELECT * FROM `players_db` WHERE `id`=186 OR `id`=1574 ORDER BY RAND() LIMIT 1";
} else if ($random >= 11 && $random <= 59) {
$sql = "SELECT * FROM `players_db` WHERE `TOTW`=30 ORDER BY RAND() LIMIT 1";
} else if ($random >= 60 && $random <= 129) {
$sql = "UPDATE users SET balance = balance + 1 WHERE username = '" . $usernamez . "'";
$sql = "SELECT * FROM `players_db` WHERE `revision`=98 ";
} else if ($random >= 130 && $random <= 144) {
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 250000 AND 650000$informs" . $whereClause . " ORDER BY RAND() LIMIT 1";
} else if ($random >= 145 && $random <= 164) {
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 150000 AND 250000$informs" . $whereClause . " ORDER BY RAND() LIMIT 1";
} else if ($random >= 165 && $random <= 189) {
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 50000 AND 150000$informs" . $whereClause . " ORDER BY RAND() LIMIT 1";
} else if ($random >= 190 && $random <= 219) {
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` BETWEEN 25000 AND 50000$informs" . $whereClause . " ORDER BY RAND() LIMIT 1";
} else if ($random >= 220 && $random <= 100000) {
$sql = "SELECT * FROM `players_db` WHERE `winterupgrades`!=2 AND `revision`=0 AND `rating`>74 AND `rare`=2 AND `xb_end_range` < 25000$informs" . $whereClause . " ORDER BY RAND() LIMIT 1";
}
//echo 'Run # : ' . $i . var_export($sql, true) . '<br>';
}
echo 'Finishing at : ' . microtime(true).'<br>';
结果是:
Starting at: 1461782077.4058
Finishing at : 1461782077.4058
使用microtime()
单独确认您的查询,以准确确定哪一个正在减慢您的速度,然后我们可以从那里开始。