我有一個測試頁面正在運行#-tag趨勢,它存儲在我的數據庫中。該頁面需要很長的時間顯示的內容之前,只用一個函數來加載(最多2分鐘)即頁面PHP/MYSQL排名代碼導致緩慢加載頁面PHP/MYSQL
網址:http://www.sudanesetweeps.com/trendingtopics.php
如何調整使頁面加載的聲明中少時間?
這是我的代碼:
<?php
require_once("dbconnect.php");
require_once("lib_isarabic.php");
$query = "SELECT COUNT(*) cnt, hashtags
FROM tweets
WHERE tweeted_at >= DATE_SUB(NOW() , INTERVAL 2 DAY)
AND hashtags != ''
GROUP BY hashtags
ORDER BY cnt DESC LIMIT 100";
$res = mysql_query($query);
while($row = mysql_fetch_assoc($res)) {
$count = $row['cnt'];
$hashtags = explode(" ", $row['hashtags']);
foreach($hashtags as $hashtag) {
if(strtolower($hashtag) != 'sudan' && strtolower($hashtag) != 'new' && strtolower($hashtag) != 'new')
if(is_arabic($hashtag))
$topics_ara[strtolower(trim($hashtag))] += $count;
else
$topics_eng[strtolower(trim($hashtag))] += $count;
}
}
array_multisort($topics_ara, SORT_DESC);
array_multisort($topics_eng, SORT_DESC);
$index = 0;
foreach($topics_eng as $key=>$value) {
$query = "SELECT count(*) cnt FROM (
SELECT count(*), tweeted_by FROM tweets
WHERE hashtags like '%$key%'
AND tweeted_at >= DATE_SUB(NOW() , INTERVAL 2 DAY)
GROUP BY tweeted_by
) AS T";
/* $query = "
SELECT count(*) FROM tweets
WHERE hashtags like '%$key%'
AND tweeted_at > DATE_SUB(NOW() , INTERVAL 1 DAY) ";
*/
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
if($row['cnt'] > 1) {
$index++;
if($key != "") {
$trending_eng[$key] = $value;
}
}
if($index > 30)
break;
}
$index = 0;
foreach($topics_ara as $key=>$value) {
$query = "SELECT count(*) cnt FROM (
SELECT count(*), tweeted_by FROM tweets
WHERE hashtags like '%$key%'
AND tweeted_at >= DATE_SUB(NOW() , INTERVAL 2 DAY)
GROUP BY tweeted_by
) AS T";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
if($row['cnt'] > 1) {
$index++;
if($key != "") {
$trending_ara[$key] = $value;
}
}
if($index > 30)
break;
}
//var_dump($trending_eng) ;
//var_dump($trending_ara) ;
?>
請表的鳴叫,不要使用'mysql_ *'功能的新代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/KJveJ)。請參閱[**紅框**](http://goo.gl/GPmFd)?相反,您應該瞭解[準備好的語句](http://goo.gl/vn8zQ)並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定,[本文](http://goo.gl/3gqF9)將有助於選擇。如果你關心學習,[這裏是很好的PDO教程](http://goo.gl/vFWnC)。 –
[Profile your code](http://stackoverflow.com/questions/133686/profiling-php-code/134305#134305),然後提出具體問題。 –
@Truth - 感謝您發佈PDO教程;我在我的評論中也包含了使用'mysql_ *''的人。 –