Flite Careers

Wide Rows in Cassandra CQL

If you use Cassandra and you are a CQL native (meaning you never used the thrift interface) then the term “wide rows” can be confusing. Here’s the simplest definition I have come up with:

  • If the primary key only contains the partition key then the table has skinny rows
  • If the primary key contains columns other than the partition key then the table has wide rows

In CQL “wide rows” just means that there can be more than one row per partition. Thus a table with a single column primary key has skinny rows, and a table with a compound primary key can have wide or skinny rows depending on the partition key.

Another way to put it is that CQL tables with clustering columns have wide rows.

As an example, this CQL table has skinny rows because tweet_id is the only column in the primary key:

1
2
3
4
5
CREATE TABLE tweets (
  tweet_id uuid PRIMARY KEY,
  author varchar,
  body varchar
 );

This CQL table has wide rows because tweet_id is in the primary key but is not part of the partition key:

1
2
3
4
5
6
7
CREATE TABLE timeline (
  user_id varchar,
  tweet_id uuid,
  author varchar,
  body varchar,
  PRIMARY KEY (user_id, tweet_id)
);

However, if I put both of the primary key columns in the partition key, then I would have skinny rows:

1
2
3
4
5
6
7
CREATE TABLE timeline (
  user_id varchar,
  tweet_id uuid,
  author varchar,
  body varchar,
  PRIMARY KEY ((user_id, tweet_id))
);

To finish it off here’s an example of a CQL table with a compound primary key, a composite partition key, and wide rows. It has wide rows because event_time is in the primary key but is not part of the partition key:

1
2
3
4
5
6
7
CREATE TABLE temperature_by_day (
   weatherstation_id text,
   date text,
   event_time timestamp,
   temperature text,
   PRIMARY KEY ((weatherstation_id,date),event_time)
);

For further reading, I recommend Jonathan Ellis’s excellent post on this subject.


Comments