1

As far as I understand there are no secondary indices with multiple columns, only single columns.

So how do I solve the index problem I'm having? This is my table:

CREATE TABLE IF NOT EXISTS logstv.messages (
    timestamp timestamp,
    channelid bigint,
    userid bigint,
    message text,
    PRIMARY KEY ((channelid, timestamp), userid)
) WITH CLUSTERING ORDER BY (timestamp DESC);

But this is the error currently: Missing CLUSTERING ORDER for column userid

I want these things:

  • SELECT WHERE with channelid, userid, timestamp with an order by timestamp and limit

  • SELECT WHERE with channelid, timestamp with an order by timestamp and limit

Is this possible?

The only option I currently see is setting timestamp as the primary key and then sorting through the rest in my software, which seems very expensive. Or having 2 tables for my data which i want to very much avoid because the data will grow very fast and get big.

Example queries:

SELECT message, timestamp
        FROM logstv.messages 
        WHERE userid = ? 
        AND channelid = ? 
        AND timestamp >= ? 
        AND timestamp <= ?
        ORDER BY timestamp DESC
        LIMIT 100;

SELECT message, timestamp
        FROM logstv.messages 
        WHERE channelid = ? 
        AND timestamp >= ? 
        AND timestamp <= ?
        ORDER BY timestamp DESC
        LIMIT 100;
0

Yeah, It is possible with 'ALLOW FILTERING' option. BUT NOT ADVISABLE (well explained in this link). Schema design in Cassandra is majorly Query Specific (i.e) design your schema so that it is able to answer a query.

If you want to query on different attributes, go for individual schemas for each one.

  • Is it maybe possible without ALLOW FILTERING; but only supporting WHERE timestamp + channelid and timestamp + userid + channelid ? Leaving out some queries? – gempir Sep 24 '18 at 14:19
  • @gempir as per your model in the question, you can query only based on channel.id + user.id /// or /// channel.id+user.id+timestamp and nothing more than that . – Dinesh Kumar Sep 24 '18 at 14:45
  • The schema is changeable. I just want to avoid storing messages twice in 2 tables – gempir Sep 24 '18 at 14:51
  • What kind of queries do you want to apply on logstv.messages? – Dinesh Kumar Sep 24 '18 at 14:58
  • 2 kind something with channelid, timestamp and userid AND a query with just channelid and timestamp – gempir Sep 24 '18 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.