Cassandra中的Counter Vs Int列? [英] Counter Vs Int column in Cassandra?
问题描述
我是Cassandra的新手。我无法理解在表中使用计数器的优势(如果非计数器列不是复合PRIMARY KEY的一部分,甚至在其他表中使用计数器的优势)?为什么我不使用Int类型的列,当我有x = x ++之类的语句时;
可以在Cassandra中使用Int Type的增量或减量吗?
I'm new in Cassandra. I can't understand what is the advantage of using counter in a table (or even in a different table if the non-counter columns are not part of the composite PRIMARY KEY)? Why we don't use a column with Int type, when I will have some statements like x=x++; what is the different between using int or counter?
Is that possible to use increments or decrements for Int Type in Cassandra at all?
推荐答案
为什么我们不使用Int类型的列,当我有一些语句时像x = x ++;使用int或counter有什么区别?
Why we don't use a column with Int type, when I will have some statements like x=x++; what is the different between using int or counter?
因为使用普通的Int列需要 read-before-write 和锁定,例如 x = x ++
Because using a normal Int column would require read-before-write and lock for operations like x=x++
当您可以对同一值进行并发更新时,保证 x = x ++
行为一致的唯一方法是:
Indeed, for a distributed database when you can have concurrent updates on the same value, the only way to guarantee consistent behaviour for x=x++
is:
- 锁定当前记录
- 读取x的当前值,将其递增1
- 写回x的新值
- 释放锁
- lock the current record
- read the current value of x, increment it by 1
- write back the new value of x
- release the lock
Counter 类型允许同时递增/递减值,而无需先写先读或锁定
Counter type allows concurrent increment/decrement on the value without neither requiring read-before-write nor locking
这篇关于Cassandra中的Counter Vs Int列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!