Jump to content

How can I make this code only load the first 30 rows or run faster on loading?


Recommended Posts

When I run this code it takes longer then I want to load the search results. I thought by only loading the first 30 rows it catches it would speed things up. Any ideas? I changed the $db to be 30, but only my first page painted and it was not much faster....

 

if(!empty($keyword) && $keyword!='Zip Code')	
$sql="SELECT profiles.*,zipCode FROM profiles LEFT JOIN area_served  ON area_served.profileId = profiles.profileId WHERE profileType!='HO' AND 

profiles.status='Y' $qrystr GROUP BY profiles.profileId $orderby CASE zipCode  $zips_order_str , companyName asc ";
else
$sql="SELECT * FROM profiles LEFT JOIN area_served  ON area_served.profileId = profiles.profileId WHERE profileType!='HO' and status='Y' $qrystr 

GROUP BY profiles.profileId $orderby ";

//echo  $sql;

$db->query($sql);
$numRows=$db->numRows();
$page->set_page_data($_SERVER[php_SELF],$numRows,$listing,10,true,false,true);
$page->set_qry_string("searchFor=$searchFor&cat=$cat&state=$state&keyword=$keyword&distance=$distance&sorting=$sorting&listing=$listing"); 
$res=$db->query($page->get_limit_query($sql));
$smarty->assign('numResult',$numRows); /* show pagination */



while($row=$db->fetchAssoc($res))
{	
if($row['thumbnailPhoto']=='personalPhoto' || $row['thumbnailPhoto']==''){
	if(!empty($row['personalPhoto'])){
	$personalPhoto="profiles/".$row['personalPhoto'];
	$rimg->setImage($personalPhoto);
	$row['personalPhoto']=$rimg->resize(75,75,substr($personalPhoto,0,strpos($personalPhoto,"."))."_search");
	}else{
	$row['personalPhoto']="images/my-photo.gif";
	}
}else{
	if(!empty($row['logo'])){
	$personalPhoto="profiles/".$row['logo'];
	$rimg->setImage($personalPhoto);
	$row['personalPhoto']=$rimg->resize(75,75,substr($personalPhoto,0,strpos($personalPhoto,"."))."_search");
	}else{
	$row['personalPhoto']="images/my-photo.gif";
	}
}
if(count($cat>0)){
$row['cat']=@implode(", ",$cat);
}

if(strlen($row['companyInfo'])>90){
	$concat="...";
}
else{
		$concat="";
}

There's not nearly enough information to know why it seems slow to you. What's $db? What's $page? Is that stuff with $rimg actually resizing the image every single time?

if you have phpmyadmin, or mysql in a terminal, run the query using "explain" before each select. Example:

 

explain select * from my_table where user_name='Mickey Mouse';

 

The columns you want to look at is:

possible_keys and key

 

if key returns null, you probably want to add some indexes to your table.

 

so, I might index your first query like so:

 

ALTER TABLE  profiles ADD INDEX (profileType, status);

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.