SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2006
    Location
    Closer than you think
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Storing Arrays as a Session

    Is there any reason why I wouldn't want to store an array with Thousands of records as a session variable?

    I am trying to improve a pagination script and I am trying to avoid having to do a DB call each time they view the next set of records.
    Live Well.

  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 pagination script wouldn't normally need to do that. It usually just needs to get the records required for the current page based on the current page number and the number of records to show per page, and the total number (just the number/count not the actual full records) of records in the database so it can work out how many page links to show.

    If you search this forum for 'pagination' you'll find many solutions.

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Session variables works by serializing the data to a string, and save it to a text file, with a unique name (Based on the session-id). When the session is restored, the text file is read, and the contents is used to re-create the variables.
    File I/O is very slow, and the serialization/de-serialization is CPU and memory heavy processes. So from a performance pov, it's a very expensive way of passing data from one script to another. Databases on the other hand, are specialised in what they do, and therefore you are often better off querying the database again, than caching data in the session.

    That said, pagination is usually done by only selecting the rows which you actually need, from the database. With MySql, you would use a combination of LIMIT and OFFSET to do that. You'll need to make a SELECT COUNT(*) WHERE ... to make the correct calculation. As markl999 said, this is a topic hich has been touched many times on these forums.

  4. #4
    Who turned the lights out !! Mandes's Avatar
    Join Date
    May 2005
    Location
    S.W. France
    Posts
    2,496
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WeakestLink View Post
    I am trying to improve a pagination script and I am trying to avoid having to do a DB call each time they view the next set of records.
    Just to add my voice to the chorus.

    Why would you want to get all the records in one swoop, take a search engine example, you may get 50,000 hits on a particular search, but you wouldnt consider storing all 50,000 in an array as the user would probably only view the first 3 or 4 pages before moving onto a different search, the same with your application, can you be sure that the user is going to go though all of your 1000's of records, if not its better to call just the 100 or so they will search in groups.
    A Little Knowledge Is A Very Dangerous Thing.......
    That Makes Me A Lethal Weapon !!!!!!!!

    Contract PHP Programming

  5. #5
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's a tutorial on pagination:
    http://codewalkers.com/tutorials/13/1.html


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
  •