This article describes all the query expression types that cassandra supports and I know so far. If you need more complex queries, it is difficult to do it with cassandra alone, you need to use other means or tools.
There are currently three expressions supported by cassandra:
let's assume that our table structure is like this:
CREATE TABLE test( a INT, b INT, c INT, d INT, and INT, PRIMARY KEY(a,b,c,d) ); CREATE INDEX ON test(e);
1. Prefix expression
That is, the query condition must be the primary key, and the preceding primary key is the = sign, and only the last primary key is > >= < <=.
Example:
SELECT * FROM test WHERE a=1 AND b>2; SELECT * FROM test WHERE a=1 AND b=1 AND c>2; SELECT * FROM test WHERE a=1 AND b=1 AND c=1 AND d>2; SELECT * FROM test WHERE a=1 AND b=1 AND c>2 AND c<2;
All of the above are possible.
SELECT * FROM test WHERE a>1; //No, the first primary key can only use = sign SELECT * FROM test WHERE a=1 AND c>2; //No, there can be no jumps in the middle SELECT * FROM test WHERE a=1 AND b>2 AND b>=2; //No, the same primary key cannot be used at the same time > >= = or < <= =
2. Prefix expression without the first primary key
This type of expression needs to be queried with ALLOW FILTERING, which is definitely less efficient.
Example:
SELECT * FROM test WHERE b>2 ALLOW FILTERING; SELECT * FROM test WHERE b=1 AND c>2 ALLOW FILTERING; SELECT * FROM test WHERE b=1 AND c=1 AND d>2 ALLOW FILTERING; SELECT * FROM test WHERE b=1 AND c>2 AND c<2 ALLOW FILTERING;
3. Include index column query
Example:
SELECT * FROM test WHERE e=1; // only include index SELECT * FROM test WHERE a=1 AND b=1 AND c>1 AND e=1; // Contains a combination of indexed columns and prefix expressions SELECT * FROM test WHERE b=1 AND c>1 AND e=1; //The combination of the indexed column and the prefix expression without the first primary key does not require ALLOW FILTERING SELECT * FROM test WHERE c>1 AND e=1 ALLOW FILTERING; //In addition to this and any combination of other expressions, you can query, //The premise is to add ALLOW FILTERING;
Finally, let me say that the results of cassandra query, no matter what the query results are, are first sorted by token(a), then sorted by b, then sorted by c, and then sorted by d:
a | b | c | d | e ---+---+---+---+--- 13 | 2 | 3 | 4 | 5 1 | 2 | 1 | 4 | 5 1 | 2 | 3 | 1 | 5 1 | 2 | 3 | 2 | 5 1 | 2 | 3 | 4 | 5 1 | 2 | 3 | 7 | 5 1 | 2 | 6 | 4 | 5 1 | 3 | 2 | 4 | 5 1 | 4 | 3 | 7 | 5 1 | 7 | 3 | 7 | 5 2 | 2 | 3 | 4 | 5 21 | 2 | 3 | 4 | 5
This helps you with paginated queries