Column Families

A column family is a group of columns in a table that are stored as a single key-value pair in the underlying key-value store. The reduced number of keys results in a smaller storage overhead and, even more significantly, in improved performance during INSERT, UPDATE, and DELETE operations.

This page explains how CockroachDB organizes columns into families as well as cases in which you might want to manually override the default behavior.

Default behavior

When a table is created, all columns are stored as a single column family.

This default approach ensures efficient key-value storage and performance in most cases. However, when frequently updated columns are grouped with seldom updated columns, the seldom updated columns are nonetheless rewritten on every update. Especially when the seldom updated columns are large, it's more performant to split them into a distinct family.

Manual override

Assign column families on table creation

To manually assign a column family on table creation, use the FAMILY keyword.

For example, let's say we want to create a table to store an immutable blob of data (data BYTES) with a last accessed timestamp (last_accessed TIMESTAMP). Because we know that the blob of data will never get updated, we use the FAMILY keyword to break it into a separate column family:

  1. > CREATE TABLE test (
  2. id INT PRIMARY KEY,
  3. last_accessed TIMESTAMP,
  4. data BYTES,
  5. FAMILY f1 (id, last_accessed),
  6. FAMILY f2 (data)
  7. );
  1. > SHOW CREATE users;
  1. +-------+---------------------------------------------+
  2. | Table | CreateTable |
  3. +-------+---------------------------------------------+
  4. | test | CREATE TABLE test (
  5. |
  6. | | id INT NOT NULL,
  7. |
  8. | | last_accessed TIMESTAMP NULL,
  9. |
  10. | | data BYTES NULL,
  11. |
  12. | | CONSTRAINT "primary" PRIMARY KEY (id),
  13. |
  14. | | FAMILY f1 (id, last_accessed),
  15. |
  16. | | FAMILY f2 (data)
  17. |
  18. | | ) |
  19. +-------+---------------------------------------------+
  20. (1 row)

Note:
Columns that are part of the primary index are always assigned to the first column family. If you manually assign primary index columns to a family, it must therefore be the first family listed in the CREATE TABLE statement.

Assign column families when adding columns

When using the ALTER TABLE .. ADD COLUMN statement to add a column to a table, you can assign the column to a new or existing column family.

  • Use the CREATE FAMILY keyword to assign a new column to a new family. For example, the following would add a data2 BYTES column to the test table above and assign it to a new column family:
  1. > ALTER TABLE test ADD COLUMN data2 BYTES CREATE FAMILY f3;
  • Use the FAMILY keyword to assign a new column to an existing family. For example, the following would add a name STRING column to the test table above and assign it to family f1:
  1. > ALTER TABLE test ADD COLUMN name STRING FAMILY f1;
  • Use the CREATE IF NOT EXISTS FAMILY keyword to assign a new column to an existing family or, if the family doesn't exist, to a new family. For example, the following would assign the new column to the existing f1 family; if that family didn't exist, it would create a new family and assign the column to it:
  1. > ALTER TABLE test ADD COLUMN name STRING CREATE IF NOT EXISTS FAMILY f1;
  • If a column is added to a table and the family is not specified, it will be added to the first column family. For example, the following would add the new column to the f1 family, since that is the first column family:
  1. > ALTER TABLE test ADD COLUMN last_name STRING;

Compatibility with past releases

Using the beta-20160714 release makes your data incompatible with versions earlier than the beta-20160629 release.

See also

Was this page helpful?
YesNo