0Pricing
Serverless Backend with AWS Lambda & API Gateway · Lektion

Abfragen mit sekundären Indizes: GSIs und LSIs

Lernen Sie, wie Global und Local Secondary Indexes Ihnen ermöglichen, DynamoDB anhand anderer Attribute als des Primärschlüssels abzufragen, und wie Sie zwischen ihnen wählen.

Abfragen mit sekundären Indizes: GSIs und LSIs ist eine kostenlose Serverless Backend with AWS Lambda & API Gateway-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Serverless Backend with AWS Lambda & API Gateway-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Serverless Backend with AWS Lambda & API Gateway-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Häufig gestellte Fragen

Ist die Lektion „Abfragen mit sekundären Indizes: GSIs und LSIs“ kostenlos?

Ja — der vollständige Text von „Abfragen mit sekundären Indizes: GSIs und LSIs“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Serverless Backend with AWS Lambda & API Gateway-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Serverless Backend with AWS Lambda & API Gateway-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Abfragen mit sekundären Indizes: GSIs und LSIs“?

Lernen Sie, wie Global und Local Secondary Indexes Ihnen ermöglichen, DynamoDB anhand anderer Attribute als des Primärschlüssels abzufragen, und wie Sie zwischen ihnen wählen. Du übst Serverless Backend with AWS Lambda & API Gateway mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Serverless Backend with AWS Lambda & API Gateway zu starten?

Keine Vorkenntnisse erforderlich. Serverless Backend with AWS Lambda & API Gateway auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Abfragen mit sekundären Indizes: GSIs und LSIs“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Serverless Backend with AWS Lambda & API Gateway-Lektion Code schreiben und ausführen?

Ja. Jede Serverless Backend with AWS Lambda & API Gateway-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Einführung in DynamoDB
  2. DynamoDB-Tabellen entwerfen
  3. Lambda- und DynamoDB-Integration
  4. Abfragen mit sekundären Indizes: GSIs und LSIs
← Zurück zu Serverless Backend with AWS Lambda & API Gateway