Optimize for Large Number of Columns in a Wide MySQL Table

by Yang Yang on October 19, 2014

In addition to necessary indexes and proper SELECT queries (only select needed columns), for wide tables, 2 basic tips apply well:

  1. Define your column as small as possible.
  2. Avoid using dynamic columns such as VARCHAR or TEXT as much as possible when you have large number of columns per table. Try using fixed length columns such as CHAR. This is to trade off disk storage for performance.

For instance, for columns ‘name’, ‘gender’, ‘age’, ‘bio’ in ‘person’ table with as many as 100 or even more columns, to maximize performance, they are best to be defined as:

  1. name – CHAR(70)
  2. gender – TINYINT(1)
  3. age – TINYINT(2)
  4. bio – TEXT

The idea is to define columns as small as possible and in fixed length where reasonably possible. Dynamic columns should be to the end of the table structure so fixed length columns ALL come before them. MySQL would then have a better time predicting the position of a column to perform select or update, otherwise it would have to read through the entire row byte by byte to find it.

It goes without saying this would introduce tremendous disk storage wasted with large amount of rows, but as you want performance I guess that would be the cost.

Another tip is as you go along you would find columns that are much more frequently used (selected or updated) than the others, you should separate them into another table to form a one to one relationship to the other table that contains infrequent used columns and perform queries with less columns involved.

Comments on this entry are closed.

Previous post:

Next post: