high and low cardinality in Cassandra

后端 未结 1 1200
自闭症患者
自闭症患者 2021-01-12 02:37

I keep coming across these terms: high cardinality and low cardinality in Cassandra.

I don\'t understand what exactly they mea

1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-12 03:00

    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

    0 讨论(0)
提交回复
热议问题