SELECT performance for a hude table

Discussion in 'MySQL' started by hostingcoupon, Feb 4, 2008.

  1. #1
    Hi, I have a Huge mysql table (100M rows), and let's say the table have two columns, column A and column B. Column A is indexed, and Column B is not.

    So:
    SELECT * from table1 WHERE A='DATA_A'
    should be very fast.

    How about the performance of the following?
    (1) SELECT * from table1 WHERE A='DATA_A' AND B='DATA_B';
    (2) SELECT * from table1 WHERE B='DATA_B' AND A='DATA_A';

    Thank you
     
    hostingcoupon, Feb 4, 2008 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not an SQL efficiency expert however my understanding is that conditions are looked up in sequential order and therefore 1 would be quicker as the initial look up is based on the indexed column.
     
    AstarothSolutions, Feb 5, 2008 IP
  3. urstop

    urstop Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Both the statements should be equally good in your case. Most of the databases are clever enought to use the indexes to the maximum to give us optimal performace so in both the cases the SQL engine will use the index of A to select the matching rows and then from this list will use B to select the final set of rows.
     
    urstop, Feb 5, 2008 IP
  4. Petey

    Petey Peon

    Messages:
    68
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi hostingcoupon

    The optimizer will automagically rewrite your SQL to use any indexes it can to speed the query up so (1) and (2) should run in the same time.

    If you want to see the execution plan for a query then just add the command EXPLAIN before the SELECT:

    EXPLAIN SELECT * from table1 WHERE A='DATA_A' AND B='DATA_B';

    If you build you database using bulk loading then sometimes the query optimizer will choose not to use indexes because it doesn't know the real size of the table. It is always good practice to run the command "OPTIMIZE TABLE table1;" (if you are using MySQL) right after a bulk load.

    Petey
     
    Petey, Feb 5, 2008 IP
  5. LittleJonSupportSite

    LittleJonSupportSite Peon

    Messages:
    386
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I would also add some more indexes. Make some multiple unique fields bind to give a somewhat license plate or mac address if you will.

    SO fields 1+2+3 == Unique Index.

    This will allow for faster searching on full text.
     
    LittleJonSupportSite, Feb 5, 2008 IP
  6. hostingcoupon

    hostingcoupon Peon

    Messages:
    447
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you very much for your suggestions, actually, Column A is a MD5 sum, so it's almost unique.

    Thanks.
     
    hostingcoupon, Feb 5, 2008 IP