include file vs db queries

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm programming an Amazon type web site and find myself "wasting" a
lot of time writing code that gets information from include files
rather than from the database because I have this notion that it will
avoid clogging the db when many users come to the site. My main goal
anyway is to have the user wait as little as possible because that's
good for business apparently.

Here's an example:

When the user will be checking out a certain product I'll have to
display at the top of the page the product category path. For instance

Products > Electronics > Computers > Laptops > Accessories > Mouse

If I wanted to code quickly I could write a function that builds this
path by querying the database and run the function every time someone
accesses a page. Instead I have a folder "Paths" containing an include
file for every category possible. For example: "".
In my web page I do something like


and the file contains:
echo "Products > Electronics > Computers > Laptops > Accessories >

I have code that generates these include files and I only have to run
it when a category is added, removed or renamed - which will be rare.
Is this not a lot better than querying the database 1000 times a day
just to get information that is static? Now this was one example but
there are many other elements I could apply this method to. The
problem is that it takes a lot longer time to code and I also have a
delivery date to meet, yet I don't want to deliver something that is
not efficient because it queries the db all the time.

Any advice?

Re: include file vs db queries

zorro wrote:
Quoted text here. Click to load it

Not that I know a great deal about the subject, but I do know some
databases will cache the querys so if you submit multiple queries that
return the same thing, the dbms will simply return the cached results
and not bother searching the database again. For something like
generating those paths, I assume this mechinism would kick in and thus
using files or a database probably wouldn't make that big of a
difference speed wise.

Also to go along with the cached queries, make sure you are using SQL to
its full potential and doing things in the smallest number and most
efficient queries possible.  Use table joines, group functions, etc. to
produce the results requied in the fastest time.

Re: include file vs db queries

zorro wrote:

Quoted text here. Click to load it

Aside from your database-file include problem, I hope you're not actually
using this line...

Quoted text here. Click to load it

I don't think a database request will take that much time, if it is just one

 Rutger Claes
Rutger Claes                                                rgc@rgc.tld
Replace tld with top level domain of belgium to contact me    pgp:0x3B7D6BD6
You will always find something in the last place you look.

Re: include file vs db queries

zorro wrote:
Quoted text here. Click to load it

I think you will find it is faster and more efficient to use a
database query then to open, read, and close the include file
every time you use it.  Bear in mind that database accesses are
very fast (even a slow computer can support several hundred
database accesses per minute) and give you the opportunity to
make changes to your data without having to make corresponding
changes to your code.


Re: include file vs db queries

Quoted text here. Click to load it

Might as well just go the full route and cache the whole site.  It isn't
efficient either to have PHP execute the same code over and over again
yielding the same HTML over and over.

Just touch a file whenever the database is changed. Then, based on the
modified time of this file and that of the cached HTML file, regenerate the
latter as necessary. Implemented correctly, it can give you a big boost in
speed while requiring little intervention.

Re: include file vs db queries

zorro wrote:
Quoted text here. Click to load it
Quoted text here. Click to load it

As other posters have said I don't think the DB access method is all
that bad :)

However, my first thought to remove the DB access from your code was to
use $_SESSION variables, maybe something like:

$_SESSION['path'] = array('Products', 'Electronics', 'Computers',
                          'Laptops', 'Accessories', 'Mouse');

Of course it would have to be changed everytime the user moves around
your site. Then just output it at the checkout page:

echo implode(' &gt; ', $_SESSION['path']);

Mail to my "From:" address is readable by all at /
== ** ## !! ------------------------------------------------ !! ## ** ==
may bypass my spam filter. If it does, I may reply from another address!

Re: include file vs db queries

zorro wrote:
Quoted text here. Click to load it

If that's your main goal, consider tuning your database server
to allow query caching.  For a high-traffic site, think about
multiple database servers with load balancing.

Quoted text here. Click to load it

If that data is indeed static, query caching should minimize
your overhead...

Quoted text here. Click to load it

Think beyond coding.  Web application performance is often
defined not by how it's written, but by how it's deployed.


Site Timeline