Database Reference
In-Depth Information
Reversing clustering order in the schema
While it's useful to be able to show the newest status updates first using ORDER BY , it
would be even better if the rows were naturally ordered newest first. Although we can't
change the clustering order of an existing table, we can make a new table that has the same
structure as user_status_updates , but with id ordered newest first:
CREATE TABLE "reversed_user_status_updates" (
"username" text,
"id" timeuuid,
"body" text,
PRIMARY KEY ("username", "id")
) WITH CLUSTERING ORDER BY ("id" DESC);
The CLUSTERING ORDER BY property tells Cassandra that we want to store columns in
descending order by id , rather than the default ascending order. Adding a few quick rows
to our table, we can see the reversed clustering order in action:
INSERT INTO "reversed_user_status_updates"
("username", "id", "body")
VALUES ('alice', NOW(), 'Reversed status 1');
INSERT INTO "reversed_user_status_updates"
("username", "id", "body")
VALUES ('alice', NOW(), 'Reversed status 2');
INSERT INTO "reversed_user_status_updates"
("username", "id", "body")
VALUES ('alice', NOW(), 'Reversed status 3');
SELECT * FROM "reversed_user_status_updates"
WHERE "username" = 'alice';
Even though we did not specify a descending order, the rows are naturally returned with
the newest records first:
Search WWH ::




Custom Search