Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 7 of 7

This is a discussion on Efficiency of Queries vs. Arrays in the Shared & Semi-Dedicated forum
Question for those in the know: Background: A few weeks back when reading some postings on a PHP/MySQL board I read a post where a ...

  1. #1
    JPC Member
    Join Date
    Apr 2003
    Posts
    16

    Efficiency of Queries vs. Arrays

    Question for those in the know:

    Background:
    A few weeks back when reading some postings on a PHP/MySQL board I read a post where a person talked about a three-tier architecture where the first tier was the db queries an such with the results being put into arrays and setting variables. The middle tier was the actual guts (programming) of the solution and the third tier was the user interface.

    Now, after several redesigns and rebuilds of my learning-site (is there any other way?) I am actually beginning to implement this architecture!

    Question(s):
    Just how much more efficient (processor utilization, time to produce pages, server load, etc.) is it to store query results in an array and then manipulate (query, read from, etc.) the array instead of requerying the db?
    As an illustrative example, I forsee a table in my solution to have ~15 fields and 1200 records in it. If I need to search this table to pull a dozen records out of it which would be more efficient: doing the query directly or having some PHP code to search through an array which holds all 1200 records?

    Is there a break even point? I can see clearly how a small table with ~12 records is best queried once, the result put into an array and using the array from there. But how about the 1200 I used in the example above? Is there a rule of thumb such as # of times I expect to read the array (or do the query) during a session or hits on the site or ???

    Many thanks once again for your thoughts and comments!

    EZ

  2. #2
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775
    Hrm.. interesting question! You are obviously 'one of us', or at least on your way to becoming 'one of us.'

    As fate would have it, last night I got interested in this very thing, and added code to my web site that addresses this issue. Now, let's see how sharp you actually are.

    Go to my web page by clicking the 'WWW' below this message. Look at the footer of various pages, and tell me what you see, young Luke.

    May 'The Force' be with you...
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

  3. #3
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    PHP's array manipulation has historically been rather slow. For most general purpose applications it works fine, but I would think that loading up an array with 1200 records would be slower than a more specific query. SQL was designed to be an efficient means of getting the data you need out of a database. PHP has some pretty good array handling functions, but they don't compare to the search capabilities of a well designed SQL database (with proper keys, indexing, and such).

    If you do a query for 1200 records and only use a quarter of them in your script, then the added overhead of pulling everything into an array is probably overkill. On the other hand, if you need to use the same data in different ways in a single script, or you need two two or three subsets of that data in different places in the script then they array method might work OK for you. Just try to make your queries as sepecific as possible to avoid pulling records you don't need.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  4. #4
    JPC Member
    Join Date
    Apr 2003
    Posts
    16

    Ah, Master....

    Ah, Master Vin!

    Cool that footer is! Do it how do you? Difficult is, writing like Yoda.

    So I'll write like a normal dufus instead of a Yoda dufus.

    Vin, I was on your site a while back - quite amazing skillz you have! (I think there might be 110 queries in my whole site right now - not just one page!) Care to share the code you use for that footer? I think that would most definitely help me decide situation-by-situation how to optimize my code for speed.

    Jason, thanks! You reinforced some things I was thinking about how the MySQL "engine" is optimized for searching/pulling data. Why use a screwdriver when a prybar is what's needed? Different tools for different jobs.

    The site I'm building will be relatively low-volume once it goes live so if I miss some optimizations or have some inefficient code, it won't be too bad but I very much want to learn best practices for other higher-volume sites/projects in the future.

    Many thanks (again)!!

    EZ

  5. #5
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775

    Re: Ah, Master....

    Originally posted by zandermander
    Vin, I was on your site a while back - quite amazing skillz you have! (I think there might be 110 queries in my whole site right now - not just one page!) Care to share the code you use for that footer? I think that would most definitely help me decide situation-by-situation how to optimize my code for speed...
    No way, baby! You're part of 'the energy'. The world would never forgive me...
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

  6. #6
    JPC Member
    Join Date
    Apr 2003
    Posts
    16
    No way, baby! You're part of 'the energy'. The world would never forgive me...
    Vin, information wants to be free and Google knows all. You won't have to worry about forgiveness from anyone though. Here's a nice page that explains it:

    http://www.webclass.ru/tut.php?tut=218

  7. #7
    Yeah, I know a LOT! Vin DSL's Avatar
    Join Date
    Mar 2003
    Location
    Arizona Uplands
    Posts
    10,775
    Originally posted by zandermander

    Vin, information wants to be free and Google knows all...
    Lesson number 2: Knowledge is power. I explain things only to those which have no possibility of understanding what I'm talking about, nor the ability to use it. This post is a prime example. You understood what I said earlier, but now you don't have a clue. There is no danger in that...
    DISCLAIMER Any resemblance between the views expressed above and those of the owners and operators of this system is purely coincidental. Any resemblance between these views and my own are non-deterministic. The existence of Vin DSL is questionable. The existence of views in the absence of anyone to hold them is problematic. The existence of the reader is left as an exercise in the second-order coefficient.

    No Guts, No Story! VinDSL © 2010

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •