‹ All guides

A practical introduction to indexes

Understand what an index does, when to add one, and why column order matters.

An index is an additional structure that helps a database find rows. It can make a useful query much faster, but it also takes space and must be maintained when data changes. Good indexes start with actual queries, not a desire to index every column.

Start with a frequent lookup

If your application often loads orders for one user, an index on orders.user_id is a reasonable candidate. The database can use it to narrow the search rather than inspecting every row. Whether it actually uses the index depends on table size, selectivity, statistics, and the query planner.

CREATE INDEX idx_orders_user_id
  ON orders (user_id);

In SchemaCraft, select a table, open the Indexes tab in the properties inspector, and add an index. Select the columns in the order you want them to appear in the index.

Column order is part of the design

A multicolumn B-tree index on (user_id, created_at) can be useful for queries that filter by user and then work with creation time. An index on (created_at, user_id) is a different structure and may perform differently for those queries.

CREATE INDEX idx_orders_user_created
  ON orders (user_id, created_at);

A useful starting principle is to align leading columns with common equality filters, then consider range and ordering requirements. This is a heuristic, not a guarantee. Test it against your target database using representative data and query plans.

Unique indexes enforce a rule

A unique index prevents duplicate combinations of its columns. For example, a unique index on (workspace_id, slug) allows the same slug in different workspaces while preventing duplicates within one workspace.

SchemaCraft uses single-column unique indexes when checking whether a column may be referenced by a foreign key. A unique index over several columns does not make each individual column unique.

Check existing indexes before adding more

Primary key and unique constraints commonly create supporting indexes. Foreign key behavior differs between databases: some create a suitable index automatically, while others do not index the referencing column for you. Avoid assuming identical behavior across dialects.

Measure the tradeoff

SchemaCraft models ordinary column indexes and unique indexes. Expression indexes, partial indexes, included columns, and engine-specific index methods are outside its current model; SQL imports report unsupported index definitions.

For deeper guidance, read PostgreSQL’s index documentation and SQLite’s query planner overview.

Put it into practice.

Make a small schema and explore the SQL as you go.

Open the designer