Scylla and Cassandra have two features that fit your needs secondary index and materialized view:
You enable a secondary index on another_id
with the command:
CREATE INDEX ON test (another_id);
And now your query SELECT * FROM test WHERE another_id = 12;
will work normally. You should be aware that if this SELECT
returns a huge number of results (a so-called low-cardinality index) this select is inefficient - it retrieves just the keys from the index, and to show everything (the SELECT *
) Scylla needs to go back to the table and fetch its rows one by one, a fairly inefficient process.
The second alternative is a materialized view with another_id
as a partition key, as in the command:
CREATE MATERIALIZED VIEW test_by_another_id AS
SELECT * FROM test
WHERE another_id IS NOT NULL AND uniuque_string IS NOT NULL and id IS NOT NULL
PRIMARY KEY (another_id, id, uniuque_string);
This materialized view will contain a copy of all the data (not just keys) from test
, and can be searched by another key. For example: SELECT * FROM test_by_another_id WHERE another_id = 12;
(note we had to replace test
by the viewtest_by_another_id
.
The materialized-view solution will make long selects much more efficient than the secondary-index solution, but it requires more storage (all the data is stored twice), so both solutions should be considered depending on your use case.