SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What is a result resource id actually pointing to?

    Hi,

    Sorry if this question is elementary...

    I'm just wondering about efficiency in database queries. At work, I need to access data from MySQL, MS SQL, and Oracle. I was wondering, when you query from PHP and you have the result resource id, what memory is that result data in? The database? or PHP?

    I've been through the mechanics of connecting and grabbing data from all, but never read an explanation of what is happening and where it's happening behind the scenes.

    So, you've done the query, you have a result pointer. Before you fetch a row, all rows, or anything, where is the data, and should I vary how I grab the data depending on whether it's a local DB, or a remote DB for efficiency sake?

    Thanks

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A result resource is just a variable holding a reference to an external resource. So the actual result set is stored in the database and PHP holds a reference to this. The only real consideration is wether to free that result set with mysql_free_result() for example, but that's only needed in rare circumstances as the result is freed automatically when the script ends.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    are you sure markl999?

    the existance of this function makes me think mysql_query() buffers the results into php memory:
    mysql_unbuffered_query()

    or does that just tell mysql to not buffer it?

  4. #4
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    are you sure markl999?

    the existance of this function makes me think mysql_query() buffers the results into php memory:
    mysql_unbuffered_query()

    or does that just tell mysql to not buffer it?
    mysql_query() fetches and buffers the entire result set. mysql_unbuffered_query() does not - it will return rows as soon as they are available (even before the query has finished executing!). Advantages: less memory, faster access to data. Disadvantages: no mysql_num_rows() or mysql_data_seek(), and you must finish reading the result set before you can send a new query.

    Quote Originally Posted by Joe_Rocket
    So, you've done the query, you have a result pointer. Before you fetch a row, all rows, or anything, where is the data, and should I vary how I grab the data depending on whether it's a local DB, or a remote DB for efficiency sake?
    The data usually sits in PHP memory. Some functions, like mysql_unbuffered_query, conserve memory by only fetching rows as they are read, but they have a cost.

    Really, unless you're working with massive result sets and getting large numbers of simultaneous hits, it's not worth the hassle. If it does become a hassle, try fetching smaller result sets (LIMIT statement).

    Don't become too overly concerned with premature optimization. Hardware is cheap. Programmer time is not.
    This space for rent.

  5. #5
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by clamcrusher
    are you sure markl999?
    I thought I was .. but I was wrong Thanks for the correction.


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
  •