Search⌘ K
AI Features

Table option: CLUSTERING ORDER BY

Learn how to use the CLUSTERING ORDER BY table option in Apache Cassandra to control the sorting order of clustering columns within partitions. Understand the impact on data retrieval, limitations on SELECT queries, and best practices for defining primary keys to optimize query performance and data organization.

As mentioned earlier, a table’s primary key in Cassandra consists of one or more partition keys followed by zero or more clustering columns, with partition keys listed first.

Clustering columns ensures record uniqueness and determines the sorting order of rows within a partition. A Cassandra table can have zero or more clustering columns after the partition key in the PRIMARY KEY clause. Together, partition keys and clustering columns form the table’s primary key. Without clustering columns, each partition contains only a single row.

The CLUSTERING ORDER BY table option

By default, rows in a partition are sorted in ascending order by clustering column values. If queries require data in descending order, for instance, time series data retrieval from newest to oldest, the desired sorting order may be specified in the table schema.

Apache Cassandra accomplishes really fast reads by storing rows within a partition in sorted order on disk, providing pre-optimization to the fetch query. The CLUSTERING ORDER BY table option uses a comma-separated list of clustering columns, to specifically set the order ASC or DESC for the clustering column(s). 

Let’s recreate the courses table, specifying that the data should be partitioned ...