在卡桑德拉存储最后触动时间的最佳方式

时间:2015-06-15 21:39:33

标签: database postgresql cassandra

我在Postgres的User表中存储了最后一次触摸的时间,但是有很多频繁的更新和足够的争用,我可以看到3个相同更新死锁的示例。

Cassandra似乎更适合这个 - 但我应该专门用这个目的吗?而且我不需要旧的时间戳,只需要最新的时间戳。我应该使用Cassandra以外的东西吗? 如果我应该使用Cassandra,有关表格属性的任何提示吗?

我想到的表格:

CREATE TABLE ksp1.user_last_job_activities (
    user_id bigint,
    touched_at timeuuid,
    PRIMARY KEY (user_id, touched_at)
) WITH CLUSTERING ORDER BY (touched_at DESC)
    AND bloom_filter_fp_chance = 0.01
    AND caching = '{"keys":"ALL", "rows_per_partition":"NONE"}'
    AND comment = ''
    AND compaction = {'min_threshold': '4', 'class': 'org.apache.cassandra.db.compaction.SizeTieredCompactionStrategy', 'max_threshold': '32'}
    AND compression = {'sstable_compression': 'org.apache.cassandra.io.compress.LZ4Compressor'}
    AND dclocal_read_repair_chance = 0.1
    AND default_time_to_live = 0
    AND gc_grace_seconds = 864000
    AND max_index_interval = 2048
    AND memtable_flush_period_in_ms = 0
    AND min_index_interval = 128
    AND read_repair_chance = 0.0
    AND speculative_retry = '99.0PERCENTILE';

更新

谢谢!我在写时间做了一些实验,因为我还是要编写一个值,我只是写了时间。

像这样:

CREATE TABLE simple_user_last_activity (
    user_id bigint,
    touched_at timestamp,
    PRIMARY KEY (user_id)
);

然后:

INSERT INTO simple_user_last_activity (user_id, touched_at) VALUES (6, dateof(now()));
SELECT touched_at from simple_user_last_activity WHERE user_id = 6;

由于triggered_at不再在主键中,因此每个用户只存储一条记录。

更新2

我还有另外一个选择。我也可以存储job_id,这为分析提供了更多数据:

CREATE TABLE final_user_last_job_activities (
    user_id bigint,
    touched_at timestamp,
    job_id bigint,
    PRIMARY KEY (user_id, touched_at)
) 
WITH CLUSTERING ORDER BY (touched_at DESC)
AND default_time_to_live = 604800;

添加1周TTL会处理过期记录 - 如果没有,我会返回当前时间。

INSERT INTO final_user_last_job_activities (user_id, touched_at, job_id) VALUES (5, dateof(now()), 5);
INSERT INTO final_user_last_job_activities (user_id, touched_at, job_id) VALUES (5, dateof(now()), 6);
INSERT INTO final_user_last_job_activities (user_id, touched_at, job_id) VALUES (5, dateof(now()), 7);
INSERT INTO final_user_last_job_activities (user_id, touched_at, job_id) VALUES (5, dateof(now()), 6);

SELECT * FROM final_user_last_job_activities LIMIT 1;

这给了我:

 user_id | touched_at               | job_id
---------+--------------------------+--------
       5 | 2015-06-17 12:43:30+1200 |      6

简单的基准测试显示在存储或读取较大的表时没有显着的性能差异。

2 个答案:

答案 0 :(得分:4)

因为c *是最后一次写入获胜,所以您可以简单地保留每行的最新版本。

正如MSD建议的那样,您可以使用writetime来缩短写入时间。但要小心,因为这是特定于列的,您不能在主键列上使用写入时间。例如,在表格中如下:

cqlsh> create TABLE test.test ( a int, b int, c int, d int, primary key (a))
   ... ;
cqlsh> insert INTO  test.test (a, b, c, d) VALUES ( 1,2,3,4)
   ... ;

cqlsh> select * from test.test
   ... ;

 a | b    | c | d
---+------+---+------
 1 |    2 | 3 |    4

(2 rows)

cqlsh> insert into test.test (a,c) values (1, 6);
cqlsh> select * from test.test ;

 a | b    | c | d
---+------+---+------
 1 |    2 | 6 |    4

(2 rows)
cqlsh> select writetime(a), writetime(b), writetime(c), writetime(d) from test.test
   ... ;
InvalidRequest: code=2200 [Invalid query] message="Cannot use selection function writeTime on PRIMARY KEY part a"

cqlsh> select  writetime(b), writetime(c), writetime(d) from test.test  ;

 writetime(b)     | writetime(c)     | writetime(d)
------------------+------------------+------------------
 1434424690700887 | 1434424690700887 | 1434424702420929

否则,您可以添加带有时间戳的cql列:

create TABLE test.test ( a int, b int, c int, d int, touched_at timeuuid, primary key (a)) ;

一些快速的基准测试可以帮助您确定哪个性能更高。

答案 1 :(得分:0)

Cassandra每列都隐含支持writetime。请参阅this,看起来就像您在这里寻找的那样。

相关问题