可以在 Appsync schema 中定义查询参数以及使用 DynamoDB 的索引来优化查询。例如,在 Appsync schema 中定义参数:
type Query {
listItems(
filter: ModelItemFilterInput
limit: Int
nextToken: String
): ModelItemListConnection
}
并在 DynamoDB 中创建索引来支持查询:
{
"TableName": "MyTable",
"AttributeDefinitions": [
{
"AttributeName": "itemId",
"AttributeType": "S"
},
{
"AttributeName": "category",
"AttributeType": "S"
}
],
"KeySchema": [
{
"AttributeName": "itemId",
"KeyType": "HASH"
},
{
"AttributeName": "category",
"KeyType": "RANGE"
}
],
"GlobalSecondaryIndexes": [
{
"IndexName": "MyIndex",
"KeySchema": [
{
"AttributeName": "category",
"KeyType": "HASH"
},
{
"AttributeName": "itemId",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 5,
"WriteCapacityUnits": 5
}
}
这将在 DynamoDB 中创建一个名为“MyIndex”的全局二级索引,该索引按照“category”和“itemId”排序,以此支持按“category”和“itemId”字段查询并执行更高效的操作。