Lambda DynamoDB “x and (y or z)” 查询

huangapple go评论56阅读模式
英文:

Lambda DynamoDB "x and (y or z)" Query

问题

KeyConditionExpression: '要查询的条件:#list = :listID and (#owner = :userID or #collab = :userID)';

list 已创建索引。

如果我只有一个额外的参数可用,我可以将其添加为索引的排序键,但是如果我需要两个呢?

我需要为 ownercollab 都添加索引吗?那么在查询时我应该将什么作为 IndexName

英文:

I need to create a dynamoDB query that fetches results based on an index, but only if 1 of 2 conditions are met.

Something like this:

KeyConditionExpression: '#list = :listID and (#owner = :userID or #collab = :userID)'

list is indexed.

I understand if I had only 1 extra param to use, I could add that as the sort key on the index, but what when I need 2?

Do I need to add indexes for both owner and collab, but then what do I put as the IndexName for the query?

答案1

得分: 1

你不能高效地在键上执行 OR 表达式。你应该以这样一种方式建模你的数据,使分区键和排序键模型化大多数被评估的数据,然后你可以使用 FilterExpression 来细化剩余的数据。

或者简单地这样应用它:

KeyConditionExpression: '#list = :listID'
FilterExpression: '#owner = :userID or #collab = :userID'
英文:

You cannot do an OR expression on keys efficiently. You should model your data in such a way that the partition key and sort key model the majority of the evaluated data, then you can use a FilterExpression to refine the remainder of the data.

Or simply apply it like this:

KeyConditionExpression: '#list = :listID'
FilterExpression: '#owner = :userID or #collab = :userID)'

huangapple
  • 本文由 发表于 2023年7月17日 17:37:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703172.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定