How to Quickly Add Indices to Large MySQL Tables

Whenever a MySQL query contains a WHERE conditional, the database server must find all the matching rows before it can execute any operations.  In order to optimize this matching, a database administrator might define an index on any columns he or she knows will be used more frequently in WHERE conditions.

As an analogy, imagine if you had an unorganized stack of business cards, and someone asked you to find Jon Smith’s telephone number.  You might have to look through every single business card in order to find Jon Smith, because he could be anywhere in the stack.  On the other hand, if you sorted the cards in alphabetical order by last name, you would be able to jump much more quickly to where Jon Smith might be found.  Furthermore, every time you receive a new business card, you could quickly insert it into its proper place (based on last name) and be sure that future lookups would continue to be quick.  This is the basic idea of MySQL indices.

Now, the easiest time to define the ordering is before we’ve even received a single card.  But let’s say we’ve collected a million cards before realizing that we’d also like to be able to index business contacts by their company.  It would take a long time to organize all that data in the proper order!

Fortunately, there’s a snazzy little trick for quickly adding indices to large MySQL tables.  The basic concept is to create a new table, add the column indices you want, and then import the data from the existing table into the new one.  Here’s how it goes in MySQL: