Querying strings vs id numbers?

Discussion in 'MySQL' started by UCDaZ, May 8, 2009.

  1. #1
    I have a quick question concerning mysql. Is it better query based on the primary key or a field in the table?
    Does it really take up that much resources when querying a varchar field over a primary key?

    I see a lot of sites now that don't use IDs in their url
    for example, www.domain.com/content-title instead of www.domain.com/23
     
    UCDaZ, May 8, 2009 IP
  2. freelistfool

    freelistfool Peon

    Messages:
    1,801
    Likes Received:
    101
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Numbers are almost always faster than varchars. Numbers are stored in less space so you get more keys on each index page in the database. As a result, you traverse fewer index pages to get to any individual key value.
     
    freelistfool, May 9, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Numbers are better but if you have an index on the column, then the speed can be acceptable. A lot of it depends on the total usage that you're expecting. A few thousand queries per day on a varchar or char table with an index is pretty much negligible. Also, using a front-end cache can reduce the load on the DB a lot.
     
    jestep, May 11, 2009 IP