Cassandra Clustering Key 无法排序

Cassandra Clustering Key not able to order by

我是 cassandra 的新手,我想弄清楚为什么我不能在 created_at 日期之前订购我的日志。 以下是 table 描述、select 结果和我试图创建的 select 语句。

    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

我不明白的是它没有按 created_at 列降序排列。 我的最终目标是将我的应用程序的日志存储在此 table 中,然后只能在仪表板中显示其中的一些日志,这就是为什么我限制为 10。

我在这里做错了什么? 此致

What I don't understand is that it doesn't order by the created_at column in a descending order.

因为 Cassandra 只会在分区键内强制执行集群顺序。您的分区键是 id。但这看起来具有几乎独特的基数水平。如此独特,以至于如果您对其进行分区,您将没有任何数据来进行排序。

SELECT *  FROM mytable.log  
WHERE "created_at" <= '2015-04-29 00:00:00' AND "user" = 20 LIMIT 10;

要满足此查询,您应该创建一个单独的查询 table,按 user 分区,例如 logByUser。您会希望 table 具有相同的列,但具有如下所示的 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;

另外,我想指出两点:

  1. 当您设计数据模型以适应您的查询模式时,Cassandra 的功能最佳。这可能意味着为每个查询创建一个 table。这听起来可能很疯狂,创建五个或六个 table 以适应您的每个潜在查询比将 5 个二级索引添加到一个 table 执行 .

  2. 二级索引是为了方便,而不是为了性能。它们的使用是一种已知的 Cassandra 反模式。在低基数列(尤其是布尔值)上使用它们是自找麻烦。它们的目的不是 "magic bullet" 来弥合您的数据模型的缺点。