我是cassandra的新手,我想弄清楚为什么我不能通过created_at日期订购我的日志。 以下是表格描述,选择结果和我试图创建的选择语句。
cassandra@cqlsh:mytable> DESCRIBE TABLE mytable.log;
CREATE TABLE mytable.log (
id uuid,
created_at timestamp,
deleted boolean,
level text,
message text,
obj text,
obj_name text,
origin text,
user int,
PRIMARY KEY (id, created_at)
) WITH CLUSTERING ORDER BY (created_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 INDEX deleted_idx ON mytable.log (deleted);
CREATE INDEX level_idx ON mytable.log (level);
CREATE INDEX message_idx ON mytable.log (message);
CREATE INDEX origin_idx ON mytable.log (origin);
CREATE INDEX user_idx ON mytable.log (user);
cassandra@cqlsh:mytable> SELECT * FROM mytable.log WHERE "created_at" <= '2015-04-29 00:00:00' AND "user" = 20 LIMIT 10;
id | created_at | deleted | level | message | obj | obj_name | origin | user
--------------------------------------+--------------------------+---------+-------+---------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------+-----------------+------
a98a98d5-5710-431b-a23d-d78ece882763 | 2015-04-28 19:18:34-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:34.159619+00:00', 'id': 143L, 'content': u'192.213.216.16', 'change_date': 1430277514, 'owner_id': 20L} | Record | update_a_record | 20
893e9600-3d57-4b82-bdfd-41586023a90f | 2015-04-28 19:21:01-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:21:01.414393+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277661, 'owner_id': 20L} | Record | update_a_record | 20
f951b3ec-092a-4e9e-95c5-a6dce3363c29 | 2015-04-28 19:18:35-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:35.199869+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277515, 'owner_id': 20L} | Record | update_a_record | 20
db60ac52-39e9-4b46-accb-28a34b10579c | 2015-04-28 19:18:37-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:37.650135+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277517, 'owner_id': 20L} | Record | update_a_record | 20
336acc47-6a93-4ff9-a6c5-d29d3b2c4e35 | 2015-04-28 19:23:24-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:23:24.146505+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277804, 'owner_id': 20L} | Record | update_a_record | 20
4ca66f70-36cb-47cc-9324-6a5747d6a592 | 2015-04-28 19:18:48-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:48.242689+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277528, 'owner_id': 20L} | Record | update_a_record | 20
dbfda8bc-f6f2-4b97-b3c1-ccaff21338bb | 2015-04-28 19:18:32-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:32.857508+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277512, 'owner_id': 20L} | Record | update_a_record | 20
6c05779a-d3b8-40ac-84ee-af91a3bf6b15 | 2015-04-28 19:18:47-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:47.181657+00:00', 'id': 143L, 'content': u'192.213.216.16', 'change_date': 1430277527, 'owner_id': 20L} | Record | update_a_record | 20
a037fb9d-cb58-4994-baad-88c441429199 | 2015-04-28 19:18:31-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:18:31.680786+00:00', 'id': 143L, 'content': u'192.213.216.16', 'change_date': 1430277511, 'owner_id': 20L} | Record | update_a_record | 20
66ee42af-6770-4ef8-a300-764246ccc8ff | 2015-04-28 19:20:33-0400 | False | net | updated | {'prio': None, 'type_id': u'A', 'auth': None, 'is_free': False, 'ttl': 300L, 'active': True, 'domain_id': 32L, 'ordername': None, 'name': u'myrecord.mytable.net', 'created': '2015-04-14 17:44:23+00:00', 'modified': '2015-04-29 03:20:33.336544+00:00', 'id': 143L, 'content': u'192.213.15.16', 'change_date': 1430277633, 'owner_id': 20L} | Record | update_a_record | 20
我不明白的是,create_at列没有按降序排序。 我的最终目标是将我的应用程序的日志存储在此表中,然后只能在仪表板中显示其中一些,这就是我限制为10的原因。
我在这里做错了什么? 此致
答案 0 :(得分:3)
我不明白的是,create_at列没有按降序排序。
因为Cassandra只会在分区键中强制执行群集顺序。您的分区键为id
。但看起来它具有几乎独特的基数水平。如此独特,如果您在其上进行分区,您将无法获得任何数据以使排序变得有价值。
SELECT * FROM mytable.log
WHERE "created_at" <= '2015-04-29 00:00:00' AND "user" = 20 LIMIT 10;
要满足此查询,您应创建一个由user
分区的单独查询表,例如logByUser
。您希望该表具有相同的列,但具有如下的PRIMARY KEY定义:
PRIMARY KEY (user, created_at, id)
此PRIMARY KEY定义将允许以下查询按预期运行:
SELECT * FROM mytable.logByUser
WHERE "created_at" <= '2015-04-29 00:00:00' AND "user" = 20 LIMIT 10;
另外,我想指出两件事:
当您设计数据模型以适合您的查询模式时,Cassandra的功能最佳。这可能意味着为每个查询创建一个表。尽管听起来很疯狂,但创建五到六个表来满足每个潜在查询的效果要比在一个表中添加5个二级索引更好多。
辅助索引是为了方便而非性能。它们的使用是一种已知的Cassandra反模式。在低基数列(尤其是布尔值)上使用它们会遇到麻烦。它们并非旨在弥合数据模型缺点的“神奇子弹”。