[PHP] Which is more efficient?

Associate
Joined
10 Jul 2006
Posts
2,423
Hi there,

Basically I am creating a project that creates a monthly view of things that are happening on specific days.

I am going to store all the information that is to be filled in the calendar in a database and I was wondering what the most efficient and quickest way to get the data would be.

What I am thinking is there is two options.

I can either query the database as I create each cell of the table for each day, or I can query for the month and sort it out using code manually, via If statements and what not.

Obviously the first one will be the easiest, but I am wondering it would be better to do the second one?

Thanks.
 
I would expect getting the whole month would be best. If you get each day, you might be wasting queries where nothing is returned, but getting the whole month would just grab all the ones that do exist and not waste anything on days where data doesn't exist :)

Without properly 'benchmarking' both of them, I can't be sure, but that's what I think would be most efficient.
 
The bottleneck performance-wise on most computer systems is disk access times.

If a database table is stored on disk then then that disk will need to be accessed for each query (various query-caching methods can improve this situation but you get the idea). This means that each query comes with an overhead (the time it takes to get to the right place on the disk) i.e. 20 x query = 20 x disk accesses = 20 x the overhead.

Considering the overhead time is probably much greater than the execution time for a simple SELECT query, it makes sense to get all your data in as few queries as possible and then deal with it in memory (i.e. your php script).
 
You also have to think of the load and bandwidth of the server. If you are doing individual queries [30 per month] and you have lots of people accessing your page, that's a lot of queries and your site could grind to a halt.

I have always read it is better to do as few queries as possible. If you do the query right you can load each day, in order, into an array and then just iterate over that to set up the info on the page.
 
Back
Top Bottom