首页 > 解决方案 > 扫描 10GB 的大型 Amazon DynamoDB 数据

问题描述

以下代码适用于我,但 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))

标签: amazon-web-servicesamazon-dynamodbboto3large-data

解决方案


因为它是在所有分区中搜索,所以scan操作可能非常慢。如果您正在使用关系数据库,您将无法像使用关系数据库那样“调整”此查询。

为了最好地帮助您,我需要更多地了解您的访问模式(按年获取电影?)以及您的表当前的外观(您的分区键/排序键,其他属性等)。


推荐阅读