PHP / MySQL 很慢,不知道为什么?
PHP / MySQL is slow, can't figure out why?
第一个函数 recentlyModified()
恰好在 1 秒内运行,即使它正在轮询两个节目。我承认,它的参数要少得多,但速度非常快,每场演出大约需要 0.5 秒。
然而,第二个函数 myList()
运行 4 个标题大约需要 16 秒,2 个需要 11 秒,1 个需要 6 秒。这当然是平均水平。
<?php
include_once './php/loadanime.php';
function crunchyrollNews() {
echo('<iframe class="rss fullwidth fullheight" src="http://feeds.feedburner.com/crunchyroll/animenews?format=xml"></iframe>');
}
function recentlyModified($count) {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$query = $animedb->query("SELECT id, img, name, description FROM animedeck.animelist ORDER BY modified DESC LIMIT " . $count . "");
if($query) {
$result = $query->fetch_all();
echo '<div class="recentanimemodule">';
echo '<h1 class="h1 borderb">AnimeDeck Recent</h1>';
foreach($result as $id) {
echo '<div class="recentanimepiece">';
echo '<img class="recentanimeposter" src="';
echo $id[1];
echo '" />';
echo '<a class="recentanimeurl" href="'; //a href start
echo './viewanime.php?id=' . $id[0];
echo '">';
echo '<div class="recentanimename">';
echo $id[2];
echo '</div>';
echo '</a>'; //a href end
echo '<div class="recentanimedes">';
echo $id[3];
echo '</div>';
echo '</div>';
}
echo '</div>';
} else {
echo "Failed to retrieve entries.";
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This module was created in ".$totaltime." seconds";
}
function myList($status, $sort, $startindex, $count) {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$orderby;
$title = "Recent";
$rstatus = ", status='" . $status . "'";
if($status == "all") {
$rstatus = null;
}
if($startindex == null) {
$startindex = 0;
}
switch ($sort) {
case "recent":
$sort = "DESC";
$orderby="modified";
$title = "Recent";
break;
case "oldest":
$sort = "ASC";
$orderby="modified";
$title = "Oldest";
break;
case "az":
$sort = "ASC";
$orderby="animename";
$title = "A to Z";
break;
case "za":
$sort = "DESC";
$orderby="animename";
$title = "Z to A";
break;
}
$query = $animedb->query("SELECT animeid FROM animedeck.mylist WHERE userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . "");
if($query) {
$result = $query->fetch_all();
echo '<div class="myanimemodule">';
echo '<h1 class="h1 borderb">MyList ' . $title . '</h1>';
foreach($result as $id) {
$info = loadSemiAnime($id[0]);
echo '<div class="animeid">' . $id[0] . '</div>';
echo '<div class="myanimepiece">';
echo '<img class="myanimeposter" src="';
echo $info['img'];
echo '" />';
echo '<a class="myanimeurl" href="'; //a href start
echo './viewanime.php?id=' . $id[0];
echo '">';
echo '<div class="myanimename">';
echo $info['name'];
echo '</div>';
echo '</a>'; //a href end
echo '<div class="myanimedes">';
echo $info['description'];
echo '</div>';
echo '</div>';
echo '<div class="animelistoptions">';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 0)" id="stowatch' . $id[0] . '" class="bluebutton">To Watch</span>';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 1)" id="swatching' . $id[0] . '" class="bluebutton">Watching</span>';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 2)" id="swatched' . $id[0] . '" class="bluebutton">Seen</span>';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 3)" id="srewatching' . $id[0] . '" class="bluebutton">Re-Watching <i class="fa fa-heart"></i></span>';
echo '</div>';
echo '<script>getStatus(' . $id[0] . ');</script>';
}
echo '</div>';
} else {
echo "Failed to retrieve entries. Error: " . $animedb->errno . " --- " . $animedb->error;
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This module was created in ".$totaltime." seconds";
}
?>
所以我的问题是我不确定我的代码中的什么导致它花费这么长时间?一次迭代需要 6 秒?是 PHP 吗?我认为 MySQL 执行得很快,所以我认为它是 PHP。但是哪一部分呢?
我不熟悉 PHP 的好习惯和坏习惯,所以我不知道是什么真正降低了我的功能。
这是我的 loadAnime() 和 loadSemiAnime() 函数:
<?php
function loadAnime($id, $toload) {
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$load = $animedb->query("SELECT " . $toload . " FROM animedeck.animelist WHERE id=" . $id . "");
if($load) {
$result = $load->fetch_assoc();
echo $result[$toload];
}
}
function loadSemiAnime($id) {
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$load = $animedb->query("SELECT img, name, description FROM animedeck.animelist WHERE id=" . $id . "");
if($load) {
$result = $load->fetch_assoc();
return $result;
}
}
?>
好吧,我最终按照此处的建议实施了对数据库设置的所有更改:https://www.percona.com/blog/2006/09/29/what-to-tune-in-mysql-server-after-installation/
你的查询是动态构建的,所以很难在那里看到任何东西,但构建它看起来很简单,所以我怀疑查询非常简单。
但是,您有连接到数据库并对主查询返回的每条记录进行查询的函数。这些似乎是不必要的,因为看起来您只需执行 JOIN 即可获取主查询中的字段:-
SELECT a.animeid, b.img, b.name, b.description
FROM animedeck.mylist a
LEFT OUTER JOIN animedeck.animelist b
ON a.animeid = b.id
WHERE a.userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . "
也是这个问题
$query = $animedb->query("SELECT animeid FROM animedeck.mylist WHERE userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . "");
你的 "userid" 在你的 where 子句中,在一个大的 table 上,如果这个字段不是索引,这将非常慢。进入您的 MySQL table 并向该列添加索引,例如
ALTER TABLE `animedeck` ADD INDEX `userid` (`userid`)
这应该会大大加快速度。
一般经验法则,where 子句中的任何字段都需要索引。 MySql 默认情况下将索引主键。因此您不需要手动索引主键(即 ID 字段)。
第一个函数 recentlyModified()
恰好在 1 秒内运行,即使它正在轮询两个节目。我承认,它的参数要少得多,但速度非常快,每场演出大约需要 0.5 秒。
然而,第二个函数 myList()
运行 4 个标题大约需要 16 秒,2 个需要 11 秒,1 个需要 6 秒。这当然是平均水平。
<?php
include_once './php/loadanime.php';
function crunchyrollNews() {
echo('<iframe class="rss fullwidth fullheight" src="http://feeds.feedburner.com/crunchyroll/animenews?format=xml"></iframe>');
}
function recentlyModified($count) {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$query = $animedb->query("SELECT id, img, name, description FROM animedeck.animelist ORDER BY modified DESC LIMIT " . $count . "");
if($query) {
$result = $query->fetch_all();
echo '<div class="recentanimemodule">';
echo '<h1 class="h1 borderb">AnimeDeck Recent</h1>';
foreach($result as $id) {
echo '<div class="recentanimepiece">';
echo '<img class="recentanimeposter" src="';
echo $id[1];
echo '" />';
echo '<a class="recentanimeurl" href="'; //a href start
echo './viewanime.php?id=' . $id[0];
echo '">';
echo '<div class="recentanimename">';
echo $id[2];
echo '</div>';
echo '</a>'; //a href end
echo '<div class="recentanimedes">';
echo $id[3];
echo '</div>';
echo '</div>';
}
echo '</div>';
} else {
echo "Failed to retrieve entries.";
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This module was created in ".$totaltime." seconds";
}
function myList($status, $sort, $startindex, $count) {
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$starttime = $mtime;
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$orderby;
$title = "Recent";
$rstatus = ", status='" . $status . "'";
if($status == "all") {
$rstatus = null;
}
if($startindex == null) {
$startindex = 0;
}
switch ($sort) {
case "recent":
$sort = "DESC";
$orderby="modified";
$title = "Recent";
break;
case "oldest":
$sort = "ASC";
$orderby="modified";
$title = "Oldest";
break;
case "az":
$sort = "ASC";
$orderby="animename";
$title = "A to Z";
break;
case "za":
$sort = "DESC";
$orderby="animename";
$title = "Z to A";
break;
}
$query = $animedb->query("SELECT animeid FROM animedeck.mylist WHERE userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . "");
if($query) {
$result = $query->fetch_all();
echo '<div class="myanimemodule">';
echo '<h1 class="h1 borderb">MyList ' . $title . '</h1>';
foreach($result as $id) {
$info = loadSemiAnime($id[0]);
echo '<div class="animeid">' . $id[0] . '</div>';
echo '<div class="myanimepiece">';
echo '<img class="myanimeposter" src="';
echo $info['img'];
echo '" />';
echo '<a class="myanimeurl" href="'; //a href start
echo './viewanime.php?id=' . $id[0];
echo '">';
echo '<div class="myanimename">';
echo $info['name'];
echo '</div>';
echo '</a>'; //a href end
echo '<div class="myanimedes">';
echo $info['description'];
echo '</div>';
echo '</div>';
echo '<div class="animelistoptions">';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 0)" id="stowatch' . $id[0] . '" class="bluebutton">To Watch</span>';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 1)" id="swatching' . $id[0] . '" class="bluebutton">Watching</span>';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 2)" id="swatched' . $id[0] . '" class="bluebutton">Seen</span>';
echo '<span onClick="setStatus(' . $id[0] . ', \'';
echo loadAnime($id[0], "name") . "'";
echo ', 3)" id="srewatching' . $id[0] . '" class="bluebutton">Re-Watching <i class="fa fa-heart"></i></span>';
echo '</div>';
echo '<script>getStatus(' . $id[0] . ');</script>';
}
echo '</div>';
} else {
echo "Failed to retrieve entries. Error: " . $animedb->errno . " --- " . $animedb->error;
}
$mtime = microtime();
$mtime = explode(" ",$mtime);
$mtime = $mtime[1] + $mtime[0];
$endtime = $mtime;
$totaltime = ($endtime - $starttime);
echo "This module was created in ".$totaltime." seconds";
}
?>
所以我的问题是我不确定我的代码中的什么导致它花费这么长时间?一次迭代需要 6 秒?是 PHP 吗?我认为 MySQL 执行得很快,所以我认为它是 PHP。但是哪一部分呢?
我不熟悉 PHP 的好习惯和坏习惯,所以我不知道是什么真正降低了我的功能。
这是我的 loadAnime() 和 loadSemiAnime() 函数:
<?php
function loadAnime($id, $toload) {
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$load = $animedb->query("SELECT " . $toload . " FROM animedeck.animelist WHERE id=" . $id . "");
if($load) {
$result = $load->fetch_assoc();
echo $result[$toload];
}
}
function loadSemiAnime($id) {
$animedb = new mysqli("localhost", "animedeck", "W8a7fs5gu");
$load = $animedb->query("SELECT img, name, description FROM animedeck.animelist WHERE id=" . $id . "");
if($load) {
$result = $load->fetch_assoc();
return $result;
}
}
?>
好吧,我最终按照此处的建议实施了对数据库设置的所有更改:https://www.percona.com/blog/2006/09/29/what-to-tune-in-mysql-server-after-installation/
你的查询是动态构建的,所以很难在那里看到任何东西,但构建它看起来很简单,所以我怀疑查询非常简单。
但是,您有连接到数据库并对主查询返回的每条记录进行查询的函数。这些似乎是不必要的,因为看起来您只需执行 JOIN 即可获取主查询中的字段:-
SELECT a.animeid, b.img, b.name, b.description
FROM animedeck.mylist a
LEFT OUTER JOIN animedeck.animelist b
ON a.animeid = b.id
WHERE a.userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . "
也是这个问题
$query = $animedb->query("SELECT animeid FROM animedeck.mylist WHERE userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . "");
你的 "userid" 在你的 where 子句中,在一个大的 table 上,如果这个字段不是索引,这将非常慢。进入您的 MySQL table 并向该列添加索引,例如
ALTER TABLE `animedeck` ADD INDEX `userid` (`userid`)
这应该会大大加快速度。
一般经验法则,where 子句中的任何字段都需要索引。 MySql 默认情况下将索引主键。因此您不需要手动索引主键(即 ID 字段)。