Question

According to this blog:

ORDER BY clauses can only select a single column, and that column has to be the second column in a composite PRIMARY KEY. This holds even for tables with more than 2 column components in the primary key. Ordering can be done in ascending or descending order, default ascending, and specified with the ASC or DESC keywords.

Second column in composite PRIMARY KEY means first cluster key. If ORDER BY column has to be the second column in a composite PRIMARY KEY, Then what is the benefit of having more than one cluster key?

Was it helpful?

Solution

One thing to remember is that a cluster key column is also a primary key column. Most of the time, you'll want to introduce additional key columns (that also happen to be clustering columns) not because they give you more ordering flexibility, but simply to be able to store more unique rows.

With that in mind, if you do introduce extra cluster key columns, you will be able to leverage them for result ordering and range queries.

Consider this table:

CREATE TABLE table4 (col1 int, col2 int, col3 int, col4 text, PRIMARY KEY (col1, col2, col3));

1) Clustering across multiple columns means you are able to issue compound range queries such as:

 cqlsh:ks> SELECT * FROM table4 WHERE col1=1 AND col2=2 AND col3 > 1;

 col1 | col2 | col3 | col4
------+------+------+------
    1 |    2 |    2 |  122
    1 |    2 |    3 |  123

 (2 rows)

2) As far as ordering by multiple columns (what the blog post claimed you cannot do): you can in fact do that:

cqlsh:ks> SELECT * FROM table4 WHERE col1=3 ORDER BY col2 DESC, col3 DESC;

 col1 | col2 | col3 | col4
------+------+------+------
    3 |    3 |    3 |  333
    3 |    3 |    2 |  332
    3 |    3 |    1 |  331
    3 |    2 |    3 |  323
    3 |    2 |    2 |  322
    3 |    2 |    1 |  321
    3 |    1 |    3 |  313
    3 |    1 |    2 |  312
    3 |    1 |    1 |  311

(9 rows)

But there is a gotcha. Anything beyond a simple reverse that would require the query engine to reorder rows is disallowed. So the following doesn't work, because it would require non-trivial sorting:

cqlsh:ks> SELECT * FROM table4 WHERE col1=3 ORDER BY col2 DESC, col3 ASC;
Bad Request: Unsupported order by relation

To put it another way, there are only two possible orders that the query engine can handle: natural or reversed. To specify which of the two you want, you never need to specify more than one column for ORDER BY. Therefore a query like this will return the exact same result set as the query from above (#2):

cqlsh:ks> SELECT * FROM table4 WHERE col1=3 ORDER BY col2 DESC;

 col1 | col2 | col3 | col4
------+------+------+------
    3 |    3 |    3 |  333
    3 |    3 |    2 |  332
    3 |    3 |    1 |  331
    3 |    2 |    3 |  323
    3 |    2 |    2 |  322
    3 |    2 |    1 |  321
    3 |    1 |    3 |  313
    3 |    1 |    2 |  312
    3 |    1 |    1 |  311

(9 rows)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top