Guys I'm trying to update my table which have 60 thousand rows, and this is my code:
just ignore the logic of the code, just take a look at the number of queries it has and the loops. I', planning to put a limit to the main query but I want it to be the maximum limit of SQL so that I can save time. Any idea guys?
$query=mysql_query("SELECT DISTINCT(nid) FROM `comment` LIMIT ");
//mysql_query("UPDATE comment set thread = '00/' WHERE pid = 0 and uid = 333333");
while($result=mysql_fetch_array($query))
{
$query2=mysql_query("SELECT cid,pid,thread FROM comment WHERE nid = ".$result['nid']." ORDER by created");
while($result2=mysql_fetch_array($query2)){
$nodethread = 0;
if($result2['pid'] == 0)
$thread = int2vancode($nodethread) . "/";
else{
$parent = (string) rtrim((string) $result2['thread'], '/');
$parent=explode('.',$parent);
echo $parent." this is parent</br>";
$max=max($parent);
if($max == '')
{
$thread = $result['thread'].'.'.int2vancode(0) .'/';
}
else
{
//$parts = explode('.', $max);
$parent_depth = count($parent);
echo "parent".$parent_depth;
$last = $parent[$parent_depth];
$thread = $result2['thread'] .'.'. int2vancode(vancode2int($last) + 1) .'/';
}
}
mysql_query("UPDATE comment set thread = '$thread' where cid = ".$result2['cid']."");
}
}
SO to summarize my code, I have first a:
while loop for my first query
then i have another while loop inside for my second query
and lastly i have an update inside.
The issue I think you're trying to get at is concurrency.
INSERT
/UPDATE
/DELETE
operations require locks to be able to run. MyISAM tables will lock the entire table while these run, while InnoDB usually only locks the affected rows. If a query runs that need access to these tables/rows, even a SELECT
, it will be queued until the lock is removed.
You seem to be firing off a squillion tiny queries which should be OK, so long as it doesn't matter if other queries run in between. If it's not OK, then you need to switch to InnoDB and use transactions. But, as it stands, you're probably not going to have much issue with concurrent queries slowing each other down.