Skip to content
Andrew Geweke edited this page Dec 19, 2013 · 1 revision

You should always, if possible, define flex columns as NULLable. (This is the default in ActiveRecord migrations; simply do not add null => false.)

Why? Because if you have no data present in any fields in the flex column, flex_columns is smart enough to set the underlying column to SQL NULL. Databases can often store this with a single byte (or even less — some databases have a bitmask in the record header indicating which fields are NULL), so this is considerably more efficient than storing an empty string.

If your column is defined as non-NULL, flex_columns is also smart enough to recognize this; it will store the empty string ("") in the column if there is no data defined for any flex-column field. Technically, this is not valid JSON. However, the shortest possible JSON string is two bytes longer. While saving two bytes may seem silly, if you have a database with tens or hundreds of millions of rows, and very few of them contain any data in a flex column, the net savings is actually very substantial and can affect performance quite a bit.

(flex_columns will happily read rows with the empty JSON string, "{}", in them, and interpret this identically to the empty string or SQL NULL.)

Clone this wiki locally