Cassandra UDT as primary key

The official documentation tells us not to use UDTs for primary keys. Is there a specific reason for this? What will be the lack of capacity in this?

+3


source to share


1 answer


This proposal was intended to discourage users from indiscriminately using UDTs for PK columns. The main motivation for UDTs in its current incarnation (that is, given that Cassandra supports "frozen" UDTs) is to store more complex values ​​within collections. Outside of collections, the UDT can use it, but it's worth asking twice if you need to. For example:

CREATE TYPE myType (a text, b int);



CREATE TABLE myTable (id uuid PRIMARY KEY, v frozen<myType>);

it is often not very sensible that you lose the ability to update va without updating vb So this is actually more flexible:

CREATE TABLE myTable (id uuid PRIMARY KEY, a text, b int);

This trivial example indicates that UDTs outside of collections are not always good, and this also applies to primary key columns. It is not necessarily better to do this:

CREATE TYPE myType (a text, b int);



CREATE TABLE myTable (id frozen<myType> PRIMARY KEY);

the simpler:

CREATE TABLE myTable (a text, b int, PRIMARY KEY ((a, b)))

Also, as far as the primary key is concerned, any complex UDT probably doesn't make sense. Consider even a moderately complex type, for example:

CREATE TYPE address ( number int, street text, city text, phones set<text> )

Using this type inside a primary key is almost certainly not very useful as the PC identifies strings and therefore 2 addresses that are the same except for a set of phones will not identify the same string. There are not many situations where this would be desirable. More generally, PK tends to be relatively simple, and you can have fine-grained control over the clustering columns and therefore UDTs are rarely good candidates.

As such, UDTs on PK columns are not always bad, just not often useful in this context, and therefore users should not scrutinize how to use UDTs on PK columns just because they are allowed.

+5


source







All Articles