This is more of a design question.
Let's imagine I have this table (most columns omitted for readability):
CREATE TABLE IF NOT EXISTS users (
userid uuid,
emailaddress text,
passwordhash text,
passwordsalt text,
datecreated timestamp,
PRIMARY KEY (userid, emailaddress)
);
On this table, I need to sometimes get userid by emailaddress and other times, simply use the userid to get the rest of the values, so the queries are:
- Search by
emailaddress
, getuserid
and use in other places - Search by
userid
, get all the values for specific user and use that elsewhere.
Querying by only userid
works since it's the first key in the composite key table but for querying by emailaddress
(not knowing the userid in advance), I will have to turn on the ALLOW FILTERING
which is strongly discouraged.
Question is: knowing this design is not good in terms of how Cassandra operates, how else would I achieve my goal (to get user details by EITHER userid
OR emailaddress
)? Should I create 2 tables such as these?
CREATE TABLE IF NOT EXISTS users (
userid uuid,
passwordhash text,
passwordsalt text,
datecreated timestamp,
PRIMARY KEY (userid)
);
CREATE TABLE IF NOT EXISTS useremails (
emailaddress text,
userid uuid,
PRIMARY KEY (emailaddress)
);
Or is there a cleaner way to achieve what I want to do? I apologize in advance for my lack of understanding the ins and outs of Cassandra. I'm still learning it and trying to approach the problems from the correct angle instead of hacking (which ALLOW FILTERING
would allow me to do).
Many thanks,