Jump to content

How to load the page?


scvinodkumar

Recommended Posts

since i have three nested loop so its takes more than 1 minute to load the whole page.

 

I assume these loops contain nested queries? Sounds like you need to rethink your logic so you don't have nested loops.

 

I second that. Based upon many samples of code I have seen posted in these forums I would be that you are using three sets of nested queries when you could get all of your data with just one. Post your code around these loops and we may be able to help reduce the processing time considerably.

Link to comment
Share on other sites

i have pasted below the code that i used, i am writing the output to an xml file

 

$query = "select * from fr_subcategories1 where catid=7 and parent_level=2 order by priority";
$result = mysql_query($query) or die(mysql_error());
while($fetch = mysql_fetch_object($result))
{	
$box_name = 'Box'.$main_count;
$query1 = "select * from fr_subcategories1 where catid=".$fetch->subid." and parent_level=3 order by priority limit 6";
$result1 = mysql_query($query1) or die(mysql_error());
if(mysql_num_rows($result1)>0)
{
	$count_start = 1;

	$category_name = $fetch->subcategory_title;

	while($fetch1 = mysql_fetch_object($result1))
	{															
		$query2	=  "SELECT f.id 'fid',f.category_id,i.* 
					FROM fr_feeds f
					JOIN fr_categories cat ON ( f.category_id = cat.id )    
					JOIN fr_items i ON ( i.feed_id = f.id ) 
					where (lower(i.content) like '%".$fetch1->subcategory_title."%' or lower(i.title) like '%".$fetch1->subcategory_title."%') 
					and category_id=7 AND (i.featured=1 or i.featured=0)
					AND DATE_FORMAT(publish_time, '%Y-%m-%d') = CURDATE()
					GROUP BY i.feed_id
					ORDER BY i.vote DESC, i.publish_time DESC , i.id DESC limit 3";	
		//echo $query2;
		//echo "<br>";			
		$result2 = mysql_query($query2) or die(mysql_error());			
		if(mysql_num_rows($result2)==0)
		{
			$query2	=  "SELECT f.id 'fid',f.category_id,i.* 
						FROM fr_feeds f
						JOIN fr_categories cat ON ( f.category_id = cat.id )    
						JOIN fr_items i ON ( i.feed_id = f.id ) 
						where (lower(i.content) like '%".$fetch1->subcategory_title."%' or lower(i.title) like '%".$fetch1->subcategory_title."%') 
						and category_id=7 AND (i.featured=1 or i.featured=0)
						AND DATE_FORMAT(publish_time,'%Y-%m-%d') = DATE_FORMAT(DATE_ADD(CURDATE(),INTERVAL -1 DAY ) , '%Y-%m-%d')
						GROUP BY i.feed_id
						ORDER BY i.vote DESC, i.publish_time DESC , i.id DESC limit 3";	
			$result2 = mysql_query($query2) or die(mysql_error());
		}
		if(mysql_num_rows($result2)==0)
		{
			$query2	=  "SELECT f.id 'fid',f.category_id,i.* 
						FROM fr_feeds f
						JOIN fr_categories cat ON ( f.category_id = cat.id )    
						JOIN fr_items i ON ( i.feed_id = f.id ) 
						where (lower(i.content) like '%".$fetch1->subcategory_title."%' or lower(i.title) like '%".$fetch1->subcategory_title."%') 
						and category_id=7 AND (i.featured=1 or i.featured=0)							
						GROUP BY i.feed_id
						ORDER BY i.vote DESC, i.publish_time DESC , i.id DESC limit 3";	
			$result2 = mysql_query($query2) or die(mysql_error());
		}									
		if(mysql_num_rows($result2)>0)
		{				
			$_xml .="\t<".$box_name." id='".$count_start."'>\r\n";
			$index_id = 1;
			$row_id =1;												
			while($fetch2 = mysql_fetch_object($result2))
			{									
				if($count_start<=3)	
				{							
					$_xml .="\t\t<title".$row_id.">".htmlspecialchars($fetch2->title)."</title".$row_id.">\r\n";	 
					$_xml .="\t\t<link".$row_id.">".htmlspecialchars("liststory.php?id=".$fetch2->id)."</link".$row_id.">\r\n";	 
				}	
				else
				{				
					if($index_id<=2)
					{
						$_xml .="\t\t<title".$row_id.">".htmlspecialchars($fetch2->title)."</title".$row_id.">\r\n";	 
						$_xml .="\t\t<link".$row_id.">".htmlspecialchars("liststory.php?id=".$fetch2->id)."</link".$row_id.">\r\n";	 							
					}
					$index_id++;	
				}
				$row_id++;
			}
			$_xml .="\t</".$box_name.">\r\n";				
			$count_start++;																	
		}
		else
		{
			$_xml .="\t<".$box_name." id='".$count_start."'>\r\n";
			$_xml .="\t\t<title1>No News Found</title1>\r\n";	 				
			$_xml .="\t\t<link1></link1>\r\n";
			$_xml .="\t</".$box_name.">\r\n";				
			$count_start++;
		}
		/*$count_cat++;			
		$count_start++;	
	}		
}
$main_count++;
}

Link to comment
Share on other sites

Yes, absolutely. I am by no means a SQL guru though. I would suggest prosting in the MySQL forum. But, looking at your first two queries, I *think* you could accomplish getting all of the same data without the two looping queries with just one query such as the one below. I have not tested it so I can't say if it will work. But, I know there is a solution, just not an expert in the syntax

 

SELECT t1.subid as p_subid, t1.subcategory_title as p_subcategory_title
FROM fr_subcategories1 t1
JOIN (SELECT *
      FROM fr_subcategories1
      WHERE catid=t1.subid
        AND parent_level=3
      ORDER BY priority
      LIMIT 6) AS t2
  ON t1.subid = t2.catid
WHERE t1.catid=7
  AND t1.parent_level=2
ORDER BY t1.priority, t2.priority

Link to comment
Share on other sites

  • 2 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.