suggest correct query

Discussion in 'MySQL' started by JEET, Mar 8, 2014.

  1. #1
    Hi,
    I'm trying to select a distinct field from DB like this

    
    select field1 from table where memberID='1' group by field1
    
    Code (markup):
    The DB has thousands of records. I want the query to read just 100 records "for each memberID" specified in query and then exit.
    I cannot use "limit 100 " because members might have less than 100 records, in which case the query will scan the whole table.
    What will be the query here?
    (scans 100 records of memberID specified and then exists)

    Thanks
     
    JEET, Mar 8, 2014 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #2
    If your table is set up correctly with the right datatypes and indexes it shouldn't matter if it has to query the whole table.
    Use limit.
     
    sarahk, Mar 8, 2014 IP
    ryan_uk likes this.
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    I think it'll matter in long run when table grows very large.
    I cannot delete data from this table, so at some point of time it'll take minutes to go through the entire table.
    My requirement is to select a certain distinct field of a user when they visit a section of the website. These distinct records will never exceed 100 records in total.
    So I'm thinking whats the point in reading all 2 million records or so...

    Is there any way to do this?
    thanks
     
    JEET, Mar 9, 2014 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,830
    Likes Received:
    4,541
    Best Answers:
    123
    Trophy Points:
    665
    #4
    Your post was #18904200 in the forum posts table - didn't take too long to come up for me - along with all the other posts in the thread and all the extra info around alerts, etc.
    Use indexes and you'll be fine.
     
    sarahk, Mar 9, 2014 IP
  5. ryan_uk

    ryan_uk Illustrious Member

    Messages:
    3,983
    Likes Received:
    1,022
    Best Answers:
    33
    Trophy Points:
    465
    #5
    If you are not too sure about this (sarahk's suggestion regarding indexes, and also in the other post she mentioned about using the right data types), you might be better off posting your table STRUCTURE (not the data, obviously) and we might be able to give some tips on fine tuning it.
     
    ryan_uk, Mar 9, 2014 IP
    sarahk likes this.