Partition Key Vs Primary Key Vs Sort Key Vs Clustering Key
Primary Key
A normal SIMPLE primary key in the database
create table customer (
id bigint PRIMARY KEY,
data text
)
Partition Key, Clustering Key and Sort Key
A primary key can be COMPOSITE (COMPOUND) which means it includes multiple columns
create table customer_order (
customer_id bigint,
order_id bigint
created_date timestamp,
PRIMARY KEY(customer_id, order_id)
)
In this case, the primary key is both customer_id and order_id
. As a result:
customer_id
: partition key- How the data is being distributed across the nodes
order_id
: sort key, clustering key- How the data is being sorted within a partition
Composite / compound key is just any regular key.
Note
We can have multiple clustering key or partition key, for example:
create table customer_order (
customer_id bigint,
account_id bigInt,
order_id bigint
created_date timestamp,
data text,
PRIMARY KEY((customer_id, account_id), created_date, order_id)
)
In this case,
- Partition key:
customer_id
,account_id
- Clustering key / Sort key:
created_date
,order_id