Home > Enterprise >  Can I use `contains` in a query for DynamoDB GSI?
Can I use `contains` in a query for DynamoDB GSI?

Time:06-16

I am new to DynamoDB. If I make a GSI, can I do a query with KeyConditionExpression: contains (GSI, :val1) and contains(GSI, :val2) Will it be a full scan?

CodePudding user response:

You need to do a Scan. The query only supports things like begins_with, >=, <=, >, <, =, or between.

See Key Condition Expressions for Query

CodePudding user response:

Not with the Query API, as @Maurice says. However, you can achieve the same "query, not scan" end result with ExecuteStatement and a PartiQL statement with an IN operator applied to the index key in question. For example, 2 partition key values:

SELECT * from "my_table"."GSI" WHERE my_gsi_pk_key IN ['val1', 'val2']

It executes as a query operation. This answer has a complete example.

N.B. You have no choice but to use PartiQL here, but in general I would recommend avoiding it whilst learning DynamoDB. The core API better forces you to learn DynamoDB's idioms and unlearn RDBMS/SQL thinking.

  • Related