I make no bones about being a total newb to SQL... I have only ever done simple selects, inserts, deletes and updates to records... most of what I have done thusfar using the SQL DBs was to display data that I had already processed into a database.
This is the function I'm using in order to grab the top 10 attackers from the database... This function times out every time it runs (30secs). I use the same structure in other places in my code and the only difference is the sheer volume of SQL queries made. The only index I have on the scores and news tables are on the "id" field.
function attackers($criteria,$temptag) {
global $module_name, $db, $prefix;
opentable();
print "
<center><big><b>Top 10 Attackers:</b></big></center><br><br>
<table border='1'><tr>
<td width='25' align='center'>Rank</td>
<td width='250' align='center'>Country</td>
<td width='50' align='center'>Tag</td>
<td width='50' align='center'>SS-PS</td>
<td width='50' align='center'>GS-BR-AB</td>
<td width='50' align='center'>NM-CM-EM</td>
<td width='50' align='center'>Total</td></tr>";
$alphanum = $db->sql_numrows($alpharesult = $db->sql_query("SELECT * FROM x_scores WHERE tag='$temptag'"));
$a = 0;
while ($a < $alphanum) {
$alphalist[$a]['country'] = mysql_result($alpharesult,$a,"country");
$alphalist[$a]['countrynum'] = mysql_result($alpharesult,$a,"countrynum");
$alphalist[$a]['tag'] = mysql_result($alpharesult,$a,"tag");
$a++;
}
$b = 0;
while ($b < $alphanum) {
$country = $alphalist[$b]['country'];
$countrynum = $alphalist[$b]['countrynum'];
$tag = $alphalist[$b]['tag'];
$alphalist[$b]['lgcount'] = 0;
$alphalist[$b]['speccount'] = 0;
$alphalist[$b]['misslecount'] = 0;
$betanum = mysql_numrows($betaresult = $db->sql_query("SELECT code FROM x_earthnews WHERE attcountry='$country' && atttag='$tag'"));
$x = 0;
while ($x < $betanum && $betanum > 0) {
$code = mysql_result($betaresult,$x,"code");
switch($code) {
case 1:
case 2:
$alphalist[$b]['lgcount']++;
break;
case 5:
case 6:
case 7:
$alphalist[$b]['speccount']++;
break;
case 10:
case 11:
case 12:
$alphalist[$b]['misslecount']++;
break;
}
}
$alphalist[$b]['count'] = $alphalist[$b]['lgcount']+$alphalist[$b]['speccount']+$alphalist[$b]['misslecount'];
$b++;
}
array_csort($alphalist, 6, 'desc');
$i = 0;
while ($i < 10) {
$rank = $i + 1;
$country = $alphalist[$i]['country'];
$countrynum = $alphalist[$i]['countrynum'];
$tag = $alphalist[$i]['tag'];
$lgcount = $alphalist[$i]['lgcount'];
$speccount = $alphalist[$i]['speccount'];
$misslecount = $alphalist[$i]['misslecount'];
$count = $alphalist[$i]['count'];
print "
<tr><td align='center'>$rank</td>
<td align='middle'><a href='modules.php?name=Scores&func=viewcountry&countrynum=$countrynum'>$country</a>
<td align='center'> <a href='/modules.php?name=Scores&func=search&criteria=$tag'>$tag</a></td>
<td align='center'>$lgcount</td>
<td align='center'>$speccount</td>
<td align='center'>$misslecount</td>
<td align='center'>$count</td></tr>";
$i++;
}
echo "</table>";
closetable();
}
To give you a quick run down of the function I am first building an array of countries that are members of a given organization. That array is built from an 8000 record DB... at this time the largest org is about 265 members. Then, using they array built I count on 4 different attack types from the "code" field in the table that is roughly 200k records as of now, but could go to 300k+ records without too much of a problem...
So, I end up with up to 265 queries into 200k+ records and the script never finishes. So, can anyone offer any help to streamline this code?