Siddhant Bagga
Siddhant Bagga

Reputation: 33

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

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?

Upvotes: 0

Views: 1661

Answers (2)

fedonev
fedonev

Reputation: 25669

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.

Upvotes: 1

Maurice
Maurice

Reputation: 13127

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

See Key Condition Expressions for Query

Upvotes: 2

Related Questions