MySql: 4 ways to lower the impact of TEXT columns

Starting point

Normally there is a table with some small stuff like id/type/xxx_id(~50B) and a text column(default 768B + x) that is not needed all the time, because most users enter less than 255 chars or most queries only need the smaller columns.

  • text bloats table size
  • text makes queries slow
  • text bloats mysql query cache
  • text is always loaded into buffer pool (consumes ram)

Alternative Row format

With row ROW_FORMAT=​DYNAMIC those default 768B+x can be reduced to a mere default 20B+x, but requires innodb_file​_format=Barracuda.

1 to 1 Relationship

Keep all small columns on the first model and pull in the text from another model e.g. using translated_attributes.
Generates some overhead in the model but is reasonably fast.

Varchar column as cache + 1 to 1 Relationship

Only fetches the text/adds the relationship when needed. Useful when only a few texts are larger then 255 chars and the content is often needed. e.g. on original model:

def text_truncated(to=255)
  text = if to > 255 and text_cache.size == 255
    1_to_1_relationship.text
  else
    text_cache
  end
  text.truncate(to)
end

(this needs truncate on string)
And e.g. in the UI the text could always show truncated to 255 chars and only on click it is really loaded from the db.

Using covering index

Make an index that covers all the normal columns except the text, and FORCE INDEX on the queries.

Performance

Source:
A lot more details on the 1-to-1-relation ship / covering index can be found here

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s