Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cassandra: choosing a Partition Key

Tags:

I'm undecided whether it's better, performance-wise, to use a very commonly shared column value (like Country) as partition key for a compound primary key or a rather unique column value (like Last_Name).

Looking at Cassandra 1.2's documentation about indexes I get this:

"When to use an index: Cassandra's built-in indexes are best on a table having many rows that contain the indexed value. The more unique values that exist in a particular column, the more overhead you will have, on average, to query and maintain the index. For example, suppose you had a user table with a billion users and wanted to look up users by the state they lived in. Many users will share the same column value for state (such as CA, NY, TX, etc.). This would be a good candidate for an index."

"When not to use an index: Do not use an index to query a huge volume of records for a small number of results. For example, if you create an index on a column that has many distinct values, a query between the fields will incur many seeks for very few results. In the table with a billion users, looking up users by their email address (a value that is typically unique for each user) instead of by their state, is likely to be very inefficient. It would probably be more efficient to manually maintain the table as a form of an index instead of using the Cassandra built-in index. For columns containing unique data, it is sometimes fine performance-wise to use an index for convenience, as long as the query volume to the table having an indexed column is moderate and not under constant load."

Looking at the examples from CQL's SELECT for

"Querying compound primary keys and sorting results", I see something like a UUID being used as partition key... which would indicate that it's preferable to use something rather unique?

enter image description here

like image 972
andandandand Avatar asked Aug 11 '13 03:08

andandandand


People also ask

Does a partition key have to be unique Cassandra?

Yes the primary key has to be unique. Otherwise there would be no way to know which row to return when you query with a duplicate key.

How do you define a partition key?

A table partitioning key is an ordered set of one or more columns in a table. The values in the table partitioning key columns are used to determine in which data partition each table row belongs. To define the table partitioning key on a table use the CREATE TABLE statement with the PARTITION BY clause.


2 Answers

Indexing in the documentation you wrote up refers to secondary indexes. In cassandra there is a difference between the primary and secondary indexes. For a secondary index it would indeed be bad to have very unique values, however for the components in a primary key this depends on what component we are focusing on. In the primary key we have these components:

PRIMARY KEY(partitioning key, clustering key_1 ... clustering key_n)

The partitioning key is used to distribute data across different nodes, and if you want your nodes to be balanced (i.e. well distributed data across each node) then you want your partitioning key to be as random as possible. That is why the example you have uses UUIDs.

The clustering key is used for ordering so that querying columns with a particular clustering key can be more efficient. That is where you want your values to not be unique and where there would be a performance hit if unique rows were frequent.

The cql docs have a good explanation of what is going on.

like image 154
Lyuben Todorov Avatar answered Nov 26 '22 22:11

Lyuben Todorov


if you use cql3, given a column family:

CREATE TABLE table1 (   a1 text,   a2 text,   b1 text,   b2 text,   c1 text,   c2 text,   PRIMARY KEY ( (a1, a2), b1, b2) ) ); 

by defining a primary key ( (a1, a2, ...), b1, b2, ... )

This implies that:

a1, a2, ... are fields used to craft a row key in order to:

  • determine how the data is partitioned
  • determine what is phisically stored in a single row
  • referred as row key or partition key

b1, b2, ... are column family fields used to cluster a row key in order to:

  • create logical sets inside a single row
  • allow more flexible search schemes such as range range
  • referred as column key or cluster key

All the remaining fields are effectively multiplexed / duplicated for every possible combination of column keys. Here below an example about composite keys with partition keys and clustering keys work.

If you want to use range queries, you can use secondary indexes or (starting from cql3) you can declare those fields as clustering keys. In terms of speed having them as clustering key will create a single wide row. This has impact on speed since you will fetch multiple clustering key values such as:

select * from accounts where Country>'Italy' and Country<'Spain'

like image 41
natbusa Avatar answered Nov 26 '22 21:11

natbusa