Indexes and write cost

Make an important read cheaper, and account for the work moved to writes.

Written deep dive25% previewBy Daniyal Saqib

Read the opening here. A free account gives you the full guide.

Suppose you are designing a task-management application where users create tasks, update their status, and retrieve their recent tasks.

The database contains millions of tasks, and you have added several indexes to speed up filtering and sorting. Reads have improved, but creating and updating tasks now takes longer.

The challenge is to identify which indexes provide meaningful benefits, determine which can be removed or redesigned, and restore write performance without making frequently used queries unacceptably slow.

01

Identify the queries that need optimization

Choose an index for the complete query pattern, including its filtering, ordering and result size.

Start by examining the queries the application executes rather than adding indexes based on individual columns.

Suppose users frequently retrieve their most recent tasks:

As the table grows, the database may need to examine many records belonging to the user and sort them by creation time. An index on user_id can help locate matching tasks, but it may not eliminate the sorting work.

A composite index on (user_id, created_at) may support both filtering and ordering, allowing the database to retrieve the most recent tasks more efficiently.

With this ordered composite index, entries for the same user are grouped together, and timestamps are ordered within that user's group.

3 more sections to read

Sign in to keep reading

Indexes and write cost

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.

3 more sections to read

There’s more in the full article

Continue reading with a free account.

Keep reading

Free account. No payment required.

Inside the full article

Continue the guide with 3 more sections.

  1. Understand the write cost of existing indexesRead after sign-in
  2. Choose which indexes to keep or redesignRead after sign-in
  3. Validate the read/write trade-offRead after sign-in
Continue reading

3 more sections to read

Sign in to keep reading

Indexes and write cost

Continue this section and read the complete guide and explore its visuals.

Free account. No payment required.

Read how we handle account information in our privacy notice.