I use PHP and MySQL to manage the content on my server/site (I have a video site).
Lately, the results of any page started coming really slow, not like any other site.
The CPU and Memory of the server are at 0-5% so overhead is not a problem.
What I do is query a database for entries and then show them, along with their thumbanil/score and other info.
What happens is when I open a results page, it does not get all the results at once, they add up one by one.
Is that normal?! of course not…to understand better: what happens is that the header loads, and then the content (many thumbnails linking to other pages) starts loading, but like this: the first 3 thumbs appear, then the next 4, then the next 2, then 4,then… until the end, when finally the bottom of my page loads, meaning the contact link and ToS.
Is there any idea crossing your mind?
I mention that my server has good hardware condition, strong CPU and lots of RAM which are all at a minimum of used % and also mention that the ping to my server is around 40ms (I live in central Europe, the server is in Chicago)
My database contains around 10,000 entries, each page shows 24 entries which are being processed each by this php script:
while($arr=mysql_fetch_array($result)){
?>
<div class="thumb" align="center">
<?php
echo "<a href='videos/".$arr['title'].".php'>";
?>
<img src="thumbs/<?php
$title=basename($arr['address']);
$title=substr($title,0,strlen($title)-4);
echo $title;
?>_5.jpg" onMouseOver="startThumbSlide('<?php echo $title; ?>', '5', 'thumbs/<?php echo $title.'_'; ?>')" onMouseOut='stopThumbSlide();' id='pic_<?php echo $title; ?>' width="200px" height="150px"/>
<br />
<div align="left">
<?php
rating_form($arr['address']);
echo '</div>';
echo "</a>";
echo "<a href='videos/".$arr['title'].".php' class=\"thumba\" onMouseOver=\"startThumbSlide('".$title."', '5', 'thumbs/".$title.'_'."')\" onMouseOut='stopThumbSlide();'>".$arr['title']."</a><br>
<span style=\"font: normal 10px verdana; color:#999;\">Viewed: ".$arr['views']." times</span>".'
<span style="float:right; font: normal 10px verdana; color:#999;">'.$arr['lenght'].'</span> </center>'."
</div>";}
Thanks a million times in advance to any advise you might give me!
Cheers!
PS: I have to add that the traffic has not changed, actually the site worked better 2 weeks ago when I had double the traffic I have now, and no settings were changed on my server, just slight scripting changes, but I can’t remember exactly what I changed when this started happening…
Edit: Here is the MySQL query I use to get my data from the database, including search string manipulation, the search string variable is $s, this code comes after I have stripped tags and trimmed it.
Here is the code:
$pattern='/[^a-zA-Z0-9]+/';
$s=preg_replace($pattern,' ',$s);
$query="SELECT
*,
CASE when title like '%".$s."%' then 1 else 0 END as titlematch,
CASE when tags like '%".$s."%' then 1 else 0 END as tagsmatch,
MATCH (title, tags) AGAINST ('*".$s."*' IN BOOLEAN MODE) AS relevance
FROM videos
WHERE
(title like '%".$s."%' OR
tags like '%".$s."%' OR
MATCH (title, tags) AGAINST ('*".$s."*' IN BOOLEAN MODE))
AND page=1
ORDER by
titlematch desc, tagsmatch desc, relevance desc
";
$all = mysql_query($query);
$total = mysql_num_rows($all);
if($total>0){
$n1=$start-1;
$result = mysql_query($query. " LIMIT $n1,$max");
while($arr=mysql_fetch_array($result)){
[...This part is already posted...]
Also, I would like to add that I currently have 2 indexes, one for the index field and one fulltext, for the title and tags fields.
The short answer is, it can be very difficult to diagnose problems like these. But there are some tools that might shed some light on the problem, and lead you to a more specific area to focus on.
First, I’d start with a basic internet speed test, like: http://www.speedtest.net/ Verify that everyone else’s pages are in fact loading quickly.
Then, just for kicks, get someone outside your country to try to load your site, and see if it’s slow for them too. You might be able to try this yourself by loading your site through a foreign proxy. I suggest all this because those who control the pipes are known to throttle things that are using a lot of bandwidth, like videos sites, for example.
While it sounds like it’s the HTML that is loading slowly, and not necessarily server processing speed, it couldn’t hurt to check your database speed. I’d use a tool like phpMyAdmin, and run your SQL query and just see if the results return quickly. If they’re slow, it could be an inefficiently written SQL query, or some database settings that need to be tweaked. Just doing a Google search for “mySQL suddenly very slow” brings up many posts that are resolved simply by updating to the latest version of mySQL.
Those are all the ideas I have for now. Let me know if you discover anything.