我在SQL数据库上有2个请求。加载整个脚本/页面需要很长时间。有更好(更快)的方式吗?我的代码看起来像这样
.state('error', {
url: "/page-not-found",
templateUrl: "templates/error.html",
controller: "errorController"
})
我有一个围绕回声数据的表。如果有人能给我一个小费,我会很棒。
答案 0 :(得分:2)
首先,如果表格有关系,我建议您使用join一次调用数据。
SELECT * FROM table_a a
LEFT JOIN tabl_b b ON a.user_name = b.user_name
WHERE a.state = '0'
第二,为了加速你可以将INDEX放在WHERE子句中使用的两个字段上的东西 - state,user_name
ALTER TABLE `table_a` ADD INDEX `state` (`state`)
ALTER TABLE `tabl_b` ADD INDEX `user_name` (`user_name`)
答案 1 :(得分:0)
当涉及加载时间时,我们应该只从表中获取所需的那些列,因此在第一个查询中我们可以获取user_name,ID,company,PL_read,PL_total,country,EDIT
< / LI>由于在循环内运行的查询也需要时间,所以要避免在代码下面使用
$dataArray = array();
$userName = NULL;
$abfrage = "SELECT ID, user_name, company, PL_read, PL_total, country, EDIT FROM table_a WHERE state = '0' ORDER BY EDIT DESC";
$stmt = $pdo->query($abfrage);
foreach($stmt as $data) {
//creating a string
if (is_null($userName))
{
$userName = "'" . $data['user_name'] . "'";
}
else
{
$userName .= ",'" . $data['user_name'] . "'";
}
$dataArray[$userName] = $data;
}
$stmt = $pdo->query("SELECT follower, user_name FROM tabl_b WHERE user_name IN ($userName) ORDER BY follower");
foreach($stmt as $data) {
if(isset($dataArray[$userName]))
{
$dataArray[$userName]['follower'] = $data['follower'];
}
else
{
$dataArray[$userName]['follower'] = 'N/A';
}
}
foreach($dataArray as $data) {
echo $data['ID'];?>
<?php echo $data['ID'];?>
<?php if ($data['company'] == '') { ?>
<a href="https://open.spotify.com/user/<?php echo $data['user_name'];?>" target="_blank"><?php if ($data['state'] == '3') { echo "<span style=\"color:#0000FF\";>".$data['user_name']."</span>"; } else { echo $data['user_name'];}?></a>
<?php } else {?>
<a href="https://open.spotify.com/user/<?php echo $data['user_name'];?>" target="_blank"><?php if ($data['state'] == '3') { echo "<span style=\"color:#0000FF\";>".$data['company']."</span>"; } else { echo $data['company'];}?></a>
<?php } ?>
<?php echo $data['PL_read'];?>
<?php echo $data['PL_total'];?>
<?php echo $data['country'];?>
<?php echo $data['EDIT'];?>
<?php echo $pl['follower'];?>
<?php } ?>