2013-10-29 72 views
-1

我想从我的数据库中获取信息并显示它。下面是页面的样子截图:如何优化和提高此MySQL查询的速度?

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> 
+0

你确定这是阻碍性能的查询吗? – geomagas

+1

请缩进并设置你的代码的格式,因为人们会有20倍的动力来帮助你。 – MonkeyZeus

+0

如果我不得不作出反应,那么'$ xat = file_get_contents('http://www.xat.com/xat'.trim($ b [“room”]));'是减速 – MonkeyZeus

回答

1

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

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

+0

是的,它是file_get_contents,我测试了它。我如何解决它?我需要下载,它也给了20:53:4120:53:4320:53:4320:53:4320:53:4320:53:4420:53:4420:53:4520:53的时间延迟:4520:53:4520:53:4520:53:46 – user2791849

+0

如果您必须获取该数据,则很难降低加载时​​间。但是,如果您使用Ajax加载它,则可以为访问者提供更好的用户体验。然后,页面将加载得更快,并且可以更快地被用户看到 - 当完成后,您可以使用Ajax开始加载每行的状态。您可能必须创建一个新的PHP页面,并将$ b [“room”]作为参数,然后在单独的页面中执行file_get_contents。 – becquerel