question

praneethk29_179300 avatar image
praneethk29_179300 asked Erick Ramirez edited

Do the order of columns in the primary key matter?

Does the order of fields a and b in multi partition key matter for data distribution?

Primary key((a,b),c))

Primary key((b,a),c))

cassandradata modeling
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

saravanan.chinnachamy_185977 avatar image
saravanan.chinnachamy_185977 answered saravanan.chinnachamy_185977 edited

@praneethk29_179300 The order of fields does matter in multi partition key.

Tokens are hash values that partitioners use to determine where to store rows on each node. This value determines the node's position in the ring and what data the node is responsible for.

For Example in a 3 node cluster create 2 tables and switch the partition key order:

create table killer_video.email_by_title (email text,title text,year int,PRIMARY KEY ((email, title)));
create table killer_video.title_by_email (title text,email text,year int,PRIMARY KEY ((title, email)));

insert some data:

insert into killer_video.email_by_title (email,title,year) values ('joe@test.com','avatar',2009);
insert into killer_video.title_by_email (title,email,year) values ('avatar','joe@test.com',2009);

Get the endpoints that own the partition key as detailed in nodetool getendpoints

-bash-4.2$ nodetool getendpoints killer_video title_by_email "avatar:joe@test"
10.142.0.4
-bash-4.2$ nodetool getendpoints killer_video email_by_title "joe@test.com:avatar"
10.142.0.3

The 2 partition keys (the orders are switched) are owned by different nodes.

Share
10 |1000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.