visitor (0 QPoints)
  • FR
  • EN
  • NL
  • DE
  • ES
315 experts, 1193 registered users, 1659 questions already answered
European Experts Exchange, the very best site for high-quality IT solutions

New Improved Search!

 


05/10/2011 1h30 : Steve Jobs is dead, the father of Apple ][ is gone, we are all orphaned.

Languages :: PHP :: slow process


By: roe1and Great Britain  Date: 05/02/2008 10:51:11  English  Points: 20 Status: Answered
Quality : Excellent
hi vgr. this question is re: my earlier one. what i am doing is loading the results of a unix du -ak into a database. so in my code below du_size is a number(usually between 50 and 300). all i want to do is add them up. but it seems my method is quite slow. any ideas?

echo '<table>';

$sql3 = "SELECT du_size FROM $tbl_du;";
$query3 = mysql_query($sql3, $marc) or die ("No 3: Misbehaving: ".mysql_error());
$rows3 = mysql_num_rows($query3);
$du_size_total = 0;
for ($i=0; $i<$rows3; $i++) {
$du_size = mysql_result($query3, $i, "du_size");
$du_size_total = $du_size + $du_size_total;
}

echo '<tr><td>' . $du_size_total . ' total size in bytes</td></tr>';
echo '</table>';
mysql_free_result($query3);
By: VGR Date: 07/02/2008 08:49:37 English  Type : Answer
yes, it's ugly slow because you are doing the exact opposite of the recommendations on http://fr3.php.net/manual/en/function.mysql-result.php ;-)


When working on large result sets, you should consider using one of the functions that fetch an entire row (specified below). As these functions return the contents of multiple cells in one function call, they're MUCH quicker than mysql_result(). Also, note that specifying a numeric offset for the field argument is much quicker than specifying a fieldname or tablename.fieldname argument.



in your case, you want to addthe du_size column value for all lines : why don't you do simply
select sum(du_size) as theresult from $tbl_du;

?

and then do a single $res=mysql_fetch_array(); and get $res['theresult']...

Moreover, you do a loop for all lines while you ask already for the complete dataset : so, in fact, and unless I'm mistaken - I always use fetching functions, never that mysql_result() form... - you are operating in O(n²) in stead of O(n) !!! This explains why it's so slooowwww....

Do register to be able to answer

EContact
browser fav
page generated in 89.163060 milliseconds

Why Google AdSense ads ?

compteur
 Ranking-Hits PageRank for this page