Local Secondary Index (LSI)

A table is restricted to a maximum of 5 Local Secondary Indexes

Replication

The attribute item data changes are replicated to the LSI:

  • Synchronously to the primary partion. As a result LSI queries support Strongly Consistent Reads
  • Asynchronously to the secondary partitions in the replication group

LSI-replication

Query/Scan scope

Since the LSI shares the partition with the base table, the item data and the related-index are co-located. The scope of the LSI query & scan is a single base table partition.

Consider this example, in which base table has Partition Key = base_pk and Sort Key = base_sk.An LSI created on this table with an Alternate Sort Key X, will share the table’s partition. What that means is that the index will share the partition’s throughput capacity and storage with the base table.

The scope of the LSI query is a single partition, same as a base table.

LSI-scope

Sort Key Selection

LSI Creation

Can be done only at the time of table creation.

aws dynamodb create-table \
    --table-name Orders \
    --attribute-definitions '[
      {
          "AttributeName": "Customer",
          "AttributeType": "S"
      },
      {
          "AttributeName": "OrderId",
          "AttributeType": "S"
      },
      {
          "AttributeName": "OrderDate",
          "AttributeType": "S"
      }
    ]' \
    --key-schema '[
      {
          "AttributeName": "Customer",
          "KeyType": "HASH"
      },
      {
          "AttributeName": "OrderId",
          "KeyType": "RANGE"
      }
    ]' \
    --local-secondary-indexes '[
      {
          "IndexName": "LSI_OrderDate",
          "KeySchema": [
              {
                  "AttributeName": "Customer",
                  "KeyType": "HASH"
              },
              {
                  "AttributeName": "OrderDate",
                  "KeyType": "RANGE"
              }
          ],
          "Projection": {
              "ProjectionType": "ALL"
          }
      }
    ]' \
    --provisioned-throughput '{
      "ReadCapacityUnits": 1,
      "WriteCapacityUnits": 1
    }'