我的应用程序当前执行以下操作(最小示例):
UPDATE counts SET count = count + 1 WHERE id = 1
UPDATE counts SET count = count + 1 WHERE id = 2
UPDATE counts SET count = count + 1 WHERE id = 2
UPDATE counts SET count = count + 1 WHERE id = 3
UPDATE counts SET count = count + 1 WHERE id = 3
但是每秒有几千次更新,并且在高峰流量时太慢了。
因此,我想通过组合语句来加快速度。以下内容当然不起作用:
UPDATE counts SET count = count + 1 WHERE id IN (1, 2, 2, 3, 3)
count
和2
的{{1}}是错误的(从1代替2)。
我是否必须手动将其拆分为以下内容:
3
还是直接在语句中有更聪明的方法?
答案 0 :(得分:2)
您可以使用非1的数字加到计数中。 (从功能上讲,您可以;您必须测试性能。)
cqlsh> UPDATE mykeyspace.counts SET count = count + 1 where k1=1;
cqlsh> UPDATE mykeyspace.counts SET count = count + 1 where k1=2;
cqlsh> UPDATE mykeyspace.counts SET count = count + 1 where k1=3;
cqlsh> select * from mykeyspace.counts ;
k1 | count
----+-------
1 | 1
2 | 1
3 | 1
(3 rows)
cqlsh> UPDATE mykeyspace.counts SET count = count + 57 where k1=1;
cqlsh> select * from mykeyspace.counts ;
k1 | count
----+-------
1 | 58
2 | 1
3 | 1
(3 rows)
cqlsh> UPDATE mykeyspace.counts SET count = count + 65 where k1 in (2,3);
cqlsh> select * from mykeyspace.counts ;
k1 | count
----+-------
1 | 58
2 | 66
3 | 66