这是我的情况:
- VPS 服务器:
- 1 Cassandra 数据库:
- 键空间:“atim_cloud”
- 表:“消息”
- 键空间:“atim_cloud”
- 1 Cassandra 数据库:
CREATE TABLE atim_cloud.messages (
deviceid text,
channelname text,
time timestamp,
avgsignal float,
latitude float,
longitude float,
rssi float,
snr float,
stationid text,
value blob,
valuetype text,
PRIMARY KEY ((deviceid, channelname), time)
) WITH CLUSTERING ORDER BY (time 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 INDEX messages_deviceid_idx ON atim_cloud.messages (deviceid);
CREATE INDEX messages_channelname_idx ON atim_cloud.messages (channelname);
CREATE INDEX messages_time_idx ON atim_cloud.messages (time);
我的问题:
该表是为大量数据(数百万行)而创建的。 简单的请求工作正常,例如:
SELECT * FROM messages WHERE deviceid ='1DB8D';
我得到:
deviceid | channelname | time | avgsignal | latitude | longitude | rssi | snr | stationid | value | valuetype
----------+-------------+--------------------------+-----------+----------+-----------+--------+-------+-----------+------------+-----------
1DB8D | INDEX1 | 2015-07-26 22:21:59+0200 | 9.9 | 45 | 6 | -125.5 | 9.66 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 22:11:58+0200 | 9.89 | 45 | 6 | -125.5 | 9.85 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 22:01:59+0200 | 9.87 | 45 | 6 | -123.5 | 10.08 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 21:51:59+0200 | 9.83 | 45 | 6 | -125.5 | 9.8 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 21:41:59+0200 | 9.83 | 45 | 6 | -124.5 | 10.02 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 21:31:58+0200 | 9.8 | 45 | 6 | -126.5 | 10.35 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 21:21:59+0200 | 9.78 | 45 | 6 | -122.5 | 9.91 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 21:11:59+0200 | 9.82 | 45 | 6 | -130.5 | 8.85 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 21:01:59+0200 | 9.79 | 45 | 6 | -129.5 | 10.11 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 20:51:58+0200 | 9.77 | 45 | 6 | -124.5 | 10.06 | 0E00 | 0x00000000 | int
1DB8D | INDEX1 | 2015-07-26 20:41:59+0200 | 9.78 | 45 | 6 | -123.5 | 9.52 | 0E00 | 0x00000000 | int
但是当我使用时间戳的计算执行一些更复杂的请求时,例如: (这种情况会发生几次,但并非总是如此)
SELECT * FROM messages WHERE deviceid = '1DB8D' AND time >= 1437981692831 LIMIT 500 ALLOW FILTERING ;
或者简单地:
SELECT COUNT(*) FROM messages ;
我得到(几秒钟后。我猜超时):
errors={}, last_host=127.0.0.1
您有什么建议可以解决我的问题吗? 我正在寻找有关索引或主键的一些建议,但我没有找到任何东西。
如果您有一些执行此数据表的提示,我很高兴听到。那么多集群呢?我不明白这一切。
谢谢;)
最佳答案
仅建议对基数较低的字段使用二级索引。对于基数较高的字段(例如时间字段),它们的效率非常低。这就是为什么当您在查询中使用时间字段时会收到超时错误的原因。
在 Cassandra 中,您应该专注于使用良好的主键,而不是通过创建辅助键来修复架构问题。
关于cassandra - 带有时间戳范围的 SELECT 请求中出现超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31649847/