Due to growing number of users and high server loads caused by MySQL I decided to check all my scripts (written not by me - I am not that a coder) to see if it's possible to reduce the number of MySQL queries and to do some optimisation.
The are a lot of selects from 2 or even 3 tables. See some examples below.
Is it possible (is it necessary) to change (optimise) the queries in these examples?
Thank you.
This is the query to show the online number of friends.
$results = mysql_query("SELECT count(*) as total FROM session, friends WHERE CAST(friends.friendid AS CHAR) = session.user AND friends.ownerid = '$num'")or die(mysql_error());
Next lines are from forum script. The thread consists of 20 messages per page, so there are 20 queries to receive user information for each message.
$tmp=mysql_query("SELECT id, uid, text, user, fdate, deleted_reply, deleted FROM forum_replies where threadid='$threadid' order by fdate $sortby limit $start,$step") or die(mysql_error());
while($ix=mysql_fetch_array($tmp)) {
$userinfo=mysql_query("SELECT photo, fthreads, freplies FROM users where id='".$ix['uid']."'") or die(mysql_error());
list($photo, $fthreads, $freplies) = mysql_fetch_row($userinfo);
}