Which way is the most efficient/fastest

Discussion in 'MySQL' started by wmburg, Jul 11, 2006.

  1. #1
    I'm trying to figure out which of the following ways would be the best if I am concerned about speed and efficiency. I'll be using PHP and MySQL.

    Let's say that I have 1000 numbers in the database for each account (1000's of accounts). When I get a new number I need to check to make sure that it isn't already in the database. Numbers will be checked once or twice an hour.

    Which would be better to store the data?

    1) Store all of the numbers for the accounts in seperate rows.
    Cons: Excessive rows. Could eventually be more CPU intensive with millions and millions of rows.

    2) Store all of the numbers for the accounts in an array in 1 row
    Cons: 1000's of numbers in 1 field. More CPU intensive searching the array? Not sure.

    NOW which way would be better to query the data?

    1)
    select * from numbers where account='1' and number='NEW_NUMBER'; OR
    select * from numbers where account='1' and number LIKE '%NEW_NUMBER%'; if using an array
    Cons: A lot of queries if I have to do this for every new number that I am checking for

    2) Use an OR between every new number. Use PHP to construct the OR side of the query.
    select * from numbers where account='1' and number='1' or number='2' or number='3' or number='4' or number='5' or number='1000';
    Cons: A very large single query if we are taking about checking 1000+ numbers. Change = to LIKE if using array.

    I hope this doesn't confuse you. If you have a better solution please let me know.
     
    wmburg, Jul 11, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    1 is by far the quickest and easiest to query. I'd do it that way. Also cleaner to store.
     
    T0PS3O, Jul 11, 2006 IP
  3. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #3
    I'm with T0PS (not literally) #1 is the best option and you could always do that in one query too
    
    select * from numbers where account='1' and number in ('1', '2', '3')
    
    Code (sql):
     
    dct, Jul 11, 2006 IP
  4. wmburg

    wmburg Active Member

    Messages:
    300
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    58
    #4
    Thanks guys. I've never used IN before. I'll check it out.

    BTW - When I used "array" above I meant delimited.
     
    wmburg, Jul 11, 2006 IP