问题
I keep coming across these terms: high cardinality
and low cardinality
in Cassandra
.
I don't understand what exactly they mean. what effects they have on queries and what is preferred. Please explain with example since that will be be easy to follow.
回答1:
The cardinality of X is nothing more than the number of elements that compose X. In Cassandra the partition key cardinality is very important for partitioning data.
Since the partition key is responsible for the distribution of the data across the cluster, choosing a low cardinality key might lead to a situation in which your data are not distributed.
Imagine you have a cluster of 20 nodes storing comments -- the RF is 2. Each comment has it's own vote going from 1 to 5. Now, since you want to easily retrieve comments by votes, you might be tempted to choose vote as partition key.
CREATE TABLE comments(vote int, content text, id uuid, PRIMARY KEY(vote, id));
In this situation the only key responsible for data distribution is vote, which has a very low cardinality since it can contains only 5 values (1,2,3,4,5). This means that, in the best situation 5 different nodes will be the owners of the 5 different partitions (which are "all comments with vote 1" ... "all comments with vote 5"), and again in best situation, with a RF of 2, 10 different nodes will hold your data. As you can see you have a 20 nodes cluster which isn't used more than 50% in best situation.
Data distribution is very important, that's why partition key cardinality matters a lot
HTH, Carlo
来源:https://stackoverflow.com/questions/25101176/high-and-low-cardinality-in-cassandra