I’ve been coding a new dashboard, where I want to show various DB stats, mainly counting rows and setting the results into variables, its all working correctly, but I am a bit concerned that so many SELECT queries could become too heavy if a lot of users are entering or refreshing the page.
Appreciate your input 🙂
$tbl_players = players;
$xbox = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'XBOX360'");
$ps3 = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PS3'");
$pc = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PC'");
$xbfa = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'XBOX360' && fathread LIKE 'http%'");
$psfa = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PS3' && fathread LIKE 'http%'");
$pfa = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PC' && fathread LIKE 'http%'");
$xbcr = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'XBOX360' && crthread LIKE 'http%'");
$pscr = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PS3' && crthread LIKE 'http%'");
$pcr = mysql_query("SELECT * FROM $tbl_players WHERE Console = 'PC' && crthread LIKE 'http%'");
while($row = mysql_fetch_array($xbox))
{
$cxbox = mysql_num_rows($xbox);
}
while($row = mysql_fetch_array($ps3))
{
$cps3 = mysql_num_rows($ps3);
}
while($row = mysql_fetch_array($pc))
{
$cpc = mysql_num_rows($pc);
}
while($row = mysql_fetch_array($xbfa))
{
$xboxfa = mysql_num_rows($xbfa);
}
while($row = mysql_fetch_array($psfa))
{
$ps3fa = mysql_num_rows($psfa);
}
while($row = mysql_fetch_array($pfa))
{
$pcfa = mysql_num_rows($pfa);
}
while($row = mysql_fetch_array($xbcr))
{
$xboxcr = mysql_num_rows($xbcr);
}
while($row = mysql_fetch_array($pscr))
{
$ps3cr = mysql_num_rows($pscr);
}
while($row = mysql_fetch_array($pcr))
{
$pccr = mysql_num_rows($pcr);
}
$tbl_interactive = interactive;
$maximum = mysql_query("SELECT ID FROM $tbl_interactive ORDER BY ID DESC LIMIT 1");
while($max = mysql_fetch_array($maximum))
{
$cint = $max['ID'];
}
$xboxf = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'XBOX360' && Type = 'Player' && $date < EndTime");
$xboxc = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'XBOX360' && Type = 'Club' && $date < EndTime");
$ps3f = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PS3' && Type = 'Player' && $date < EndTime");
$ps3c = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PS3' && Type = 'Club' && $date < EndTime");
$pcf = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PC' && Type = 'Player' && $date < EndTime");
$pcc = mysql_query("SELECT * FROM $tbl_interactive WHERE Console = 'PC' && Type = 'Club' && $date < EndTime");
while($row = mysql_fetch_array($xboxf))
{
$xboxfi = mysql_num_rows($xboxf);
}
while($row = mysql_fetch_array($xboxc))
{
$xboxci = mysql_num_rows($xboxc);
}
while($row = mysql_fetch_array($ps3f))
{
$ps3fi = mysql_num_rows($ps3f);
}
while($row = mysql_fetch_array($ps3c))
{
$ps3ci = mysql_num_rows($ps3c);
}
while($row = mysql_fetch_array($pcf))
{
$pcfi = mysql_num_rows($pcf);
}
while($row = mysql_fetch_array($pcc))
{
$pcci = mysql_num_rows($pcc);
}
Many Thanks!
Yes, that is too many queries, way too much overhead, and too much heavy lifting to get the resultset you want.
You could get all nine of those counts from the
$tbl_playerstable with a single statement, and on a single row. Those nine separate statements you have are preparing nine separate resultsets, and you’re fetching all those rows in the resultset to the client just to get a count. (Maybe there’s some optimization in there somewhere, but MySQL is preparing all those rows to return to the client.) It would be much more efficient to have MySQL just return the counts you want, using a statement something like this:That gets you back a single row, with nine values. That would cut down on the amount of code you’ve got considerably, and significantly improve performance.
Similarly, you can get all those counts from the
$tbl_interactivetable with a single statement as well:FOLLOWUP:
It’s possible that the SQL statements above to return a
NULLrather than a zero, when there are no rows that satisfy the predicates (that is, match the WHERE clause). To have the statement return a count of zero in place of the NULL, we can use the MySQLIFNULL()function as convenient shorthand:Note that we’ve just wrapped an expression:
IFNULL(expr,0)That’s just a shorthand equivalent to:
IF(expr IS NULL, 0, expr)also equivalent to the ANSI standard:
CASE WHEN expr IS NULL THEN 0 ELSE expr END(Using
IFNULL, we only have to specifyexprone time.)Or, you could let MySQL return the NULL, and handle the replacment of the NULL with a zero in PHP. (Note that fetched value in the
$rowarray will still contain theNULL, but a zero will be assigned to the scalar: