Go DynamoDB查询使用过滤器和Limit=1时未返回任何项。

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

Go DynamoDB Query returns no item with Filter and Limit=1

问题

我有一个 DynamoDB 表格,如下所示:

user_id date game
user1 2021-12-06 14:36:46 game1
user1 2021-12-06 15:36:46 game1
user1 2021-12-07 11:36:46 game2
user1 2021-12-07 12:36:46 game2

分区键:user_id

排序键:date

我想查询用户最新的一条关于游戏 game1 的记录(即表格中日期为 2021-12-06 15:36:46 的第二条记录)。我可以通过以下代码实现:

expr, _ := expression.NewBuilder().
	WithKeyCondition(expression.Key("user_id").Equal(expression.Value("user1"))).
	WithFilter(expression.Name("game").Equal(expression.Value("game1"))).
	Build()
var queryInput = &dynamodb.QueryInput{
	KeyConditionExpression:    expr.KeyCondition(),
	ExpressionAttributeNames:  expr.Names(),
	ExpressionAttributeValues: expr.Values(),
	FilterExpression:          expr.Filter(),
	ScanIndexForward:          aws.Bool(false),
	TableName:                 aws.String(table),
}

这将返回用户 user1 在游戏 game1 中的所有记录。问题出现在当我在 QueryInput 中应用 Limit: aws.Int64(1) 时,它返回空结果。有人能解释一下为什么会这样吗?

当我将 Limit: aws.Int64(4)(表格中的总记录数)时,查询才返回了预期的单条记录。这个限制是如何工作的?

我需要将 game 作为 GSI(全局二级索引)使用吗?

英文:

I've following dynamoDB table

user_id date game
user1 2021-12-06 14:36:46 game1
user1 2021-12-06 15:36:46 game1
user1 2021-12-07 11:36:46 game2
user1 2021-12-07 12:36:46 game2

partition key: user_id

sort key: date

I want to Query the latest entry of user for game game1
(Which is the second item from table with date 2021-12-06 15:36:46). I can achieve this from code as follows;

expr, _ := expression.NewBuilder().
	WithKeyCondition(expression.Key("user_id").Equal(expression.Value("user1"))).
	WithFilter(expression.Name("game").Equal(expression.Value("game1"))).
	Build()
var queryInput = &dynamodb.QueryInput{
	KeyConditionExpression:    expr.KeyCondition(),
	ExpressionAttributeNames:  expr.Names(),
	ExpressionAttributeValues: expr.Values(),
	FilterExpression: 		   expr.Filter(),
	ScanIndexForward: 	       aws.Bool(false),
	TableName: 				   aws.String(table),
}

This returns me all items of user user1 for game game1. Problem occurs when I apply limit=1 Limit: aws.Int64(1) in QueryInput, it returns nothing. Could someone explain why is that so ?

When I change Limit: aws.Int64(4) (total number of items in table), only then the query returns single expected item. How is this limit working ?

Do I need to use game as GSI ?

答案1

得分: 2

DDB参数的限制是在应用筛选表达式之前生效的。

基本上,如果限制为1,则会检索1条记录,然后应用筛选条件,并返回与之匹配的项目(0条)。

有关更多详细信息,请参阅https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.Limit,以防链接失效。

限制结果集中的项目数

Query操作允许您限制它读取的项目数。为此,请将Limit参数设置为您想要的最大项目数。

例如,假设您查询一个表,Limit值为6,没有筛选表达式。查询结果包含与请求中的键条件表达式匹配的表中的前六个项目。

现在假设您向查询添加了一个筛选表达式。在这种情况下,DynamoDB会读取最多六个项目,然后仅返回与筛选表达式匹配的项目。最终的查询结果包含六个或更少的项目,即使如果DynamoDB继续读取更多项目,可能会有更多项目与筛选表达式匹配。

英文:

The limit on a DDB parameter is applied before your filter expressions.

Essentially with a limit of 1, it retrieves 1 record, then applies the filters and returns you the items that match (0).

See https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Query.html#Query.Limit for more details, copied in case link breaks

> Limiting the Number of Items in the Result Set

> The Query operation allows you to limit the number of items that it reads. To do this, set the Limit parameter to the maximum number of items that you want.

> For example, suppose that you Query a table, with a Limit value of 6, and without a filter expression. The Query result contains the first six items from the table that match the key condition expression from the request.

> Now suppose that you add a filter expression to the Query. In this case, DynamoDB reads up to six items, and then returns only those that match the filter expression. The final Query result contains six items or fewer, even if more items would have matched the filter expression if DynamoDB had kept reading more items.

huangapple
  • 本文由 发表于 2021年12月8日 10:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/70269230.html
匿名

发表评论

匿名网友

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

确定