我有一个看起来像这样的 dynamodb 表
resource = boto3.resource("dynamodb")
resource.create_table(TableName="mytable", KeySchema=[{"AttributeName": "pk", "KeyType": "HASH"},
{"AttributeName": "sk", "KeyType": "RANGE"}],
AttributeDefinitions=[
{"AttributeName": "pk", "AttributeType": "S"},
{"AttributeName": "sk", "AttributeType": "S"},
{"AttributeName": "type", "AttributeType": "S"},
{"AttributeName": "brand", "AttributeType": "S"},
{"AttributeName": "creationDate", "AttributeType": "S"},
],
ProvisionedThroughput={
"ReadCapacityUnits": 10,
"WriteCapacityUnits": 10,
},
GlobalSecondaryIndexes=[
{
"IndexName": "GSI1",
"KeySchema": [
{"AttributeName": "type", "KeyType": "HASH"},
{"AttributeName": "brand", "KeyType": "RANGE"},
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
},
{
"IndexName": "GSI2",
"KeySchema": [
{"AttributeName": "type", "KeyType": "HASH"},
{"AttributeName": "creationDate", "KeyType": "RANGE"},
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5,
},
}
]
)
我想创建一个查询来获取
type
的所有条目 - 假设输入“ABC”。然而,该查询也应该只允许某些品牌。假设有三个
并且该用户只能访问其中两个
["Addidas", "Nike"]
。查询应使用恒定的页面大小(假设 10 个条目)进行分页并返回 LastEvaluatedKey
。返回的数据需要通过一个名为 creationDate
的字段进行排序。
为了解决这个问题,我创建了以下查询:
response = resource.query(IndexName="GSI2", KeyConditions={
"type": {
"AttributeValueList": ["ABC"],
"ComparisonOperator": "EQ"
}
},
FilterExpression=Attr("brand").is_in(brands),
Limit=no_of_items,
ScanIndexForward=False
)
只要所有品牌都在数组中,它就会按预期工作。如果不是,则几乎总是会产生少于十个条目,因为限制是在 FilterExpression 之前应用的。 有没有什么方法可以对此进行建模,以便在不需要所有品牌的情况下使分页正常工作?
不幸的是,没有办法根据过滤后的结果数量进行限制。您需要估计过滤器产量(可能匹配的百分比)以计算要使用的预过滤器限制。可能估计有点高了。如果还不够,请进行第二次获取。