Exploring Column Families in CockroachDB

Posted on May 1, 2020

In this video, I break down what Column Families are in CockroachDB, why you’d want to use them, and how you can explore them in more detail for yourself.

Column families are a concept that allows you to change how rows are stored in a SQL table, to improve performance and reduce contention. By default, all of the columns of a row are stored in a single database key. With column families, you can ask the database to store the columns in multiple keys!

You might want to do this, for example, if one column is read or written much more frequently than the others: giving it its own column family can reduce the amount of contention on that row, and improve the performance of fetching data from the row.

To create a table with columns in CockroachDB, it’s very easy: add them to the CREATE TABLE statement. For example, if you had a users table with a seldom-read JSON payload, you could put it in its own column family:

CREATE TABLE users (
    id INT PRIMARY KEY
    name STRING,
    big_metadata_payload JSON,
    FAMILY (id, name),
    FAMILY (big_metadata_payload)
)