Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6147563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:07:29+00:00 2026-05-23T19:07:29+00:00

I have the following table: CREATE TABLE cities ( city_id INT, city_name VARCHAR(255), —

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:07:30+00:00Added an answer on May 23, 2026 at 7:07 pm

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following table and values: create table TreeTable ( ID int primary
I have the following table: CREATE TABLE [dbo].[TableB]( [id] [int] NULL, [FileName] [varchar](20) NULL
I have the following table: create table tbl ( id int identity(1,1), val varchar(100)
I have following table structure: CREATE TABLE pilot_groups ( id INT PK, name VARCHAR(50),
I have the following table: create table indices ( id int primary key generated
I have the following table CREATE TABLE `prod_prices` ( `id` varchar(32) COLLATE utf8_unicode_ci NOT
I have the following table: create table ARDebitDetail(ID_ARDebitDetail int identity, ID_Hearing int, ID_AdvancedRatePlan int)
I have the following table: CREATE TABLE IF NOT EXISTS `customer_list` ( `id` INT
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
I have the following table: $sql = CREATE TABLE received_queries ( sender_screen_name varchar(50), text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.