Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: "Improving Query Performance with Indexes using Prisma: B-Tree Indexes"
slug: "improving-query-performance-using-indexes-2-MyoiJNMFTsfq"
date: "2022-09-16"
updatedAt: "2026-07-07"
updatedAt: "2026-07-08"
authors:
- "Alex Ruheni"
metaTitle: "B-Tree Indexes in Prisma ORM: Make Slow Postgres Queries 80x Faster"
Expand All @@ -18,23 +18,8 @@ tags:

A B-tree index is the default index type in PostgreSQL. It stores a sorted copy of the indexed columns, so the database can find matching rows in logarithmic time instead of scanning the whole table. In this article you will see how B-tree indexes work, then measure their effect directly: a filtered query over 500,000 rows drops from roughly 66ms to under 1ms, an improvement of about 80x, after adding a single `@@index` line to a [Prisma ORM](https://www.prisma.io/orm) schema. Every command and number in this walkthrough was verified on Prisma ORM 7.8 and PostgreSQL 17.

## Overview

- [Introduction](#introduction)
- [The data structure that powers indexes](#the-data-structure-that-powers-indexes)
- [The time complexity of a B-tree](#the-time-complexity-of-a-b-tree)
- [When to use a B-tree index](#when-to-use-a-b-tree-index)
- [How to add an index with Prisma ORM](#how-to-add-an-index-with-prisma-orm)
- [Prerequisites](#prerequisites)
- [Set up the project](#set-up-the-project)
- [Define the schema and configuration](#define-the-schema-and-configuration)
- [Start a local Prisma Postgres database](#start-a-local-prisma-postgres-database)
- [Create and seed the database](#create-and-seed-the-database)
- [Measure the slow query](#measure-the-slow-query)
- [Improve query performance with an index](#improve-query-performance-with-an-index)
- [Bonus: Add an index to multiple fields](#bonus-add-an-index-to-multiple-fields)
- [Frequently asked questions](#frequently-asked-questions)
- [Summary and next steps](#summary-and-next-steps)
> **Updated (July 2026):** Fully revised for **Prisma ORM 7**: the walkthrough uses the `prisma-client` generator, a driver adapter, and a local Prisma Postgres database, and every command and number was verified end-to-end on Prisma ORM 7.8 and PostgreSQL 17.


## Introduction

Expand Down Expand Up @@ -152,7 +137,7 @@ export default defineConfig({

### Start a local Prisma Postgres database

Start a local [Prisma Postgres](https://www.prisma.io/postgres) server:
Start a local [Prisma Postgres](https://www.prisma.io/docs/postgres) server:

```shell
npx prisma dev
Expand Down Expand Up @@ -408,6 +393,6 @@ In this part, you learned what the structure of indexes looks like, and signific

You also learned how to add indexes to multiple columns, how to define the index sort order, and how to measure query time in Prisma ORM 7 with a client extension now that middleware is gone.

The workflow you used here carries beyond the local server. `npx prisma init --db` provisions a managed [Prisma Postgres](https://www.prisma.io/postgres) database with the same schema-first workflow, so the index you declared in code applies to production exactly the way it did locally. And if you are building new applications with AI coding agents, [Prisma Next](/prisma-next-early-access-write-your-contract-prompt-your-agent-ship-your-app) (Early Access, becoming Prisma 8) extends the same idea further: the schema becomes a central data contract that you or your agent evolve with type-safe queries and structured, machine-readable output.
The workflow you used here carries beyond the local server. `npx prisma init --db` provisions a managed Prisma Postgres database with the same schema-first workflow, so the index you declared in code applies to production exactly the way it did locally. And if you are building new applications with AI coding agents, [Prisma Next](/prisma-next-early-access-write-your-contract-prompt-your-agent-ship-your-app) (Early Access, becoming Prisma 8) extends the same idea further: the schema becomes a central data contract that you or your agent evolve with type-safe queries and structured, machine-readable output.

In the [next article](/improving-query-performance-using-indexes-3-kduk351qv1), you will learn how to work with Hash indexes in your application using Prisma ORM.
Loading