I'm trying to use DynamoDB for a project and I need some clarifications about the table schema design and indexes definition. Let me to provide you an example of the data that I need to store.
Suppose that I created a table with this schema:
- ID (Unique key)
- User-ID
- Brand
- Car-Name
- Car-Reference
- ... additional attributes
As you can see, I'm using a Primary-key (ID) that it's an unique-id so I don't need to have a sort-key also. Based on the queries that I need to do, I created some GSI (Global-Secondary-Index) indexes also:
I need to query the table by User-ID and Brand. So I created the Index_1 using User-ID as HASH and Brand as RANGE
I need to query the table by Car-Reference. So I created the Index_2 using Car-Reference as HASH
This from the application point of view seems works fine. But I have some questions here:
- I created GSI indexes but I'm asking myself.. When is the LSI (Local Secondary Index) necessary? From what I understand, it's not possible to create LSI if you are not using a Sort-Key. Is this correct? I understand also that no additional costs are required using LSI, for GSI there are some additional costs instead.
- So, at the moment, from what I described with this example, do you see something wrong or something that could be improved?
Now, suppose that I need to store additional information linked to the User-ID. Just to reuse the same example, we can suppose that we need to store the amount of the customer-service-requests that the user opened for each brand (I know, stupid example ... but let me assume it.. so we need to store a counter and the last request-date linked to the user-id). I have some doubts here:
In order to reduce the costs, maybe it could be better to avoid creating a dedicated table.. so I'm thinking of storing this info into the same table also. Is it a bad design?
Supposing that this could be ok, now I have to store raws like this:
- ID
- User_ID
- Brand
- Requests_Count
- Last request date
So, I need these possible queries:
- By User-ID and Brand
- Maybe some query like "Last request date" < P-date. But I'm not sure about it, so we can skip it for the moment.
My doubts here is:
On this table I already have a GSI for search by User-ID and Brand. I need a way to distinguish if the result rows are related to User-Counter or Car-row-info right? So.. I basically need one more info in order to search by:
User-ID - Brand - InfoType (or something like this)
How can I manage it? I'm a bit confused.