0Pricing
Serverless Backend with AWS Lambda & API Gateway · レッスン

セカンダリインデックスによるクエリ:GSIとLSI

Global Secondary IndexとLocal Secondary Indexを使って、主キー以外の属性でDynamoDBをクエリする方法と、両者の選び方を学びます。

「セカンダリインデックスによるクエリ:GSIとLSI」はCoddyKit上の無料Serverless Backend with AWS Lambda & API Gatewayレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはServerless Backend with AWS Lambda & API Gateway学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

The Primary Key Limitation

DynamoDB queries efficiently only by the primary key (partition key, optionally plus sort key). To query by other attributes you need a secondary index.

  • Scanning the whole table is slow and costly
  • Indexes give you efficient alternate access paths

What Is a Secondary Index?

A secondary index is an alternate view of your table data, organized by a different key. DynamoDB keeps it in sync automatically as you write to the base table.

Global Secondary Index (GSI)

A GSI can use any attributes as its partition and sort key, different from the table key. It has its own throughput and can be created anytime.

  • Query by a completely different attribute
  • Spans all partitions of the table
  • Eventually consistent reads only

Local Secondary Index (LSI)

An LSI shares the table partition key but uses a different sort key. It must be created at table creation time and supports strongly consistent reads.

GSI vs LSI at a Glance

Choosing between them:

  • GSI: different partition key, created anytime, eventually consistent
  • LSI: same partition key, created with the table, strongly consistent

Defining a GSI

You specify the index name and its key schema. Reads and writes against the index are billed separately from the base table.

aws dynamodb update-table --table-name Orders \
  --attribute-definitions AttributeName=status,AttributeType=S \
  --global-secondary-index-updates "[{...}]"

Querying an Index

To query a GSI you pass its name and the index key condition. The query returns items matching that alternate key.

aws dynamodb query --table-name Orders \
  --index-name status-index \
  --key-condition-expression "status = :s" \
  --expression-attribute-values '{":s":{"S":"PAID"}}'

Projection: Which Attributes Are Copied

An index projection controls which attributes are copied into it: keys only, a selected set, or all. Projecting fewer attributes saves storage but may force a base-table fetch.

  • KEYS_ONLY: smallest, just keys
  • INCLUDE: keys plus chosen attributes
  • ALL: full copy, biggest

Index Throughput and Cost

GSIs consume their own read and write capacity. Every base-table write that affects an indexed attribute also writes to the GSI, so over-indexing increases cost.

Sparse Indexes

If an item lacks the index key attribute, it is not written to the index. This creates a sparse index, an efficient way to query only items that have a certain attribute set.

Designing Access Patterns First

In DynamoDB you design indexes around your access patterns, not the other way around. List the queries your app needs, then create the minimal set of indexes that serve them.

Quick Check

Test your index knowledge.

Recap

You learned how secondary indexes enable querying DynamoDB by non-primary-key attributes. You compared GSIs (flexible keys, created anytime, eventually consistent) with LSIs (shared partition key, created with the table, strongly consistent), and covered projections, throughput, sparse indexes, and access-pattern-first design.

よくある質問

「セカンダリインデックスによるクエリ:GSIとLSI」レッスンは無料ですか?

はい。「セカンダリインデックスによるクエリ:GSIとLSI」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Serverless Backend with AWS Lambda & API Gatewayコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

「セカンダリインデックスによるクエリ:GSIとLSI」で何を学びますか?

Global Secondary IndexとLocal Secondary Indexを使って、主キー以外の属性でDynamoDBをクエリする方法と、両者の選び方を学びます。 ブラウザで直接実行するハンズオンコードでServerless Backend with AWS Lambda & API Gatewayを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Serverless Backend with AWS Lambda & API Gatewayを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのServerless Backend with AWS Lambda & API Gatewayは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「セカンダリインデックスによるクエリ:GSIとLSI」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このServerless Backend with AWS Lambda & API Gatewayレッスンでコードを書いて実行できますか?

はい。すべてのServerless Backend with AWS Lambda & API Gatewayレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. DynamoDB入門
  2. DynamoDBテーブルの設計
  3. LambdaとDynamoDBの統合
  4. セカンダリインデックスによるクエリ:GSIとLSI
← Serverless Backend with AWS Lambda & API Gatewayに戻る