include file vs db queries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • zorro

    include file vs db queries

    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_c ategory_".$_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?
  • kicken

    #2
    Re: include file vs db queries

    zorro wrote:[color=blue]
    > 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?[/color]

    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.

    Comment

    • Rutger Claes

      #3
      Re: include file vs db queries

      zorro wrote:
      [color=blue]
      > 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_c ategory_".$_GET['cat_id'].".inc");[/color]

      Aside from your database-file include problem, I hope you're not actually
      using this line...
      [color=blue]
      > 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?[/color]

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

      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.

      Comment

      • Jerry Gitomer

        #4
        Re: include file vs db queries

        zorro wrote:[color=blue]
        > 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_c ategory_".$_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?[/color]

        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

        Comment

        • Pedro Graca

          #5
          Re: include file vs db queries

          zorro wrote:
          <snip>[color=blue]
          > 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[/color]
          <snip>[color=blue]
          > Any advice?[/color]

          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!

          Comment

          • Chung Leong

            #6
            Re: include file vs db queries

            "zorro" <myahact@yahoo. ca> wrote in message
            news:1baa8f0d.0 501031833.45073 247@posting.goo gle.com...[color=blue]
            > 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_c ategory_".$_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?[/color]

            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.


            Comment

            • nc@iname.com

              #7
              Re: include file vs db queries

              zorro wrote:[color=blue]
              >
              > 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.[/color]

              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.
              [color=blue]
              > 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?[/color]

              If that data is indeed static, query caching should minimize
              your overhead...
              [color=blue]
              > 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?[/color]

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

              Cheers,
              NC

              Comment

              Working...