PHP / MySQL很慢,想不通为什么?

时间:2016-03-08 09:21:09

标签: php mysql performance query-performance

第一个函数recentlyModified()完全在1秒内运行,即使它正在轮询两个节目。我承认,它使用的参数要少得多,但每次显示约0.5秒时速度很快。

然而,第二个函数myList()大约在16秒内运行4个标题,11秒运行2个,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;
    }       
}

?>

3 个答案:

答案 0 :(得分:0)

您的查询是动态构建的,因此很难看到任何内容,但构建它看起来很简单,所以我怀疑查询非常简单。

但是,您具有连接到数据库的函数,并对主查询返回的每个记录执行查询。这些似乎是不必要的,因为看起来你可以只需要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 . "

答案 1 :(得分:0)

这也是问题所在。

$query = $animedb->query("SELECT animeid FROM animedeck.mylist WHERE userid='" . $_SESSION['userid'] . "' " . $rstatus . " ORDER BY " . $orderby . " " . $sort . " LIMIT " . $startindex . ", " . $count . ""); 

您的&#34;用户ID&#34;在你的where子句中,在一个大表上如果这个字段不是索引,这将是超级慢的。进入MySQL表并向此列添加索引,例如

ALTER TABLE `animedeck` ADD INDEX `userid` (`userid`)

这应该会大大加快速度。

一般经验法则,where子句中的任何字段都需要索引。默认情况下,MySql将索引主键。因此,您不需要手动索引主键(即ID字段)。

答案 2 :(得分:-1)

好吧,我最终按照此处的建议实施了对数据库设置的所有更改:https://www.percona.com/blog/2006/09/29/what-to-tune-in-mysql-server-after-installation/