问题
I have a data table in Cassandra and one of the columns is:
customer_favourites
, with each value being of type set and it has the details of each customer's favourite foods. For example one customer could have {'Mexican', 'Italian', 'Indian'}
and another customer could have {'Mexican', 'French'}
and another could have {'Mexican'}
.
I have the following code:
SELECT customer_id, customer_fname, customer_lname FROM customers WHERE customer_favourites CONTAINS ‘Mexican’ ALLOW FILTERING;
I want it to filter on those customers whose favourite food is ONLY Mexican, but right now it's returning the details of every customer who has Mexican as one of their favourite foods. How do I filter my query to return customer who like ONLY Mexican food?
回答1:
Naive approach: You need to use customer_favourites = {'Mexican'}
...
Better approach - create secondary index on the corresponding field, using the FULL keyword, and then use customer_favourites = {'Mexican'}
.
Best approach - create a separate table with customer_favourites
as partition key, and search users in it (column should be frozen
). One of the problems with this approach would be the data skew, as number of favorite foods is relatively small, and quite imbalanced.
Alternative approach - reconsider the use of the Cassandra, if you need to search by non-partition key very often.
来源:https://stackoverflow.com/questions/64220223/cassandra-filtering-based-one-specific-value-in-a-set