We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

lamtran • 6 years ago

Hi Alex D, do you have an example (or a guide online somewhere) that shows how to create a GSI with Inverted Index as seen in your AWS re:Invent 2019: Data modeling with Amazon DynamoDB video where you switch the PK and SK so the partition key is SK and the sort key is PK?

I tried doing that (I also have a PK, and SK, and I tried creating a GSI that switches the PK and SK), but it doesn't seem to work as seen in the video.

I couldn't find any thing online specifically about inverting the indexes... All the documentation I saw so far is just about creating the index to speed up retrieval of certain targeted attributes.

The Inverted Index, if worked as I think it would, would enable data access patterns that would otherwise require several queries to achieve. That's what I'm after, but I can't seem to be able to get it to work.

Alex D • 6 years ago

Hi lamtran , can you tell me more about why it's not working?

The CLI command to create an inverted index should be as follows (assuming your primary key schema is named "PK" and "SK":


aws dynamodb update-table \
--table-name MyTable \
--global-secondary-index-updates '[
{
"Create": {
"IndexName": "InvertedIndex",
"KeySchema": [
{
"AttributeName": "SK",
"KeyType": "HASH"
},
{
"AttributeName": "PK",
"KeyType": "RANGE"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
}
]'

Let me know if that helps! Feel free to email too if you have further questions -- email is on the About page.

Christian Trahan • 3 years ago

Thank you Alex for writing this up. I found this website through your AWS dynDB video. Working on a serverless app with Dynamo and this helps me better understand indexes and query patterns.

Alex D • 3 years ago

Great to hear, Christian! Glad you liked it :)

Chad McElligott • 3 years ago

While I was working through this I hit an error:


❯ aws dynamodb scan --table-name UserOrdersTable --index-name ReturnDateOrderIdIndex

An error occurred (ValidationException) when calling the Scan operation: Cannot read from backfilling global secondary index: ReturnDateOrderIdIndex

Some searching pointed to the fact that the Index wasn't yet finished creating:


❯ aws dynamodb describe-table --table-name UserOrdersTable | \
jq ".Table.GlobalSecondaryIndexes[] | { IndexStatus, Backfilling }"

{
"IndexStatus": "CREATING",
"Backfilling": true
}

Waiting a little while let the scan command work.

Alex D • 3 years ago

Ahh yep, this can happen. DynamoDB will create that GSI in the background, and you need to wait until it's complete before you can use it. :)

halfpipe07 • 7 years ago

I was wondering about the consumption of read and write units. If I write on the base table and the contents project to 7 GSI tables, will I be charged with 8 write units immediately?

Alex D • 7 years ago

Hey halfpipe07 , good question. There are two things to note:

1. Replication to global secondary indexes are done asynchronously, and

2. Each GSI has its own provisioned throughput.

The replication to each GSI will happen as quickly as possible, subject to throttling on the index due to load. However, note that it will consume a write unit on each of seven separate indexes rather than 7 write units on a single table.

Does that answer your question?

halfpipe07 • 6 years ago

Ah makes total sense, thanks for answering!

halfpipe07 • 7 years ago

Yes it does! So in short, I will pay for the additional 7 write units. Thanks so much! It's something I should also consider when designing the tables.

ElKristoball • 7 years ago

Yeah - thanks a lot man :)

Lam Kwok Keung • 7 years ago

Big thanks for writing this up. It explained well on LSI and GSI. Thumbs up. : )

Alex D • 7 years ago

Thanks, Lam Kwok Keung ! Glad you liked it.