DynamoDB Range-Key bit-wise search

0

I have a problem where I have a Building and the building can be in multiple projects. I need to query all the buildings that belong to a specific project.

I tried to do something like this but contains cannot be used to search for the flags.

PK => company#BUILDINGS SK => buildingId#INFO LSI => flags

I know that:

  • I can do it by duplicating the building record adding the project in the range key.
  • Local Secondary Index per project cannot solve this as the number of projects is grater then 5
  • Global Secondary Index can solve this ( but having many global secondary index ( one per project ) would increase writes cost for each building update ( the cost is not that big of a issue, but still have a limit of 20, more than needed at the moment, but I wouldn't like to have that type of hard limit and would prefer to duplicate the records instead )

This could be easily ( from users query perspective ) be solved using:

  • Numeric range key and bit-wise to search for the project.
  • String range key and contains instead of only begins_with
  • String range key with pattern * something like Range Value = "01100" and search for begins_with "??1"

I'm new to dynamoDB, and I'm migrating a mongoDB project to it. So I may be missing something.

is there a easy way to solve this query problem that I'm unaware of?

thanks in advance

  • Would have project as partition key and buildings as sort keys help?

Fazel
질문됨 일 년 전263회 조회
1개 답변
3
수락된 답변

You need to understand DynamoDB by first understanding its underlying data structure, which can be likened to a B+ Tree. DynamoDB is not like MongoDB in the fact it does not allow you to create access patterns which are in-efficient. All of your requests must locate items which are stored physically together, which is why contains is not a valid function on a sort key. You must use begins_with or between etc.. as you can be guaranteed those items are close together. This ensures DynamoDB holds one of its key tenets, "Predictable performance at any scale".

Querying all buildings that belong to a specific project is as simple as creating a GSI with project as the partition key.

SELECT building FROM mytable.myindex WHERE project = '123'

This would return you a list of buildings for that project.

profile pictureAWS
전문가
답변함 일 년 전
profile picture
전문가
검토됨 2달 전
  • Thanks for the reply,

    knowing it's a B+ Tree helps a lot, thanks. That explains why it lacks query capabilities in exchange for performance. But the query you gave don't help that much, as I need 1 GSI per project as one building record can be in multiple projects.

    Maybe extra records for projects??

    something like PK => companyId#Project SK => buildings field1 => buildingId Extrafields => Other Project specific info

    and querying for the main building record for extra info, duplicating the normally used to reduce the extra queries

  • Thats exactly it, you want to vertically split the items having one per building attached to the project. Use the sort key to achieve a hierarchy.

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠