如果您不想扫描(也许您不应该),您需要为此创建一个GSI (Global Secondary Index),并将event_status 设置为 GSIPK。
所以你的表配置将是:
table = dynamodb.create_table(
TableName="your_table",
KeySchema=[
{"AttributeName": "event_id", "KeyType": "HASH"}, # Partition key
{"AttributeName": "event_status", "KeyType": "RANGE"}, # Sort key
],
AttributeDefinitions=[
{"AttributeName": "event_id, "AttributeType": "S"},
{"AttributeName": "event_status", "AttributeType": "S"},
{"AttributeName": "event_status", "AttributeType": "S"},
{"AttributeName": "event_id", "AttributeType": "S"},
],
GlobalSecondaryIndexes=[
{
"IndexName": "gsiIndex",
"KeySchema": [
{"AttributeName": "event_status", "KeyType": "HASH"},
{"AttributeName": "event_id", "KeyType": "RANGE"},
],
"Projection": {"ProjectionType": "ALL"},
},
],
BillingMode="PAY_PER_REQUEST",
)
请注意,GSI 可能很昂贵,如果您不需要所有属性,可能需要更改 ProjectionType。
现在可以通过pk查询了:
table.query(KeyConditionExpression=Key('event_id').eq(event_id))
或通过设置为您的 sk 的 GSI PK:
lookup.query(
IndexName="gsiIndex",
KeyConditionExpression=Key("event_status").eq(event_status),
)