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.
Adding an index to an existing table with many rows, however, can take a long time or even fail entirely. Here’s a quick tip to deal with those situations.
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:
# Create a new table with the same characteristics as the old table CREATE TABLE new_table LIKE old_table; # Add any indices you'd like to the new table ALTER TABLE new_table ADD INDEX column_id (column_id); # Copy data over from the old table to the new table # Note that the columns must be the same for this to work INSERT INTO new_table SELECT * FROM old_table; # Delete the old table and rename the new table DROP TABLE old_table; ALTER TABLE new_table RENAME TO old_table;