Search code examples
amazon-dynamodbamazon-dynamodb-index

DynamoDB secondary index is not unique?


I have set a secondary index with only a partition key (without a sort key), but I found that actually I can insert multiple items with the same partition key.

If I issue a query using the partition key in the secondary index, I'll get all the items where the partition key is equal to the given partition key value.

I'm a beginner of DynamoDB, I want to know if set a secondary index with only a partition key, but insert multiple items with the same partition key is a good idea.

I'm using Amplify.js and have this GraphQL schema:

type UserMeta @model @key(fields: ["owner"]) @auth(rules: [
    { allow: owner, operations: [create, delete, update] },
    {
        allow: groups,
        groups: ["Admins"],
        operations: [update, delete]
    }
]) {
    familyName: String
    givenName: String
    facebookUrl: AWSURL
    twitterUrl: AWSURL
    description: String
    careers: [Career] @connection(keyName: "byOwner", fields: ["owner"])
    owner: String!
}

type Career @model @key(name: "byOwner", fields: ["owner"]) @auth(rules: [
    { allow: owner, operations: [create, delete, update] },
    {
        allow: groups,
        groups: ["Admins"],
        operations: [update, delete]
    }
]) {
    id: ID!
    company: String
    companyUrl: AWSURL
    industry: String
    occupation: String
    owner: String!
}

as you can see, the Career table has a secondary index byOwner with a partition key associated with owner(no sort key). but I can query the careers of a UserMeta normally.

with a traditional RDBMS, the index column can not be the same, I don't know why this is possible in DynamoDB, is this a best practice in DynamoDB??

Should I set a sort key for the byOwner index? maybe the sort key can be the id column?


Solution

  • with a traditional RDBMS, the index column can not be the same, I don't know why this is possible in DynamoDB, is this a best practice in DynamoDB??

    Every RDBMS I've worked with allows both both unique and non-unique indexes.

    The only uniqueness available in DDB is for the table's primary key.

    It's very common to have records with the same partition key. In the table, records with the same partition key must have a different sort key.

    For indexes, duplicates are allowed and again, this is very common use case.