tags:

views:

63

answers:

2

I'm trying to make my php mysql driven pages as fast as possible. I've got a fairly standard website with a left-column menu with lists of articles and recipes (loaded from the db) and then on the main content there are some randomly changing articles with clipped previews and also random recipes.

What I've got at the moment is one sql query which loads all the articles and recipes into a php array that looks like this:

$s = s("SELECT * FROM `pages` WHERE `visible`='1' ORDER BY `group` ASC, `date` DESC");
   while($r=mysql_fetch_assoc($s)) $hPage[]=$r;
   $hPage_count = count($hPage);
   $hPage_keys = array_keys($hPage);
   $hPage_size = sizeOf($hPage_keys);

Then I simply refer back to and search this array for all the data I need for different parts of my page.

One of my loops looks something like

for ($i=0; $i<$hPage_size; $i++){
$c = $hPage[$hPage_keys[$i]];
if($c['group']==1){
$type[$c['type']][count($type[$c['type']])] = array('title'=>$c['title'],'url'=>$c['url']);
}
}

What I need to find out is, is this the fastest way to do it? With only one SQL query, loading everything into a php array and then working with that, Or would it be better to make multiple SQL queries?

Thanks for any help.

+2  A: 

As a rule of thumb, pulling all the information you need in as few database calls as possible is a good thing for application performance. Assuming you aren't pulling down a ton of information you don't actually need for the page I think you're probably already on the right track.

Since you're actually using the database to query, you'll want to make sure the query is well optimized as well.

As with all things performance related only way to really know is with a measurements and a profiler, though, and that only if there's really a need to increase the performance.

easel
Yes, I'm only getting the info that I need from the database.What do you mean by the query being well optimized? I'm new to database optimization.
Emmanuel
At the simplest, make sure you've got indexes on every column showing up in the where clause. There's a whole dark voodoo to database query optimization, but it typically starts with looking at the output from the query planner and identifying needed indexes, if any, and then devolves from there.
easel
@Emmanuel it seems you do not understand what are you doing. Actually you **do not** getting only necessary info, but "all the articles and recipes" and then choose only one to display. Thus, you have to learn PHP and mysql first. Actually at the moment you are far away from any optimization attempts. You need sensible code first.
Col. Shrapnel
Well, from the results of the sql query I'm writing a menu with titles and links to all the articles and recipes, showing two random articless. I'm getting all the recipes and articles from the db and in a sense I'm using everything, but perhaps it would be better to have two sql queries, one for all titles and urls, and then another to fetch the content of the two random articles.
Emmanuel
A: 

A database's sole purpose in life is to collect, organize, crunch, and return data. It's best to let it to what it does best. However, the database is often the biggest bottleneck. Keep the number of queries down and make sure you make each call count.

If you're querying a single table and require more than one query to do the job, there is likely a more efficient and elegant solution.

Tim