By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,197 Members | 988 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,197 IT Pros & Developers. It's quick & easy.

include file vs db queries

P: n/a
Hello,

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: "path_category_32.inc".
In my web page I do something like

include("path_category_".$_GET['cat_id'].".inc");

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

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?
Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
zorro wrote:
Hello,
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?


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.
Jul 17 '05 #2

P: n/a
zorro wrote:
Hello,

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: "path_category_32.inc".
In my web page I do something like

include("path_category_".$_GET['cat_id'].".inc");
Aside from your database-file include problem, I hope you're not actually
using this line...
and the file contains:
echo "Products > Electronics > Computers > Laptops > Accessories >
Mouse";

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?


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

Rutger Claes
--
Rutger Claes rg*@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.

Jul 17 '05 #3

P: n/a
zorro wrote:
Hello,

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: "path_category_32.inc".
In my web page I do something like

include("path_category_".$_GET['cat_id'].".inc");

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

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?


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.

HTH
Jerry
Jul 17 '05 #4

P: n/a
zorro wrote:
<snip>
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 <snip> Any advice?


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:

<?php
$_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:

<?php
echo implode(' &gt; ', $_SESSION['path']);
?>

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #5

P: n/a
"zorro" <my*****@yahoo.ca> wrote in message
news:1b**************************@posting.google.c om...
Hello,

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: "path_category_32.inc".
In my web page I do something like

include("path_category_".$_GET['cat_id'].".inc");

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

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?


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.
Jul 17 '05 #6

P: n/a
nc
zorro wrote:

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.
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.
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?
If that data is indeed static, query caching should minimize
your overhead...
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?


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

Cheers,
NC

Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.