Optimize this Mysql query

Discussion in 'MySQL' started by jalex, Dec 12, 2006.

  1. #1
    select count(xxx) where id='5'

    xxx column is INT type
    i have an index on id column
    doe's help or inimical if i add an index on xxx column?
     
    jalex, Dec 12, 2006 IP
  2. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #2
    You just need an index on id. The query as it sounds should be fast. If you have a very large table and are running the same query multiple times it may be better to cache the results of have an auto generated table that contains the counts.
     
    dct, Dec 12, 2006 IP
  3. jalex

    jalex Active Member

    Messages:
    184
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    the database is large and the query is slow.. have any effect if i have or not index on xxx column?
     
    jalex, Dec 12, 2006 IP
  4. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #4
    An index on xxx will not help, does it make any difference if you do count(*)
     
    dct, Dec 12, 2006 IP
  5. vitaminp

    vitaminp Peon

    Messages:
    202
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if its possible, use a cron to save this information every hour or so into a seperate table.
    a SELECT statement from the new table will obviously be fast.

    if you want the information updated manually, just run the update script manually from an admin panel or something.
     
    vitaminp, Dec 12, 2006 IP
  6. smallbuzz

    smallbuzz Peon

    Messages:
    125
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    adding an index on id is the only thing you can do.
    count(*) and count(column name) is the same.
     
    smallbuzz, Dec 14, 2006 IP
  7. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    $sql = select count(8) where id=5;

    Will give the o/p..
     
    weknowtheworld, Dec 19, 2006 IP