On my site I experience a rather high server load time and I am thinking of ways to optimize some of my mysql queries.
What I do now:
-
First I count all the cars in the table having certain criteria;
-
If the number of cars is greater then 20, I do that query with those criteria;
- If the number is smaller, I do a more general search just to give some results on the page.
Is there a faster way to do this?
It seems I am loosing performance counting the rows with certain criteria** and querying them once again just to get the data.
$car_sql = mysql_query("SELECT COUNT(*) FROM ".$table." WHERE carid IS NOT NULL $criteria1 $criteria2 $criteria3");
$car_nr = mysql_result($car_sql, "0");
if($car_nr > 20){
$result = mysql_query("SELECT carid,pic0 FROM ".$table." WHERE carid IS NOT NULL $criteria1 $criteria2 $criteria3");
} else {
$result = mysql_query("SELECT carid FROM ".$table." LIMIT 20");
}
Any ideas?
You can combine your first and your second query as they are the basically the same:
But you would have to test if that is a lot slower than the first separate query.
It would be faster if the number of results normally is bigger than 20.