Index query and sorting in cassandra

Index query and sorting in cassandra

Although cassandra's query is very weak, it also supports indexing and sorting, of course, it is a simple query, all of which are at the cost of performance, so to use cassandra, you can't hope that it can fully apply your logic, but put Your logic design is more suitable for cassandra.

First: Index query
Cassandra supports the creation of secondary indexes, indexes can be created on all columns except the first primary key, of course, except for some types, such as collection types.
E.g

CREATE TABLE test(
	a INT,
	b INT,
	c INT,
	d INT,
	and INT,
	m INT,
	PRIMARY KEY(a,b,c));
 
CREATE INDEX ON test(c);
CREATE INDEX ON test(e);

 It is not possible to create an index on the first primary key a:

CREATE INDEX ON test(a) X

 Index columns can only be queried with the = sign, so

SELECT * FROM test WHERE e=1; //Yes
SELECT * FROM test WHERE e>1; //It won't work.

 If one of your query conditions is based on an index query, then other non-indexed non-primary key fields can be filtered by adding an ALLOW FILTERING,

E.g:

SELECT * FROM test WHERE e=1 AND m>2 ALLOW FILTERING;

 Although the m field is a non-index non-primary key field, as long as the ALLOW FILTERING condition is added, it will first be found according to e=1, and then the result will be filtered by m>2

Second: sorting
cassandra also supports sorting, order by. Of course, its sorting is also conditional,
first: there must be an = query with the first primary key. The first primary key of cassandra is to determine which machine the records are distributed on, which means that cassandra only supports the sorting of records on a single machine.
Second: That is, it can only be ordered according to the second, third, fourth ... primary key, the same sorting.
Third: no index query

SELECT * FROM test WHERE a=1 ORDER BY b DESC;
SELECT * FROM test WHERE a=1 ORDER BY b DESC, c DESC;
SELECT * FROM test WHERE a=1 ORDER BY b ASC;
SELECT * FROM test WHERE a=1 ORDER BY b ASC, c ASC;

 All of the above are possible.

SELECT * FROM test ORDER BY b DESC; //No first primary key
SELECT * FROM test WHERE a=1 ORDER BY c DESC; //Must start sorting with the second primary key
SELECT * FROM test WHERE a=1 ORDER BY b DESC, c ASC; //Not the same ordering.
SELECT * FROM test WHERE e=1 ORDER BY b DESC; //Cannot have indexes.

 In fact, any query of Cassandra, the final result is ordered, the default is b asc, c asc, because it is stored in this way.

This I mentioned in the article "How to implement paging query in cassandra2.0" . So you use b desc, c asc or b asc, c desc to sort, cassandra is more difficult.
Of course, this default storage sorting method can be specified when creating the table.

CREATE TABLE test(
	a INT,
	b INT,
	c INT,
	d INT,
	e INT,
	m INT,
	PRIMARY KEY(a,b,c))
WITH CLUSTERING ORDER BY (b DESC, c ASC);

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326847290&siteId=291194637