以下代码适用于我,但1个API请求返回结果需要19分钟。优化后的结果将不胜感激。我不喜欢使用段,因为这样我就必须进行线程管理。
dynamodb = boto3.resource('dynamodb', region_name='us-west-2', endpoint_url="http://localhost:8000")
table = dynamodb.Table('Movies')
fe = Key('year').between(1950, 1959)
pe = "#yr, title, info.rating"
# Expression Attribute Names for Projection Expression only.
ean = { "#yr": "year", }
esk = None
response = table.scan(
FilterExpression=fe,
ProjectionExpression=pe,
ExpressionAttributeNames=ean
)
for i in response['Items']:
print(json.dumps(i, cls=DecimalEncoder))
// As long as LastEvaluatedKey is in response it means there are still items from the query related to the data
while 'LastEvaluatedKey' in response:
response = table.scan(
ProjectionExpression=pe,
FilterExpression=fe,
ExpressionAttributeNames= ean,
ExclusiveStartKey=response['LastEvaluatedKey']
)
for i in response['Items']:
print(json.dumps(i, cls=DecimalEncoder))
发布于 2020-08-08 06:44:58
因为它是在所有分区中进行搜索,所以scan操作可能非常慢。您将无法像使用关系数据库时那样“调优”此查询。
为了更好地帮助您,我需要更多地了解您的访问模式(按年获取电影?)表的当前外观(您的分区键/排序键、其他属性等是什么)。
发布于 2020-08-07 21:52:56
不幸的是,scan天生就很慢。除了重新设计表以针对此访问模式进行优化之外,没有其他方法可以在代码级进行优化。
https://stackoverflow.com/questions/63302838
复制相似问题