I am a beginner in NoSQL DB and Serverless. My app has a table called Trips
. The parameters of the tables are {id, route, cost, selling, type, date, LR, asset }
and a bunch of other irrelevant document numbers, where id is generated by uuid.
Now I want to query the database for giving me
- Return all trips between a date range using the date parameter.
- Return all the trips for an asset in given period of time using date and asset parameter.
- Return all the trips for a route in a given period of time using date route parameter.
2 and 3 work fine using keyConditionExpression but for 1 I need to use a filterExpression on a scan instead of a query which could make it relatively slower since it is executed once the query is complete. Is there a better way to form the schema?
In Trips
table the schema is as such
tripTable:
Type: "AWS::DynamoDB::Table"
Properties:
AttributeDefinitions:
[
{ "AttributeName": "id", "AttributeType": "S" },
{ "AttributeName": "date", "AttributeType": "S" },
{ "AttributeName": "Asset", "AttributeType": "S" },
{ "AttributeName": "Route", "AttributeType": "S" },
]
KeySchema:
[
{ "AttributeName": "date", "KeyType": "HASH" },
{ "AttributeName": "id", "KeyType": "RANGE" },
]
ProvisionedThroughput:
ReadCapacityUnits: 5
WriteCapacityUnits: 5
StreamSpecification:
StreamViewType: "NEW_AND_OLD_IMAGES"
TableName: ${self:provider.environment.TRIPS}
GlobalSecondaryIndexes:
- IndexName: TripsVSAssets
KeySchema:
- AttributeName: asset
KeyType: HASH
- AttributeName: date
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"
GlobalSecondaryIndexes:
- IndexName: RoutesVSAssets
KeySchema:
- AttributeName: route
KeyType: HASH
- AttributeName: date
KeyType: RANGE
Projection:
ProjectionType: ALL
ProvisionedThroughput:
ReadCapacityUnits: "5"
WriteCapacityUnits: "5"