foluska Posted February 2, 2009 Share Posted February 2, 2009 my current code is way to slow. Also it looks memory extensive so please can you suggest improvements? for($i=0;$i<count($save);$i++) { $query .= ($i)?" OR id='".$save[$i]."'":" id='".$save[0]."'"; } $res = mysql_query("SELECT * FROM my_table WHERE".$query); $numsav = mysql_affected_rows(); if($res) { while($row = mysql_fetch_assoc($res)) { $doc = del_html(addslashes(@implode("",@file($row['link'])))); if(!mysql_query("INSERT INTO saved_info (title, link, description, data, cat, pic) VALUES ('".addslashes($row['title'])."','".$row['link']."','".addslashes($row['description'])."','".$doc."','".$row['cat']."', '".$row['pic']."')")) { // echo mysql_error(); $done += 1; echo "<br>". $done; } } }; Link to comment https://forums.phpfreaks.com/topic/143416-please-improve-my-mysql-code/ Share on other sites More sharing options...
Mchl Posted February 2, 2009 Share Posted February 2, 2009 1. Do you have indexes created in `my_info` table on columns that appear in WHERE clause? 2. Do not run INSERT in a loop. Create a multirow INSERT instead. Link to comment https://forums.phpfreaks.com/topic/143416-please-improve-my-mysql-code/#findComment-752281 Share on other sites More sharing options...
redarrow Posted February 2, 2009 Share Posted February 2, 2009 but i don't see why your code shouldn't work regardless off the while loop Create a multirow INSERT instead. <<<<< what this mean sorry. can not remember where i read this but using the count in the loop can slow things down. saying that i don't see any code there that will make a sever go slow at all. WARNING if this project out weighs your current database then it time to head for a dedicated database host. Link to comment https://forums.phpfreaks.com/topic/143416-please-improve-my-mysql-code/#findComment-752282 Share on other sites More sharing options...
Mchl Posted February 2, 2009 Share Posted February 2, 2009 Create a multirow INSERT instead. <<<<< what this mean sorry. Consider INSERT INTO table VALUES (v11,v21,v31); INSERT INTO table VALUES (v12,v22,v32); ... INSERT INTO table VALUES (v1n,v2n,v3n); and INSERT INTO table VALUES (v11,v21,v31), (v12,v22,v32), ..., (v1n,v2n,v3n) Which one will be faster? Running n queries each inserting one row, or one query inserting n rows at once? Link to comment https://forums.phpfreaks.com/topic/143416-please-improve-my-mysql-code/#findComment-752286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.