0Pricing
PostgreSQL Performance & Query Optimization · Lektion

Primärschlüssel und Ersatzschlüssel entwerfen

Lernen Sie, wie sich die Wahl zwischen natürlichen Schlüsseln, sequenziellen Ersatzschlüsseln und UUIDs auf Indexgröße, Einfügedurchsatz und die allgemeine Abfrageleistung auswirkt.

Primärschlüssel und Ersatzschlüssel entwerfen ist eine kostenlose PostgreSQL Performance & Query Optimization-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 PostgreSQL Performance & Query Optimization-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der PostgreSQL Performance & Query Optimization-Kurs umfasst insgesamt 4 Lektionen.

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

Natural vs Surrogate Keys

A natural key is a real-world attribute (e.g. email). A surrogate key is a meaningless generated value (e.g. an integer id). Surrogate keys stay stable even when business data changes.

Why Key Choice Affects Performance

The primary key is referenced by every foreign key and many indexes. A wide key bloats all of those structures, increasing disk usage and cache pressure. Narrow keys keep indexes small and fast.

Sequential Integer Keys

The classic choice is a monotonically increasing integer. New rows append to the end of the B-tree, minimizing page splits and keeping inserts fast.

CREATE TABLE orders (
  id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  total NUMERIC
);

IDENTITY vs serial

Prefer the SQL-standard GENERATED ALWAYS AS IDENTITY over the older serial pseudo-type. It is cleaner and avoids ownership quirks with the underlying sequence.

The UUID Temptation

UUIDs are great for distributed systems because clients can generate them. But random UUIDs (v4) scatter inserts all over the index, causing page splits and poor cache locality.

CREATE TABLE events (
  id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
  payload JSONB
);

Time-Ordered UUIDs

If you need UUIDs, prefer a time-ordered variant (UUIDv7) so values increase roughly with time. This restores the append-friendly behavior of sequential keys while keeping global uniqueness.

Key Width Matters

A BIGINT is 8 bytes; a UUID is 16 bytes. Every secondary index stores the primary key, so wider keys multiply storage across all of them. Measure the impact.

SELECT pg_size_pretty(pg_relation_size('orders_pkey'));

Composite Primary Keys

Sometimes the natural key spans two columns, such as (order_id, line_no) in a detail table. Keep composite keys narrow and put the most selective column first.

CREATE TABLE order_lines (
  order_id BIGINT,
  line_no  INT,
  PRIMARY KEY (order_id, line_no)
);

Foreign Keys Inherit the Cost

Every child row stores a copy of the parent key. A 16-byte UUID parent key makes a million-row child table 8 MB larger than an 8-byte integer would. Multiply by every referencing table.

Choosing in Practice

Guidelines:

  • Default to BIGINT IDENTITY for single-database apps
  • Use time-ordered UUIDs when clients must generate ids or you shard
  • Avoid random v4 UUIDs as primary keys on hot insert paths
  • Keep composite natural keys short

Indexing the Foreign Key Side

Whatever key you pick, always index the child's foreign key column. Without it, deleting or updating a parent forces a full scan of the child table to check references.

CREATE INDEX idx_order_lines_order
ON order_lines (order_id);

Quick Check

Test your key-design knowledge.

Recap

You learned key design for performance:

  • Surrogate keys stay stable; natural keys can change
  • Narrow keys shrink every index and foreign key
  • Sequential BIGINT IDENTITY inserts are cheap
  • Random v4 UUIDs scatter inserts; prefer time-ordered UUIDs
  • Keep composite keys short and selective-first

Häufig gestellte Fragen

Ist die Lektion „Primärschlüssel und Ersatzschlüssel entwerfen“ kostenlos?

Ja — der vollständige Text von „Primärschlüssel und Ersatzschlüssel entwerfen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des PostgreSQL Performance & Query Optimization-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der PostgreSQL Performance & Query Optimization-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Primärschlüssel und Ersatzschlüssel entwerfen“?

Lernen Sie, wie sich die Wahl zwischen natürlichen Schlüsseln, sequenziellen Ersatzschlüsseln und UUIDs auf Indexgröße, Einfügedurchsatz und die allgemeine Abfrageleistung auswirkt. Du übst PostgreSQL Performance & Query Optimization 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 PostgreSQL Performance & Query Optimization zu starten?

Keine Vorkenntnisse erforderlich. PostgreSQL Performance & Query Optimization 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 „Primärschlüssel und Ersatzschlüssel entwerfen“?

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 PostgreSQL Performance & Query Optimization-Lektion Code schreiben und ausführen?

Ja. Jede PostgreSQL Performance & Query Optimization-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. Zielkonflikte zwischen Normalisierung und Denormalisierung
  2. Geeignete Datentypen auswählen
  3. Große Tabellen partitionieren
  4. Primärschlüssel und Ersatzschlüssel entwerfen
← Zurück zu PostgreSQL Performance & Query Optimization