Search code examples
mysqloptimizationkeyvarchar

Optimal key length for a varchar column in MySQL InnoDB


I have the following table:

CREATE TABLE cities (
city_id INT,
city_name VARCHAR(255),
-- some other columns
PRIMARY KEY (city_id)
) ENGINE=InnoDB

I want to add an index for city_name so that I can search on it using the LIKE operator. How do I figure out the optimal key length?

EDIT: This table will be populated with US cities, and the usage case is read-only.


Solution

  • You want the key length to be just wide enough so that most cases are unique and no wider. If you have existing data, look how many characters wide the key would have to be so that almost all cities can be represented as distinct strings in the selected width. If you don't have existing data, google a list of cities for the geography you're interested in (e.g. USA, whole world) and use that as your guide.

    For example, if your key width is only 5 characters wide, cities like

    Saint Louis
    Saint Petersberg
    

    would both be represented as "Saint" in the key, causing the key to be a less-than-perfect discriminator.

    If that were the whole set of cities to be considered, a key length of 7 would be ideal. Your keys would be

    Saint L
    Saint P
    

    Here's the MySQL manual page that covers that:

    http://dev.mysql.com/doc/refman/5.6/en/create-index.html

    Look for the text CREATE INDEX part_of_name ON customer (name(10));

    For a list of cities, if you want to just consider the most populous cities:

    http://en.wikipedia.org/wiki/List_of_United_States_cities_by_population

    For a full list:

    http://geonames.usgs.gov/domestic/download_data.htm

    (Topical Gazetteers, Populated Places)