SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which way faster (array,mysql query) ?!

    Hi ..

    I have more than 11k record in the DB, which way is faster to view the data with different conditions ?

    1. select all the rows in the DB and store it in array, then fetching the array with different conditions (1 query).


    2. select data from DB with different conditions (many queries).




    thx in advance.

  2. #2
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If it is only 11kb I think I would go with the first option, still it does depend on how much of the data you need to use etc.
    In either case you shouldn't have to worry about it I think
    - website

  3. #3
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry ... not 11kb ... 11,000 !

  4. #4
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    or you can say almost 10MB !

  5. #5
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then I would go with the queries unless they are extreamly many.
    - website

  6. #6
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmmm ... anyway .. i hade done it with 1 query and storing the data in array, then fetching it to small arrays by conditions !

    part of 'forech'
    PHP Code:
        foreach($StatusArray  as $Value)
        {
          
    //Today
          
    if(date("d" $Value[visittime]) == $Today$TodayVisits[] = $Value[visittime];
          
    //Yesterday
          
    if(date("d" $Value[visittime]) == $Today-1$YesterdayVisits[] = $Value[visittime];
          
    //else date//
          
    if(!in_array($Value[visittime] ,$TodayVisits) and  !in_array($Value[visittime] ,$YesterdayVisits))  $ElseVisits[] = $Value[visittime];
        } 

  7. #7
    Non-Member Icheb's Avatar
    Join Date
    Mar 2003
    Location
    Germany
    Posts
    1,474
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It depends on how much of it you need. If you need everything, use one query and get everything.

    If you only need specific rows, use one query and specify the rows you need in the WHERE clause.

    The fewer queries you run, the faster your page displays.

  8. #8
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    but there is some thing red line in the middle, should you select huge result which you will only use small part of or select a few times very little data, it all depends on how big your table is and how much data you need from it, as Icheb had already told.
    - website

  9. #9
    SitePoint Guru Richi's Avatar
    Join Date
    Jul 2001
    Location
    Hu,Bp.
    Posts
    842
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see ... the data is really huge, the client wanna make custom web statics from old DB, and get the visitors country via ip, the ipcountry table is over 20MB, when you wanna see every ip u have in web static table (~10MB) will take huge time, but stupid client cann`t understand this, i told him to sort the data by data ... the answer was "NO" ... i didn`t try it yet, query data from table 10mb and verse by table 20MB size !!

    however, i had made 1 query and sorting by foreach, I think sorting array faster than query, and this what i had found from my stopwatch

    thx for reply


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
  •