apps/docs/content/docs.v6/postgres/query-optimization/recommendations/queries-on-unindexed-columns.mdx
Optimize provides recommendations to help you identify and resolve performance issues caused by missing database indexes.
The following queries targeting the User model use a where property to filter on columns that do not have indexes:
await prisma.user.findFirst({
where: {
name: "Marc",
},
});
await prisma.user.findFirst({
where: {
name: "Jon",
},
});
await prisma.user.count({
where: {
name: "Nikolas",
},
});
An index allows the database to retrieve data more quickly, similar to how an index in a book helps you locate information without reading every page.
When using Prisma with a where property, if no indexes are defined for the relevant columns, the database may need to scan every row in the table (a “full table scan”) to find matches. This can be undesirable for several reasons:
For large datasets, if the database must scan the entire table to find matching rows, users will experience longer waiting times.
:::warning
While these issues might not appear in development due to smaller datasets, they can become significant problems in production, where datasets are typically much larger.
:::
Indexes create a data structure that stores the indexed column's values along with pointers to the corresponding rows in the table. When you query the database using an indexed column, the database can use this index to quickly locate the relevant rows instead of scanning the entire table.
include.create, update, delete) because the index needs to be updated as well. Avoid excessive indexing on models with frequent write operations.:::warning
Even if you index a column, the database may not always use it. Many database management systems, such as PostgreSQL and MySQL, have a query optimizer that evaluates multiple execution plans and selects the one it estimates to be most efficient. In some cases, this may involve ignoring an existing index in favor of a different execution plan that it determines will perform better for that specific query.
:::