views:

48

answers:

4

I am new to web development and am used to extensive use of various data structures in getting things done. I've also heard that having lots of queries is worse than few queries (but that complex queries can be even slower).

I'm wondering whether lots of simple queries would perform better or worse than one complex query that is later processed extensively using PHP.

+1  A: 

I think you should just test these 2 cases.
It depends on many things. And you asked in general.
Make a stress tests and you will see what is better for your demands.
In general you are right.
But there is no exact information and anyway you will spend less time writing 2 simple cycles to know what is better in your case.

Sergey
+1  A: 

Everything needs to be benchmarked. Your question is too common. There is no correct answer.

FractalizeR
A: 

Use EXPLAIN to see how a query is executed and how much time it takes.

Frank Heikens
+1  A: 

A rule of thumb that can act as a starting point to begin further examination could be to avoid doing anything twice.

If you run multiple queries, chances are that you let your database do redundant operations. Maybe you do a sorted query on the same table in more than one of your queries, so the DBMS has to do the sorting every time. Even if the DBMS can optimize for that, it's still overhead.

Try to do things once and try to get as much information as possible from already existing result sets instead of running a new query.

In my experience, the latency for communicating with the DB is usually so high (especially if the DB lives on another machine), that a few calculations in PHP should be preferred over an additional query if they can get you the same result. Believe it or not, PHP is faster than most people think.

However. all of this needs practical evaluation. You have to check for yourself wether any of this improves your overall performance.

Techpriester