What is the difference between a partition key and clustering key?

Beginner

Answer

  • Partition Key: Determines which node stores the data (distribution)
  • Clustering Key: Determines the sort order within a partition (organization)
CREATE TABLE events (
    user_id UUID,
    event_time TIMESTAMP,
    event_type TEXT,
    data TEXT,
    PRIMARY KEY (user_id, event_time, event_type)
);
  • user_id is the partition key
  • event_time and event_type are clustering keys
  • All events for a user are stored together, sorted by time and type