Cassandra 中的 Counter Vs Int 列? [英] Counter Vs Int column in Cassandra?

查看:20
本文介绍了Cassandra 中的 Counter Vs Int 列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Cassandra 的新成员.我无法理解在表中使用计数器的优势是什么(如果非计数器列不是复合 PRIMARY KEY 的一部分,甚至可以在不同的表中使用)?为什么我们不使用 Int 类型的列,当我会有一些像 x=x++ 这样的语句时;使用 int 或 counter 有什么区别?
是否可以在 Cassandra 中对 Int 类型使用增量或减量?

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-writelock 来执行 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:

  1. 锁定当前记录
  2. 读取x的当前值,加1
  3. 写回 x 的新值
  4. 解除锁定

Counter 类型允许在不需要先读后写锁定

这篇关于Cassandra 中的 Counter Vs Int 列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆