如何优化和提高此MySQL查询的速度?

时间:2013-10-29 20:20:23

标签: php mysql database

我正在尝试从我的数据库中获取信息并显示它。以下是该页面的截图:

Screenshot of grid

页面加载速度很慢

有没有办法让它加载并更快地显示数据?

这是代码。

<table align="center" class="pTable">
<tr class="main"> <th class="pTableHeader"> ID </th> <th class="pTableHeader"> Room </th> <th class="pTableHeader"> Status</th> </tr>
<?php
gc_enable();
gc_collect_cycles();
flush();
$this->mysql = new sqli('HIDDEN','HIDDEN','HIDDEN','HIDDEN');
$bots = $this->mysql->fetch_array("SELECT `pid`,`room`,`id` from `bots`;");
foreach ($bots as $b) { 
if(is_numeric($b["room"])) {
$xat = file_get_contents('http://www.xat.com/xat'.trim($b["room"]));
$name = sp($xat, '<h1>&nbsp;', '</h1>');
}
else $name = $b['room'];
//$b["pid"] = isset($b["pid"])?$b["pid"]:false;
//$online = \"start.php {$b['id']} {$b['id']}\"", $output) && count($output) > 1 ? true : false;
$online = file_exists("/proc/{$b['pid']}" )?true:false;
if($online === true) $status = "<font color='green'>Online</font>&nbsp;<div style='float:right'><img src='http://fexbots.com/Images/Pawns/online.png'>&nbsp;<a href='/botinfo?botid={$b["id"]}'>More info</a></div>";
else $status = "<font color='red'>Offline</font>&nbsp;<div style='float:right'><img src='http://fexbots.com/Images/Pawns/offline.png'>&nbsp;<a  href='/botinfo?botid={$b["id"]}'>More info</a></div>";
if($b["room"] === '') { 
$name = "No Room"; 
$status = "<font color='red'>Not Setup!</font>&nbsp;&nbsp;<div style='float:right'><img src='http://fexbots.com/Images/Pawns/hang.png'>&nbsp;<a  href='/botinfo?botid={$b["id"]}'>More info</a></div>";
echo '<tr> <td> '.$b["id"].' </td> <td> No Room <td> '.$status.'</td> </tr>';
}
else echo "<tr> <td> ".$b['id']." </td> <td> <a href='http://www.xat.com/$name'>$name</a> <td> ".$status."</td> </tr>";
}
function sp($content, $start, $end, $lower=false) {
if($lower!=false) {
$content = strtolower($content);
$start   = strtolower($start);
$end     = strtolower($end);
}
$content = substr($content, strpos($content,$start)+strlen($start));
$content = substr($content, 0, strpos($content,$end));
return $content;
}
?>
</table>

1 个答案:

答案 0 :(得分:1)

您似乎从循环内的URL下载内容。也许这就是为什么它很慢而不是mysql查询本身的原因。

尝试写出一些时间戳或注释一些代码,看看是什么导致它变慢。