简体   繁体   中英

DynamoDB - query based on multiple columns

I have a requirement to find all users in a table that have same Id, Email or Phone.

Right now the data looks like this:

Id //hash

Market //sort

Email //gsi

Phone //gsi

I want to be able to do a query and say:

Get all items that have matching Id, email or phone.

From the docs it seems that you can only do a single query based on keys or one index. And it seems that even if I was to combine phone and email into one column and GSI that column I would still be limited to a begin with filter expression, is this correct? Are there any alternatives?

it seems that you can only do a single query based on keys or one index

Yes.

if I was to combine phone and email into one [GSI] I would still be limited to a begin with filter expression, is this correct?

Essentially, yes. Query constraints apply equally to indexes and the table keys. You must specify one-and-only-one Partition Key value, and optionally a range of Sort Key values.

Are there any alternatives?

Overload the Partition Key and denormalise the data. Redefine the Partition Key column (renamed PK ) to hold Id , Email and Phone values. Each record is (fully or partially) repeated 3 times, each time with a different PK type.

PK              Market          Id       More fields        
Id-1            A               Id-1     foo
zaphod@42.com   A               Id-1     # foo or blank
13015552572     A               Id-1     # foo or blank

Querying PK = <something> AND Market > "" will return any matching id, email or phone number value.

If justified by your query patterns, repeat all fields 3x. Alternatively, use a hit on a truncated email/phone record to identify the Id , then query other fields using the Id .

There are different flavours of this pattern. For instance, you could also overload the Sort Key column (renamed to SK ) with the Id value for Email and Phone records, which would permit multiple Id s per email/phone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM