Cassandra sorting issue on updated_at column
Author: Kunal
Originally Sourced from: https://stackoverflow.com/questions/79685747/cassandra-sorting-issue-on-updated-at-column
I have the following Cassandra table:
K = Partition Key
C = Clustering Key
user_id (text) -- K
folder_id (text) -- C
name (text)
owner (text)
size (text)
updated_at (timestamp)
My use case is to query all folders for a specific user, sorted by the most recent updated_at
timestamp. So I'm running:
SELECT *
FROM user
WHERE user_id = 'user 1'
ORDER BY updated_at DESC;
But the results are not ordered by updated_at
in descending order.
Here's my raw data:
user_id |
folder_id |
name |
owner |
size |
updated_at |
---|---|---|---|---|---|
user 1 | fadsx | root | user 1 | 20 | 10:10 |
user 1 | fadf | home | user 1 | 40 | 11:40 |
user1 | tgqer | john | user 1 | 90 | 8:00 |
I want the output to be ordered by updated_at DESC
, like this:
updated_at |
folder_id |
name |
owner |
user_id |
size |
---|---|---|---|---|---|
11:40 | fadf | home | user 1 | user 1 | 40 |
10:10 | fadsx | root | user 1 | user 1 | 20 |
8:00 | tgqer | john | user 1 | user1 | 90 |
ORDER BY updated_at DESC
doesn't seem to work since updated_at
is not a clustering key.
My question: how can I design the table so that I can fetch all folders for a user, sorted by updated_at DESC
?
What is the right data model to support this kind of query?