The Little Guy Posted June 25, 2009 Share Posted June 25, 2009 Is there any way that I could optimize this code even more? I have 1,309,345 rows in the table that it searches, and it is constantly is increasing. $date = date("Y-m-d H:i:s", strtotime("now - 30 days")); $sql2 = mysql_query(sprintf("SELECT COUNT(DISTINCT ip) as totalCount, referrence FROM stats WHERE owner_id = '%s' AND project_id = '%s' AND refDomain != '%s' AND referrence != 'URL' AND `date` >= '{$date}' GROUP BY referrence ORDER BY totalCount DESC LIMIT 100", mysql_real_escape_string($globalInfo['owner_id']), mysql_real_escape_string($_GET['projectID']), mysql_real_escape_string($domain)))or die(mysql_error()); Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/ Share on other sites More sharing options...
phant0m Posted June 25, 2009 Share Posted June 25, 2009 assuming these "Ids" are strings, just use integers instead. This should improve the performance. and use an MySQL's index feature also, you could use a separate table for the refDomain, and use a foreign key instead. This should improve the performance as well. Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-863582 Share on other sites More sharing options...
The Little Guy Posted June 25, 2009 Author Share Posted June 25, 2009 I turned it into an unbuffered query instead of a normal mysql_query, and If I do this, I don't see any speed difference, but they say that mysql_unbuffered_query is faster... while($row = mysql_fetch_array($sql2)){ $color = ($i%2) ? $color1 : $color2 ; if($row['referrence'] == '') echo '<tr><td style="background-color:'.$color.';" class="lrgNum">'.$row['totalCount'].'</td><td style="background-color:'.$color.';" class="mid">Unknown</td></tr>'; else echo '<tr><td style="background-color:'.$color.';" class="lrgNum">'.$row['totalCount'].'</td><td style="background-color:'.$color.';" class="mid"><a href="'.$row['referrence'].'">'.substr($row['referrence'],0,75).'</a></td></tr>'; $i++; if($i == 100){ mysql_free_result($sql2); echo 'killed'; } } So, my question is: am I doing this correctly? Also... I am using integers in the database, but I place quotes around them in the query, does that slow it down any? Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-863642 Share on other sites More sharing options...
Ken2k7 Posted June 25, 2009 Share Posted June 25, 2009 Assign indexes. Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-863816 Share on other sites More sharing options...
The Little Guy Posted June 26, 2009 Author Share Posted June 26, 2009 Assign indexes. I have indexed everything I can. Would it also be a good idea to spit the data into multiple tables, like phant0m suggested? I have about 15 - 20 columns in this table. Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-863827 Share on other sites More sharing options...
Ken2k7 Posted June 26, 2009 Share Posted June 26, 2009 That depends. It has to make sense. You shouldn't blindly remove data from one place and put them elsewhere. Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-863830 Share on other sites More sharing options...
The Little Guy Posted June 26, 2009 Author Share Posted June 26, 2009 could you explain more on that? Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-863832 Share on other sites More sharing options...
phant0m Posted June 26, 2009 Share Posted June 26, 2009 well, if these references that you store are all different, it doesn't really make sense, because you would have around the same amount of rows. Looking for the id in there and then inserting it in your primary query would "double" the work. But if you have rather few distinct references, it could run faster. Link to comment https://forums.phpfreaks.com/topic/163668-how-to-optimize-this-code-code/#findComment-864024 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.